string.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #ifndef _PFC_STRING_H_
  2. #define _PFC_STRING_H_
  3. template <class myChar> class string_base
  4. {
  5. private:
  6. myChar * ptr;
  7. size_t size,used;
  8. void makespace(size_t s)
  9. {
  10. if (size<s)
  11. {
  12. do size<<=1; while(size<s);
  13. ptr=(myChar*)realloc(ptr,size*sizeof(myChar));
  14. }
  15. }
  16. static int mylen(const myChar * p) {int r=0;while(p[r]) r++;return r;}
  17. public:
  18. const myChar * get_ptr() const {return ptr;}
  19. void add_char(myChar c)
  20. {
  21. makespace(used+2);
  22. ptr[used++]=c;
  23. ptr[used]=0;
  24. }
  25. string_base()
  26. {
  27. used=0;
  28. size=8;
  29. ptr=(myChar*)malloc(size*sizeof(myChar));
  30. ptr[0]=0;
  31. }
  32. ~string_base() { if (ptr) free(ptr);}
  33. operator const myChar*() const {return get_ptr();}
  34. const myChar & operator*() const {return *get_ptr();}
  35. size_t length() const {return used;}
  36. int is_empty() const {return used==0;}
  37. void add_string(const myChar * c)
  38. {
  39. int d=mylen(c);
  40. makespace(used+d+1);
  41. memcpy(ptr+used,c,sizeof(myChar)*d);
  42. used+=d;
  43. ptr[used]=0;
  44. }
  45. void add_string_n(const myChar * c,int count)
  46. {
  47. makespace(used+count+1);
  48. memcpy(ptr+used,c,sizeof(myChar)*count);
  49. ptr[used+count]=0;
  50. used+=mylen(ptr+used);
  51. }
  52. void reset() {truncate(0);}
  53. void truncate(size_t x) {if (used>x) {used=x;ptr[x]=0;}}
  54. void set_string(const myChar * s) {reset();add_string(s);}
  55. myChar *buffer_get(size_t n)
  56. {
  57. makespace(n+1);
  58. memset(ptr,0,size);
  59. return ptr;
  60. }
  61. inline void buffer_done() {used=mylen(ptr);}
  62. void set_char(int offset,myChar c)//hack for some ghey routines
  63. {
  64. if (!c) truncate(offset);
  65. else if (offset<used) ptr[offset]=c;
  66. }
  67. int find_first(myChar c) //return -1 if not found
  68. {
  69. int n;
  70. for(n=0;n<used;n++)
  71. {
  72. if (ptr[n]==c) return n;
  73. }
  74. return -1;
  75. }
  76. int find_last(myChar c)
  77. {
  78. int n;;
  79. for(n=used-1;n>=0;n--)
  80. {
  81. if (ptr[n]==c) return n;
  82. }
  83. return -1;
  84. }
  85. int replace_char(myChar c1,myChar c2)
  86. {
  87. int rv=0;
  88. int n;
  89. for(n=0;n<used;n++)
  90. {
  91. if (ptr[n]==c1) {ptr[n]=c2;rv++;}
  92. }
  93. return rv;
  94. }
  95. };
  96. template<class myChar>
  97. class string_buffer
  98. {
  99. private:
  100. string_base<myChar> * parent;
  101. myChar * data;
  102. public:
  103. string_buffer(string_base<myChar> & s,UINT siz) {parent=&s;data=s.buffer_get(siz);}
  104. ~string_buffer() {parent->buffer_done();}
  105. operator myChar* () {return data;}
  106. };
  107. #define string_buffer_w string_buffer<WCHAR>
  108. #define string_buffer_a string_buffer<char>
  109. class string_w;
  110. #define string string_a
  111. class string_a : public string_base<char>
  112. {
  113. public:
  114. string_a() {}
  115. string_a(HWND w) {s_GetWindowText(w);}
  116. string_a(const char* z) {set_string(z);}
  117. string_a(const WCHAR* z) {set_string_w(z);}
  118. string_a(const string_a& z) {set_string(z);}
  119. string_a(const string_w& z);
  120. void add_string_w(const WCHAR * c);
  121. void set_string_w(const WCHAR * c);
  122. void s_GetWindowText(HWND w);
  123. inline void from_window(HWND w) {s_GetWindowText(w);}
  124. void s_SetWindowText(HWND w);
  125. const char * operator=(const char * s) {set_string(s);return get_ptr();}
  126. const char * operator+=(const char * s) {add_string(s);return get_ptr();}
  127. const char * operator=(const WCHAR * s) {set_string_w(s);return get_ptr();}
  128. const char * operator+=(const WCHAR * s) {add_string_w(s);return get_ptr();}
  129. const char * operator=(string_a & s) {set_string(s);return get_ptr();}
  130. const char * operator+=(string_a & s) {add_string(s);return get_ptr();}
  131. inline void s_GetDlgItemText(HWND w,int id) {s_GetWindowText(GetDlgItem(w,id));}
  132. inline void s_SetDlgItemText(HWND w,int id) {s_SetWindowText(GetDlgItem(w,id));}
  133. bool reg_read(HKEY hk,const char * name);
  134. void reg_write(HKEY hk,const char * name);
  135. };
  136. class string_printf_a : public string_a
  137. {
  138. public:
  139. string_printf_a(const char * fmt,...);
  140. };
  141. #define string_printf string_printf_a
  142. template<class myChar>
  143. class string_file_title : public string_base<myChar>
  144. {
  145. public:
  146. string_file_title(const myChar * fn)
  147. {
  148. const myChar * ptr=fn,*dot=0,*src=fn;
  149. while(*ptr)
  150. {
  151. if (*ptr=='\\' || *ptr=='/' || *ptr==':') src=ptr+1;
  152. else if (*ptr=='.') dot=ptr;
  153. ptr++;
  154. }
  155. while(*src && (!dot || src<dot)) add_char(*(src++));
  156. }
  157. };
  158. #define string_f2t_a string_file_title<char>
  159. #define string_f2t_w string_file_title<WCHAR>
  160. template<class myChar>
  161. class string_extension : public string_base<myChar>
  162. {
  163. public:
  164. string_extension(const myChar * foo)
  165. {
  166. const myChar * ptr = 0;
  167. while(*foo)
  168. {
  169. if (*foo == '.') ptr = foo;
  170. foo++;
  171. }
  172. if (ptr) set_string(ptr+1);
  173. }
  174. };
  175. #define string_extension_a string_extension<char>
  176. #define string_extension_w string_extension<WCHAR>
  177. #endif //_PFC_STRING_H_