COOPY » Guide  version 0.6.5
/home/paulfitz/cvs/coopy_scm/coopy/src/libcoopy_core/Stringer.cpp
Go to the documentation of this file.
00001 #define WANT_MAP2STRING
00002 #define WANT_VECTOR2STRING
00003 
00004 #include <coopy/Stringer.h>
00005 #include <coopy/SheetStyle.h>
00006 #include <coopy/DataSheet.h>
00007 
00008 #include <stdio.h>
00009 
00010 using namespace std;
00011 using namespace coopy::store;
00012 
00013 string stringer_encoder(bool x) {
00014   char buf[256];
00015   sprintf(buf,"%s",x?"true":"false");
00016   return buf;
00017 }
00018 
00019 string stringer_encoder(int x) {
00020   char buf[256];
00021   sprintf(buf,"%d",x);
00022   return buf;
00023 }
00024 
00025 string stringer_encoder(const string& x) {
00026   SheetStyle style;
00027   string result = DataSheet::encodeCell(SheetCell(x,false),style);
00028   return (result!="")?result:"\"\"";
00029 }
00030 
00031 string stringer_encoder(const coopy::store::SheetCell& x) {
00032   //return x.toString();
00033   SheetStyle style;
00034   string result = DataSheet::encodeCell(x,style);
00035   return (result!="")?result:"\"\"";
00036 }
00037 
00038 std::string stringify(const std::string& x) {
00039   return x;
00040 }
00041 
00042 std::string stringify(const coopy::store::SheetCell& x) {
00043   return x.toString();
00044 }
00045 
00046 
00047 
00048 string quoteSql(string x, char del, bool alwaysQuote) {
00049   bool quote = false;
00050   bool easy = true;
00051   string result = x;
00052   char del2 = del;
00053   if (del=='[') del2 = ']';
00054   for (int i=0; i<(int)x.length(); i++) {
00055     char ch = x[i];
00056     if ((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')||(ch>='0'&&ch<='9')||
00057         ch=='_') {
00058       // ok
00059     } else {
00060       if (ch==del||ch==del2) {
00061         easy = false;
00062       }
00063       quote = true;
00064     }
00065   }
00066   if ((alwaysQuote||quote)&&easy) {
00067     result = string() + del + x + del2;
00068   } else if (!easy) {
00069     result = del;
00070     for (int i=0; i<(int)x.length(); i++) {
00071       if (x[i]!=del) {
00072         result += x[i];
00073       } else {
00074         result += del;
00075         result += del2;
00076       }
00077     }
00078     result += del2;
00079   }
00080   return result;
00081 }
00082 
00083 std::string Stringer::getSpreadsheetColumnName(int x) {
00084   string current;
00085   int offset = 0;
00086   int rem = x;
00087   do {
00088     offset = rem%26;
00089     rem = rem/26;
00090     char A[2] = "A";
00091     A[0] += offset;
00092     current = string(A) + current;
00093   } while (rem!=0);
00094   return current;
00095 }
00096 
00097 std::string Stringer::nextSpreadsheetColumnName(std::string current) {
00098     int len = current.length();
00099     int cursor = len-1;
00100     while (cursor>=0) {
00101       if (current[cursor]!='Z') {
00102         current[cursor]++;
00103         break;
00104       }
00105       current[cursor] = 'A';
00106       cursor--;
00107       if (cursor<0) {
00108         current += 'A';
00109       }
00110     }  
00111     return current;
00112 }
00113 
00114 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines