sql_string.h
上传用户:romrleung
上传日期:2022-05-23
资源大小:18897k
文件大小:7k
源码类别:

MySQL数据库

开发平台:

Visual C++

  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);
  22. int stringcmp(const String *a,const String *b);
  23. String *copy_if_not_alloced(String *a,String *b,uint32 arg_length);
  24. class String
  25. {
  26.   char *Ptr;
  27.   uint32 str_length,Alloced_length;
  28.   bool alloced;
  29.   CHARSET_INFO *str_charset;
  30. public:
  31.   String()
  32.   { 
  33.     Ptr=0; str_length=Alloced_length=0; alloced=0; 
  34.     str_charset= &my_charset_latin1;
  35.   }
  36.   String(uint32 length_arg)
  37.   { 
  38.     alloced=0; Alloced_length=0; (void) real_alloc(length_arg); 
  39.     str_charset= &my_charset_latin1;
  40.   }
  41.   String(const char *str, CHARSET_INFO *cs)
  42.   { 
  43.     Ptr=(char*) str; str_length=(uint) strlen(str); Alloced_length=0; alloced=0;
  44.     str_charset=cs;
  45.   }
  46.   String(const char *str,uint32 len, CHARSET_INFO *cs)
  47.   { 
  48.     Ptr=(char*) str; str_length=len; Alloced_length=0; alloced=0;
  49.     str_charset=cs;
  50.   }
  51.   String(char *str,uint32 len, CHARSET_INFO *cs)
  52.   { 
  53.     Ptr=(char*) str; Alloced_length=str_length=len; alloced=0;
  54.     str_charset=cs;
  55.   }
  56.   String(const String &str)
  57.   { 
  58.     Ptr=str.Ptr ; str_length=str.str_length ;
  59.     Alloced_length=str.Alloced_length; alloced=0; 
  60.     str_charset=str.str_charset;
  61.   }
  62.   static void *operator new(size_t size) { return (void*) sql_alloc((uint) size); }
  63.   static void operator delete(void *ptr_arg,size_t size) /*lint -e715 */
  64.     { sql_element_free(ptr_arg); }
  65.   ~String() { free(); }
  66.   inline void set_charset(CHARSET_INFO *charset) { str_charset=charset; }
  67.   inline CHARSET_INFO *charset() const { return str_charset; }
  68.   inline uint32 length() const { return str_length;}
  69.   inline uint32 alloced_length() const { return Alloced_length;}
  70.   inline char& operator [] (uint32 i) const { return Ptr[i]; }
  71.   inline void length(uint32 len) { str_length=len ; }
  72.   inline bool is_empty() { return (str_length == 0); }
  73.   inline const char *ptr() const { return Ptr; }
  74.   inline char *c_ptr()
  75.   {
  76.     if (!Ptr || Ptr[str_length]) /* Should be safe */
  77.       (void) realloc(str_length);
  78.     return Ptr;
  79.   }
  80.   inline char *c_ptr_quick()
  81.   {
  82.     if (Ptr && str_length < Alloced_length)
  83.       Ptr[str_length]=0;
  84.     return Ptr;
  85.   }
  86.   inline char *c_ptr_safe()
  87.   {
  88.     if (Ptr && str_length < Alloced_length)
  89.       Ptr[str_length]=0;
  90.     else
  91.       (void) realloc(str_length);
  92.     return Ptr;
  93.   }
  94.   void set(String &str,uint32 offset,uint32 arg_length)
  95.   {
  96.     free();
  97.     Ptr=(char*) str.ptr()+offset; str_length=arg_length; alloced=0;
  98.     if (str.Alloced_length)
  99.       Alloced_length=str.Alloced_length-offset;
  100.     else
  101.       Alloced_length=0;
  102.     str_charset=str.str_charset;
  103.   }
  104.   inline void set(char *str,uint32 arg_length, CHARSET_INFO *cs)
  105.   {
  106.     free();
  107.     Ptr=(char*) str; str_length=Alloced_length=arg_length ; alloced=0;
  108.     str_charset=cs;
  109.   }
  110.   inline void set(const char *str,uint32 arg_length, CHARSET_INFO *cs)
  111.   {
  112.     free();
  113.     Ptr=(char*) str; str_length=arg_length; Alloced_length=0 ; alloced=0;
  114.     str_charset=cs;
  115.   }
  116.   inline void set_quick(char *str,uint32 arg_length, CHARSET_INFO *cs)
  117.   {
  118.     if (!alloced)
  119.     {
  120.       Ptr=(char*) str; str_length=Alloced_length=arg_length;
  121.     }
  122.     str_charset=cs;
  123.   }
  124.   bool set(longlong num, CHARSET_INFO *cs);
  125.   bool set(ulonglong num, CHARSET_INFO *cs);
  126.   bool set(double num,uint decimals, CHARSET_INFO *cs);
  127.   inline void free()
  128.   {
  129.     if (alloced)
  130.     {
  131.       alloced=0;
  132.       Alloced_length=0;
  133.       my_free(Ptr,MYF(0));
  134.       Ptr=0;
  135.       str_length=0; /* Safety */
  136.     }
  137.   }
  138.   inline bool alloc(uint32 arg_length)
  139.   {
  140.     if (arg_length < Alloced_length)
  141.       return 0;
  142.     return real_alloc(arg_length);
  143.   }
  144.   bool real_alloc(uint32 arg_length); // Empties old string
  145.   bool realloc(uint32 arg_length);
  146.   inline void shrink(uint32 arg_length) // Shrink buffer
  147.   {
  148.     if (arg_length < Alloced_length)
  149.     {
  150.       char *new_ptr;
  151.       if (!(new_ptr=(char*) my_realloc(Ptr,arg_length,MYF(0))))
  152.       {
  153. Alloced_length = 0;
  154. real_alloc(arg_length);
  155.       }
  156.       else
  157.       {
  158. Ptr=new_ptr;
  159. Alloced_length=arg_length;
  160.       }
  161.     }
  162.   }
  163.   bool is_alloced() { return alloced; }
  164.   inline String& operator = (const String &s)
  165.   {
  166.     if (&s != this)
  167.     {
  168.       free();
  169.       Ptr=s.Ptr ; str_length=s.str_length ; Alloced_length=s.Alloced_length;
  170.       alloced=0;
  171.     }
  172.     return *this;
  173.   }
  174.   bool copy(); // Alloc string if not alloced
  175.   bool copy(const String &s); // Allocate new string
  176.   bool copy(const char *s,uint32 arg_length, CHARSET_INFO *cs); // Allocate new string
  177.   bool copy(const char*s,uint32 arg_length, CHARSET_INFO *csfrom, CHARSET_INFO *csto);
  178.   bool append(const String &s);
  179.   bool append(const char *s,uint32 arg_length=0);
  180.   bool append(IO_CACHE* file, uint32 arg_length);
  181.   int strstr(const String &search,uint32 offset=0); // Returns offset to substring or -1
  182.   int strstr_case(const String &s,uint32 offset=0);
  183.   int strrstr(const String &search,uint32 offset=0); // Returns offset to substring or -1
  184.   bool replace(uint32 offset,uint32 arg_length,const String &to);
  185.   inline bool append(char chr)
  186.   {
  187.     if (str_length < Alloced_length)
  188.     {
  189.       Ptr[str_length++]=chr;
  190.     }
  191.     else
  192.     {
  193.       if (realloc(str_length+1))
  194. return 1;
  195.       Ptr[str_length++]=chr;
  196.     }
  197.     return 0;
  198.   }
  199.   bool fill(uint32 max_length,char fill);
  200.   void strip_sp();
  201.   inline void caseup() { my_caseup(str_charset,Ptr,str_length); }
  202.   inline void casedn() { my_casedn(str_charset,Ptr,str_length); }
  203.   friend int sortcmp(const String *a,const String *b);
  204.   friend int stringcmp(const String *a,const String *b);
  205.   friend String *copy_if_not_alloced(String *a,String *b,uint32 arg_length);
  206.   uint32 numchars();
  207.   int charpos(int i,uint32 offset=0);
  208.   int reserve(uint32 space_needed)
  209.   {
  210.     return realloc(str_length + space_needed);
  211.   }
  212.   int reserve(uint32 space_needed, uint32 grow_by);
  213.   /*
  214.     The following append operations do NOT check alloced memory
  215.     q_*** methods writes values of parameters itself
  216.     qs_*** methods writes string representation of value
  217.   */
  218.   void q_append(const char &c)
  219.   {
  220.     Ptr[str_length++] = c;
  221.   }
  222.   void q_append(const uint32 &n)
  223.   {
  224.     int4store(Ptr + str_length, n);
  225.     str_length += 4;
  226.   }
  227.   void q_append(double d)
  228.   {
  229.     float8store(Ptr + str_length, d);
  230.     str_length += 8;
  231.   }
  232.   void q_append(double *d)
  233.   {
  234.     float8store(Ptr + str_length, *d);
  235.     str_length += 8;
  236.   }
  237.   void q_append(const char *data, uint32 data_len)
  238.   {
  239.     memcpy(Ptr + str_length, data, data_len);
  240.     str_length += data_len;
  241.   }
  242.   void WriteAtPosition(int position, uint32 value)
  243.   {
  244.     int4store(Ptr + position,value);
  245.   }
  246.   void qs_append(const char *str);
  247.   void qs_append(double d);
  248.   void qs_append(double *d);
  249.   void qs_append(const char &c);
  250. };