really simple sql buffer


/ Published in: C++
Save to your folder(s)



Copy this code and paste it in your HTML
  1. template <int N>
  2. struct sqlbuffN
  3. { char bf[N];
  4. char * p;
  5. sqlbuffN() { clear(); }
  6.  
  7. operator const char *() const { return bf; }
  8. void clear() { p=bf; *p=0; }
  9. void set(const char *s) { strcpy(bf,s); p=bf+strlen(s); }
  10. sqlbuffN & operator < (int i)
  11. { p+=sprintf(p,"%d",i); return *this; }
  12. sqlbuffN & operator <= (int i)
  13. { p+=sprintf(p,"%d",i); return *this; }
  14. sqlbuffN & operator < (const char * cp)
  15. { strcpy(p,cp); p+=strlen(p); return *this; }
  16. sqlbuffN & operator <= (const char * cp)
  17. { p+=sqlQuote(p,cp); return * this;}
  18. sqlbuffN & operator < (const std::string & s) { return *this<(s.c_str()); }
  19. sqlbuffN & operator <=(const std::string & s) { return *this<=(s.c_str()); }
  20.  
  21. };
  22.  
  23.  
  24.  
  25. struct sqlbuff
  26. { mem_buffer bf;
  27. int off;
  28.  
  29. explicit sqlbuff(int sz = 4096)
  30. { bf.resize(sz,0); }
  31. void clear() { off=0; }
  32.  
  33. sqlbuff & operator <(const char * txt)
  34. { int len = strlen(txt);
  35. bf.growto2(off+len+1,mem_buffer::copydata);
  36. strcpy(bf.p+off,txt); off+=len;
  37. return *this;
  38. }
  39. sqlbuff & operator <= (const char * txt)
  40. { int len = 2*strlen(txt)+2;
  41. bf.growto2(off+len+1,mem_buffer::copydata);
  42. off+=sqlQuote(bf.p+off,txt);
  43. return * this;
  44. }
  45. sqlbuff & operator < (const std::string & s) { return *this<(s.c_str()); }
  46. sqlbuff & operator <=(const std::string & s) { return *this<=(s.c_str()); }
  47.  
  48.  
  49.  
  50. sqlbuff & operator < (int i)
  51. { bf.growto2(off+15,mem_buffer::copydata); off+=sprintf(bf.p+off,"%d",i); return *this; }
  52. sqlbuff & operator <= (int i)
  53. { bf.growto2(off+15,mem_buffer::copydata); off+=sprintf(bf.p+off,"%d",i); return *this; }
  54.  
  55. operator const char *() const { return bf.p; }
  56. };

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.