inlib
1.2.0
|
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved. 00002 // See the file inlib.license for terms. 00003 00004 #ifndef inlib_cstr 00005 #define inlib_cstr 00006 00007 #include <cstring> // strcpy 00008 #include <cstdlib> // malloc,free 00009 00010 namespace inlib { 00011 00012 // NOTE : have str_ to avoid clashes with various strxxx cpp macro 00013 // that may come from C or system headers. 00014 00015 inline char* str_dup(const char* a_cstr) { 00016 return ::strcpy((char*)::malloc(::strlen(a_cstr)+1),a_cstr); 00017 } 00018 00019 inline void str_del(char*& a_cstr) { 00020 if(a_cstr==NULL) return; 00021 ::free(a_cstr); 00022 a_cstr = NULL; 00023 } 00024 00025 inline char* str_new(size_t a_l = 0,char a_char = ' ') { 00026 char* s = (char*)::malloc((a_l+1)*sizeof(char)); 00027 if(s==NULL) return NULL; 00028 char* pos = s; 00029 for(size_t c=0;c<a_l;c++,pos++) *pos = a_char; 00030 *(s+a_l) = 0; 00031 return s; 00032 } 00033 00034 inline bool str_cat(char*& a_1,const char a_c) { 00035 size_t l1 = ::strlen(a_1); 00036 char* s = (char*)::malloc(l1+1+1); 00037 if(!s) return false; 00038 ::memcpy(s,a_1,l1); 00039 ::memcpy(s+l1,&a_c,1); 00040 *(s+l1+1) = 0; 00041 ::free(a_1); 00042 a_1 = s; 00043 return true; 00044 } 00045 00046 inline bool str_cat(char*& a_1,const char* a_2) { 00047 size_t l1 = ::strlen(a_1); 00048 size_t l2 = ::strlen(a_2); 00049 char* s = (char*)::malloc(l1+l2+1); 00050 if(!s) return false; 00051 ::memcpy(s,a_1,l1); 00052 ::memcpy(s+l1,a_2,l2); 00053 *(s+l1+l2) = 0; 00054 ::free(a_1); 00055 a_1 = s; 00056 return true; 00057 } 00058 00059 inline void str_rev(char* a_s) { 00060 size_t l = ::strlen(a_s); 00061 size_t hl = l/2; 00062 char* beg = a_s; 00063 char* end = a_s+l-1; 00064 for(size_t i=0;i<hl;i++) { 00065 char c = *end; 00066 *end = *beg; 00067 *beg = c; 00068 beg++;end--; 00069 } 00070 } 00071 00072 inline char* str_sub(const char* a_s, 00073 unsigned int a_pos, 00074 unsigned int a_sz = 0) { //0 = take up end. 00075 size_t l = ::strlen(a_s); 00076 if(a_pos>=l) return 0; //throw std::out_of_range 00077 size_t ls; 00078 if(a_sz) { 00079 ls = (a_sz<(l-a_pos)?a_sz:(l-a_pos)); //min(a_sz,l-a_pos) 00080 } else { 00081 ls = l-a_pos; 00082 } 00083 char* s = (char*)::malloc(ls+1); 00084 if(!s) return 0; 00085 //abcdefgh l=8 00086 //0123456789 00087 ::memcpy(s,a_s+a_pos,ls); 00088 *(s+ls) = 0; 00089 return s; 00090 } 00091 00092 inline char* str_rep(const char* a_s,unsigned int a_pos,unsigned int a_sz,const char* a_new) { 00093 //not tested yet. 00094 size_t las = ::strlen(a_s); 00095 if(a_pos>=las) return 0; //throw std::out_of_range 00096 if(a_pos+a_sz>las) return 0; 00097 size_t lan = ::strlen(a_new); 00098 unsigned int num = a_sz<lan?a_sz:lan; 00099 //abcdefghij : l = 10 00100 //0123456789 00101 // p 00102 size_t le = las-(a_pos+a_sz); 00103 size_t ls = a_pos+num+le; 00104 char* s = (char*)::malloc(ls+1); 00105 if(!s) return 0; 00106 ::memcpy(s,a_s,a_pos); 00107 ::memcpy(s+a_pos,a_new,num); 00108 if(le) ::memcpy(s+a_pos+num,a_s+a_pos+a_sz,le); 00109 *(s+ls) = 0; 00110 return s; 00111 } 00112 00113 } 00114 00115 #endif