~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_string.h

  • Committer: Toru Maesaka
  • Date: 2008-12-17 07:16:37 UTC
  • mto: (685.1.40 devel) (713.1.5 devel)
  • mto: This revision was merged to the branch mainline in revision 713.
  • Revision ID: dev@torum.net-20081217071637-7j9040w7lpms77r2
Removed my_time() and added error checking

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
/* This file is originally from the mysql distribution. Coded by monty */
24
24
 
25
 
#ifdef USE_PRAGMA_INTERFACE
26
 
#pragma interface                       /* gcc class implementation */
27
 
#endif
28
25
 
29
26
#ifndef NOT_FIXED_DEC
30
27
#define NOT_FIXED_DEC                   31
31
28
#endif
32
29
 
33
 
#include <libdrizzle/drizzle_com.h>
 
30
#include <drizzled/common.h>
 
31
#include <mysys/iocache.h>
 
32
#include <stdlib.h>
 
33
#include <string.h>
34
34
 
35
35
class String;
36
 
int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
37
 
String *copy_if_not_alloced(String *a,String *b,uint32_t arg_length);
38
 
uint32_t copy_and_convert(char *to, uint32_t to_length, const CHARSET_INFO * const to_cs,
39
 
                        const char *from, uint32_t from_length,
40
 
                        const CHARSET_INFO * const from_cs, uint32_t *errors);
41
 
uint32_t well_formed_copy_nchars(const CHARSET_INFO * const to_cs,
42
 
                               char *to, uint32_t to_length,
43
 
                               const CHARSET_INFO * const from_cs,
44
 
                               const char *from, uint32_t from_length,
45
 
                               uint32_t nchars,
46
 
                               const char **well_formed_error_pos,
47
 
                               const char **cannot_convert_error_pos,
48
 
                               const char **from_end_pos);
49
 
size_t my_copy_with_hex_escaping(const CHARSET_INFO * const cs,
50
 
                                 char *dst, size_t dstlen,
51
 
                                 const char *src, size_t srclen);
 
36
 
 
37
#if defined(__cplusplus)
 
38
extern "C" {
 
39
#endif
 
40
 
 
41
  int sortcmp(const String *a,const String *b, const CHARSET_INFO * const cs);
 
42
  int stringcmp(const String *a,const String *b);
 
43
  String *copy_if_not_alloced(String *a,String *b,uint32_t arg_length);
 
44
  uint32_t copy_and_convert(char *to, uint32_t to_length,
 
45
                            const CHARSET_INFO * const to_cs,
 
46
                            const char *from, uint32_t from_length,
 
47
                            const CHARSET_INFO * const from_cs,
 
48
                            uint32_t *errors);
 
49
  uint32_t well_formed_copy_nchars(const CHARSET_INFO * const to_cs,
 
50
                                   char *to, uint32_t to_length,
 
51
                                   const CHARSET_INFO * const from_cs,
 
52
                                   const char *from, uint32_t from_length,
 
53
                                   uint32_t nchars,
 
54
                                   const char **well_formed_error_pos,
 
55
                                   const char **cannot_convert_error_pos,
 
56
                                   const char **from_end_pos);
 
57
  size_t my_copy_with_hex_escaping(const CHARSET_INFO * const cs,
 
58
                                   char *dst, size_t dstlen,
 
59
                                   const char *src, size_t srclen);
 
60
 
 
61
#if defined(__cplusplus)
 
62
}
 
63
#endif
52
64
 
53
65
class String
54
66
{
58
70
  const CHARSET_INFO *str_charset;
59
71
public:
60
72
  String()
61
 
  { 
62
 
    Ptr=0; str_length=Alloced_length=0; alloced=0; 
63
 
    str_charset= &my_charset_bin; 
 
73
  {
 
74
    Ptr=0; str_length=Alloced_length=0; alloced=0;
 
75
    str_charset= &my_charset_bin;
64
76
  }
65
77
  String(uint32_t length_arg)
66
 
  { 
67
 
    alloced=0; Alloced_length=0; (void) real_alloc(length_arg); 
 
78
  {
 
79
    alloced=0; Alloced_length=0; (void) real_alloc(length_arg);
68
80
    str_charset= &my_charset_bin;
69
81
  }
70
82
  String(const char *str, const CHARSET_INFO * const cs)
71
 
  { 
 
83
  {
72
84
    Ptr=(char*) str; str_length=(uint) strlen(str); Alloced_length=0; alloced=0;
73
85
    str_charset=cs;
74
86
  }
75
87
  String(const char *str,uint32_t len, const CHARSET_INFO * const cs)
76
 
  { 
 
88
  {
77
89
    Ptr=(char*) str; str_length=len; Alloced_length=0; alloced=0;
78
90
    str_charset=cs;
79
91
  }
80
92
  String(char *str,uint32_t len, const CHARSET_INFO * const cs)
81
 
  { 
 
93
  {
82
94
    Ptr=(char*) str; Alloced_length=str_length=len; alloced=0;
83
95
    str_charset=cs;
84
96
  }
85
97
  String(const String &str)
86
 
  { 
 
98
  {
87
99
    Ptr=str.Ptr ; str_length=str.str_length ;
88
 
    Alloced_length=str.Alloced_length; alloced=0; 
 
100
    Alloced_length=str.Alloced_length; alloced=0;
89
101
    str_charset=str.str_charset;
90
102
  }
91
103
  static void *operator new(size_t size, MEM_ROOT *mem_root)
92
104
  { return (void*) alloc_root(mem_root, (uint) size); }
93
 
  static void operator delete(void *ptr_arg __attribute__((unused)),
94
 
                              size_t size __attribute__((unused)))
 
105
  static void operator delete(void *, size_t)
95
106
  { TRASH(ptr_arg, size); }
96
 
  static void operator delete(void *ptr_arg __attribute__((unused)),
97
 
                              MEM_ROOT *mem_root __attribute__((unused)))
 
107
  static void operator delete(void *, MEM_ROOT *)
98
108
  { /* never called */ }
99
109
  ~String() { free(); }
100
110
 
178
188
    statement to be run on the remote server, and have a comma after each.
179
189
    When the list is complete, I "chop" off the trailing comma
180
190
 
181
 
    ex. 
182
 
      String stringobj; 
 
191
    ex.
 
192
      String stringobj;
183
193
      stringobj.append("VALUES ('foo', 'fi', 'fo',");
184
194
      stringobj.chop();
185
195
      stringobj.append(")");
189
199
    VALUES ('foo', 'fi', 'fo',
190
200
    VALUES ('foo', 'fi', 'fo'
191
201
    VALUES ('foo', 'fi', 'fo')
192
 
      
 
202
 
193
203
  */
194
204
  inline void chop()
195
205
  {
196
 
    Ptr[str_length--]= '\0'; 
 
206
    Ptr[str_length--]= '\0';
197
207
  }
198
208
 
199
209
  inline void free()
220
230
    if (arg_length < Alloced_length)
221
231
    {
222
232
      char *new_ptr;
223
 
      if (!(new_ptr=(char*) my_realloc(Ptr,arg_length,MYF(0))))
 
233
      if (!(new_ptr=(char*) ::realloc(Ptr,arg_length)))
224
234
      {
225
235
        Alloced_length = 0;
226
236
        real_alloc(arg_length);
238
248
    if (&s != this)
239
249
    {
240
250
      /*
241
 
        It is forbidden to do assignments like 
 
251
        It is forbidden to do assignments like
242
252
        some_string = substring_of_that_string
243
253
       */
244
254
      assert(!s.uses_buffer_owned_by(this));
265
275
  bool append(const char *s,uint32_t arg_length);
266
276
  bool append(const char *s,uint32_t arg_length, const CHARSET_INFO * const cs);
267
277
  bool append(IO_CACHE* file, uint32_t arg_length);
268
 
  bool append_with_prefill(const char *s, uint32_t arg_length, 
 
278
  bool append_with_prefill(const char *s, uint32_t arg_length,
269
279
                           uint32_t full_length, char fill_char);
270
280
  int strstr(const String &search,uint32_t offset=0); // Returns offset to substring or -1
271
281
  int strrstr(const String &search,uint32_t offset=0); // Returns offset to substring or -1
379
389
  }
380
390
};
381
391
 
382
 
static inline bool check_if_only_end_space(const CHARSET_INFO * const cs, char *str, 
 
392
static inline bool check_if_only_end_space(const CHARSET_INFO * const cs, char *str,
383
393
                                           char *end)
384
394
{
385
395
  return str+ cs->cset->scan(cs, str, end, MY_SEQ_SPACES) == end;
386
396
}
387
397
 
388
 
inline
389
 
bool operator==(const String &s1, const String &s2)
390
 
{
391
 
  return stringcmp(&s1,&s2) == 0;
392
 
}
393
 
 
394
 
inline
395
 
bool operator!=(const String &s1, const String &s2)
396
 
{
397
 
  return !(s1 == s2);
 
398
extern "C++" {
 
399
bool operator==(const String &s1, const String &s2);
 
400
bool operator!=(const String &s1, const String &s2);
398
401
}
399
402
 
400
403
#endif /* DRIZZLE_SERVER_SQL_STRING_H */