Advertisement
hugol

Untitled

Nov 25th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #include "dbManager.h"
  2.  
  3.  
  4. dbManager::dbManager(void)
  5. {
  6. }
  7.  
  8.  
  9. dbManager::~dbManager(void)
  10. {
  11. }
  12.  
  13. int dbManager::wordcmp(const char* str1, const char* word, int *offset)
  14. {
  15.     unsigned int len = strlen(word);
  16.     // returns 0 if equals
  17.     if (memcmp (str1, word, len)==0)
  18.     {
  19.         if (offset!=NULL)
  20.             *offset +=len;
  21.  
  22.         return 0;
  23.     }
  24.     // if not any of above
  25.     return -1;
  26. }
  27.  
  28. int dbManager::skipws(const char* str)
  29. {
  30.     int i = 0;
  31.     while(str[i] == ' ')
  32.     {
  33.         i++;
  34.     }
  35.     // returns count of ws
  36.     return i;
  37. }
  38.  
  39. int dbManager::charfind(const char* str, char letter)
  40. {
  41.     int len = strlen(str);
  42.     for (int i =0; i<len; i++)
  43.     {
  44.         if (str[i] == letter)
  45.             return i;
  46.     }
  47.     // nie znaleziono
  48.     return -1;
  49. }
  50.  
  51. bool dbManager::TakeInt(const char* cmdline, int* x, int* offset)
  52. {
  53.     int end = charfind(cmdline, ',');
  54.     if (end<0) // nie ma juz przecinka
  55.     {
  56.         end = charfind(cmdline, ')');
  57.         if (end<0) // ani nawiasu
  58.             return false;
  59.     }
  60.  
  61.     char* strtonumbuff = (char*)malloc(sizeof(char)*end);
  62.     for(int i=0; i<end; i++)
  63.     {
  64.         strtonumbuff[i]=cmdline[i];
  65.     }
  66.     strtonumbuff[end]=0;
  67.     *x = atoi(strtonumbuff);
  68.     //free(strtonumbuff);
  69.     // moves offset after ',' or ')'
  70.     if (offset!=NULL)
  71.         *offset +=end+1;
  72.  
  73.     return true;
  74. }
  75.  
  76. // ======public============
  77.  
  78. bool dbManager::ProcessCmd(const char* cmdline)
  79. {
  80.     int offset = skipws(cmdline); // obsluga ws na poczatku cmd
  81.     if (wordcmp(cmdline+offset, "dzien dobry", &offset) == 0)
  82.     {
  83.         cout << "?\n";
  84.     } else if (wordcmp(cmdline+offset, "koniec", &offset) == 0)
  85.     {
  86.         return false;
  87.     } else if (wordcmp(cmdline+offset, "dodaj", &offset) == 0)
  88.     {
  89.         Add(cmdline+offset);
  90.     }
  91.  
  92.     return true;
  93. }
  94.  
  95. bool dbManager::Add(const char* cmdline)
  96. {
  97.     int offset = skipws(cmdline);
  98.  
  99.     if (wordcmp(cmdline+offset, "liczby", &offset) == 0)
  100.     {
  101.         offset += skipws(cmdline+offset); // obsluga ws
  102.  
  103.         // sprawdzanie czy nawias
  104.         if (wordcmp(cmdline+offset, "(", &offset) ==0)
  105.         {
  106.             // sprawdzanie jakiego typu komorki sa w db i wczytanie ich
  107.             int* x = new int;
  108.             while(TakeInt(cmdline+offset, x, &offset))
  109.             {
  110.                 cout << *x << endl;
  111.             }
  112.         }
  113.         else
  114.             cout << "cos zle z parametrami dodaj liczby\n";
  115.        
  116.     } else
  117.         cout << "wpisales jakies gowno -.-\n";
  118.  
  119.     return true;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement