You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

358 lines
10 KiB

26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
22 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
22 years ago
26 years ago
  1. /* Copyright (C) 2000 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  13. /* This file is originally from the mysql distribution. Coded by monty */
  14. #ifdef USE_PRAGMA_INTERFACE
  15. #pragma interface /* gcc class implementation */
  16. #endif
  17. #ifndef NOT_FIXED_DEC
  18. #define NOT_FIXED_DEC 31
  19. #endif
  20. class String;
  21. int sortcmp(const String *a,const String *b, CHARSET_INFO *cs);
  22. String *copy_if_not_alloced(String *a,String *b,uint32 arg_length);
  23. uint32 copy_and_convert(char *to, uint32 to_length, CHARSET_INFO *to_cs,
  24. const char *from, uint32 from_length,
  25. CHARSET_INFO *from_cs, uint *errors);
  26. class String
  27. {
  28. char *Ptr;
  29. uint32 str_length,Alloced_length;
  30. bool alloced;
  31. CHARSET_INFO *str_charset;
  32. public:
  33. String()
  34. {
  35. Ptr=0; str_length=Alloced_length=0; alloced=0;
  36. str_charset= &my_charset_bin;
  37. }
  38. String(uint32 length_arg)
  39. {
  40. alloced=0; Alloced_length=0; (void) real_alloc(length_arg);
  41. str_charset= &my_charset_bin;
  42. }
  43. String(const char *str, CHARSET_INFO *cs)
  44. {
  45. Ptr=(char*) str; str_length=(uint) strlen(str); Alloced_length=0; alloced=0;
  46. str_charset=cs;
  47. }
  48. String(const char *str,uint32 len, CHARSET_INFO *cs)
  49. {
  50. Ptr=(char*) str; str_length=len; Alloced_length=0; alloced=0;
  51. str_charset=cs;
  52. }
  53. String(char *str,uint32 len, CHARSET_INFO *cs)
  54. {
  55. Ptr=(char*) str; Alloced_length=str_length=len; alloced=0;
  56. str_charset=cs;
  57. }
  58. String(const String &str)
  59. {
  60. Ptr=str.Ptr ; str_length=str.str_length ;
  61. Alloced_length=str.Alloced_length; alloced=0;
  62. str_charset=str.str_charset;
  63. }
  64. static void *operator new(size_t size, MEM_ROOT *mem_root)
  65. { return (void*) alloc_root(mem_root, (uint) size); }
  66. static void operator delete(void *ptr_arg,size_t size)
  67. { TRASH(ptr_arg, size); }
  68. static void operator delete(void *ptr_arg, MEM_ROOT *mem_root)
  69. { /* never called */ }
  70. ~String() { free(); }
  71. inline void set_charset(CHARSET_INFO *charset) { str_charset= charset; }
  72. inline CHARSET_INFO *charset() const { return str_charset; }
  73. inline uint32 length() const { return str_length;}
  74. inline uint32 alloced_length() const { return Alloced_length;}
  75. inline char& operator [] (uint32 i) const { return Ptr[i]; }
  76. inline void length(uint32 len) { str_length=len ; }
  77. inline bool is_empty() { return (str_length == 0); }
  78. inline void mark_as_const() { Alloced_length= 0;}
  79. inline const char *ptr() const { return Ptr; }
  80. inline char *c_ptr()
  81. {
  82. if (!Ptr || Ptr[str_length]) /* Should be safe */
  83. (void) realloc(str_length);
  84. return Ptr;
  85. }
  86. inline char *c_ptr_quick()
  87. {
  88. if (Ptr && str_length < Alloced_length)
  89. Ptr[str_length]=0;
  90. return Ptr;
  91. }
  92. inline char *c_ptr_safe()
  93. {
  94. if (Ptr && str_length < Alloced_length)
  95. Ptr[str_length]=0;
  96. else
  97. (void) realloc(str_length);
  98. return Ptr;
  99. }
  100. void set(String &str,uint32 offset,uint32 arg_length)
  101. {
  102. DBUG_ASSERT(&str != this);
  103. free();
  104. Ptr=(char*) str.ptr()+offset; str_length=arg_length; alloced=0;
  105. if (str.Alloced_length)
  106. Alloced_length=str.Alloced_length-offset;
  107. else
  108. Alloced_length=0;
  109. str_charset=str.str_charset;
  110. }
  111. inline void set(char *str,uint32 arg_length, CHARSET_INFO *cs)
  112. {
  113. free();
  114. Ptr=(char*) str; str_length=Alloced_length=arg_length ; alloced=0;
  115. str_charset=cs;
  116. }
  117. inline void set(const char *str,uint32 arg_length, CHARSET_INFO *cs)
  118. {
  119. free();
  120. Ptr=(char*) str; str_length=arg_length; Alloced_length=0 ; alloced=0;
  121. str_charset=cs;
  122. }
  123. bool set_ascii(const char *str, uint32 arg_length);
  124. inline void set_quick(char *str,uint32 arg_length, CHARSET_INFO *cs)
  125. {
  126. if (!alloced)
  127. {
  128. Ptr=(char*) str; str_length=Alloced_length=arg_length;
  129. }
  130. str_charset=cs;
  131. }
  132. bool set_int(longlong num, bool unsigned_flag, CHARSET_INFO *cs);
  133. bool set(longlong num, CHARSET_INFO *cs)
  134. { return set_int(num, false, cs); }
  135. bool set(ulonglong num, CHARSET_INFO *cs)
  136. { return set_int((longlong)num, true, cs); }
  137. bool set_real(double num,uint decimals, CHARSET_INFO *cs);
  138. /*
  139. PMG 2004.11.12
  140. This is a method that works the same as perl's "chop". It simply
  141. drops the last character of a string. This is useful in the case
  142. of the federated storage handler where I'm building a unknown
  143. number, list of values and fields to be used in a sql insert
  144. statement to be run on the remote server, and have a comma after each.
  145. When the list is complete, I "chop" off the trailing comma
  146. ex.
  147. String stringobj;
  148. stringobj.append("VALUES ('foo', 'fi', 'fo',");
  149. stringobj.chop();
  150. stringobj.append(")");
  151. In this case, the value of string was:
  152. VALUES ('foo', 'fi', 'fo',
  153. VALUES ('foo', 'fi', 'fo'
  154. VALUES ('foo', 'fi', 'fo')
  155. */
  156. inline void chop()
  157. {
  158. Ptr[str_length--]= '\0';
  159. }
  160. inline void free()
  161. {
  162. if (alloced)
  163. {
  164. alloced=0;
  165. Alloced_length=0;
  166. my_free(Ptr,MYF(0));
  167. Ptr=0;
  168. str_length=0; /* Safety */
  169. }
  170. }
  171. inline bool alloc(uint32 arg_length)
  172. {
  173. if (arg_length < Alloced_length)
  174. return 0;
  175. return real_alloc(arg_length);
  176. }
  177. bool real_alloc(uint32 arg_length); // Empties old string
  178. bool realloc(uint32 arg_length);
  179. inline void shrink(uint32 arg_length) // Shrink buffer
  180. {
  181. if (arg_length < Alloced_length)
  182. {
  183. char *new_ptr;
  184. if (!(new_ptr=(char*) my_realloc(Ptr,arg_length,MYF(0))))
  185. {
  186. Alloced_length = 0;
  187. real_alloc(arg_length);
  188. }
  189. else
  190. {
  191. Ptr=new_ptr;
  192. Alloced_length=arg_length;
  193. }
  194. }
  195. }
  196. bool is_alloced() { return alloced; }
  197. inline String& operator = (const String &s)
  198. {
  199. if (&s != this)
  200. {
  201. /*
  202. It is forbidden to do assignments like
  203. some_string = substring_of_that_string
  204. */
  205. DBUG_ASSERT(!s.uses_buffer_owned_by(this));
  206. free();
  207. Ptr=s.Ptr ; str_length=s.str_length ; Alloced_length=s.Alloced_length;
  208. alloced=0;
  209. }
  210. return *this;
  211. }
  212. bool copy(); // Alloc string if not alloced
  213. bool copy(const String &s); // Allocate new string
  214. bool copy(const char *s,uint32 arg_length, CHARSET_INFO *cs); // Allocate new string
  215. static bool needs_conversion(uint32 arg_length,
  216. CHARSET_INFO *cs_from, CHARSET_INFO *cs_to,
  217. uint32 *offset);
  218. bool copy_aligned(const char *s, uint32 arg_length, uint32 offset,
  219. CHARSET_INFO *cs);
  220. bool set_or_copy_aligned(const char *s, uint32 arg_length, CHARSET_INFO *cs);
  221. bool copy(const char*s,uint32 arg_length, CHARSET_INFO *csfrom,
  222. CHARSET_INFO *csto, uint *errors);
  223. bool append(const String &s);
  224. bool append(const char *s);
  225. bool append(const char *s,uint32 arg_length);
  226. bool append(const char *s,uint32 arg_length, CHARSET_INFO *cs);
  227. bool append(IO_CACHE* file, uint32 arg_length);
  228. bool append_with_prefill(const char *s, uint32 arg_length,
  229. uint32 full_length, char fill_char);
  230. int strstr(const String &search,uint32 offset=0); // Returns offset to substring or -1
  231. int strrstr(const String &search,uint32 offset=0); // Returns offset to substring or -1
  232. bool replace(uint32 offset,uint32 arg_length,const char *to,uint32 length);
  233. bool replace(uint32 offset,uint32 arg_length,const String &to);
  234. inline bool append(char chr)
  235. {
  236. if (str_length < Alloced_length)
  237. {
  238. Ptr[str_length++]=chr;
  239. }
  240. else
  241. {
  242. if (realloc(str_length+1))
  243. return 1;
  244. Ptr[str_length++]=chr;
  245. }
  246. return 0;
  247. }
  248. bool fill(uint32 max_length,char fill);
  249. void strip_sp();
  250. friend int sortcmp(const String *a,const String *b, CHARSET_INFO *cs);
  251. friend int stringcmp(const String *a,const String *b);
  252. friend String *copy_if_not_alloced(String *a,String *b,uint32 arg_length);
  253. uint32 numchars();
  254. int charpos(int i,uint32 offset=0);
  255. int reserve(uint32 space_needed)
  256. {
  257. return realloc(str_length + space_needed);
  258. }
  259. int reserve(uint32 space_needed, uint32 grow_by);
  260. /*
  261. The following append operations do NOT check alloced memory
  262. q_*** methods writes values of parameters itself
  263. qs_*** methods writes string representation of value
  264. */
  265. void q_append(const char c)
  266. {
  267. Ptr[str_length++] = c;
  268. }
  269. void q_append(const uint32 n)
  270. {
  271. int4store(Ptr + str_length, n);
  272. str_length += 4;
  273. }
  274. void q_append(double d)
  275. {
  276. float8store(Ptr + str_length, d);
  277. str_length += 8;
  278. }
  279. void q_append(double *d)
  280. {
  281. float8store(Ptr + str_length, *d);
  282. str_length += 8;
  283. }
  284. void q_append(const char *data, uint32 data_len)
  285. {
  286. memcpy(Ptr + str_length, data, data_len);
  287. str_length += data_len;
  288. }
  289. void write_at_position(int position, uint32 value)
  290. {
  291. int4store(Ptr + position,value);
  292. }
  293. void qs_append(const char *str, uint32 len);
  294. void qs_append(double d);
  295. void qs_append(double *d);
  296. inline void qs_append(const char c)
  297. {
  298. Ptr[str_length]= c;
  299. str_length++;
  300. }
  301. void qs_append(int i);
  302. void qs_append(uint i);
  303. /* Inline (general) functions used by the protocol functions */
  304. inline char *prep_append(uint32 arg_length, uint32 step_alloc)
  305. {
  306. uint32 new_length= arg_length + str_length;
  307. if (new_length > Alloced_length)
  308. {
  309. if (realloc(new_length + step_alloc))
  310. return 0;
  311. }
  312. uint32 old_length= str_length;
  313. str_length+= arg_length;
  314. return Ptr+ old_length; /* Area to use */
  315. }
  316. inline bool append(const char *s, uint32 arg_length, uint32 step_alloc)
  317. {
  318. uint32 new_length= arg_length + str_length;
  319. if (new_length > Alloced_length && realloc(new_length + step_alloc))
  320. return TRUE;
  321. memcpy(Ptr+str_length, s, arg_length);
  322. str_length+= arg_length;
  323. return FALSE;
  324. }
  325. void print(String *print);
  326. /* Swap two string objects. Efficient way to exchange data without memcpy. */
  327. void swap(String &s);
  328. inline bool uses_buffer_owned_by(const String *s) const
  329. {
  330. return (s->alloced && Ptr >= s->Ptr && Ptr < s->Ptr + s->str_length);
  331. }
  332. };