Advertisement
hugol

Untitled

Nov 26th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.50 KB | None | 0 0
  1. #include "dbManager.h"
  2.  
  3.  
  4. dbManager::dbManager(db* pDB)
  5. {
  6.     DB = pDB;
  7. }
  8.  
  9.  
  10. dbManager::~dbManager(void)
  11. {
  12. }
  13.  
  14. int dbManager::wordcmp(const char* str1, const char* word, int *offset)
  15. {
  16.     unsigned int len = strlen(word);
  17.     // returns 0 if equals
  18.     if (memcmp (str1, word, len)==0)
  19.     {
  20.         if (len==strlen(str1) || str1[len] == '(' || str1[len] == ' ') // wyraz nie konczy sie gdy nie ma po nim ' ' lub '('
  21.         {
  22.             if (offset!=NULL)
  23.                 *offset +=len;
  24.  
  25.             return 0;
  26.         }
  27.     }
  28.     // if not any of above
  29.     return -1;
  30. }
  31.  
  32. int dbManager::lettercmp(const char* str1, char letter, int *offset)
  33. {
  34.     if (str1[0] == letter && strlen(str1)>0)
  35.     {
  36.         (*offset)++;
  37.         return 0;
  38.     }
  39.        
  40.     return -1;
  41. }
  42.  
  43. int dbManager::skipws(const char* str)
  44. {
  45.     int i = 0;
  46.     while(str[i] == ' ')
  47.     {
  48.         i++;
  49.     }
  50.     // returns count of ws
  51.     return i;
  52. }
  53.  
  54. bool dbManager::gotochar(const char* str, char letter, int* offset)
  55. {
  56.     int len = strlen(str);
  57.     for (int i=0; i<len; i++)
  58.     {
  59.         if (str[i] == letter)
  60.         {
  61.             *offset+=i+1;
  62.             return true;
  63.         }
  64.     }
  65.     return false;
  66. }
  67.  
  68. bool dbManager::clamp(int value, unsigned long long int mask)
  69. {
  70.     cout << "wieksze / mniejsze od: " << (mask >> TYP::MASK::CLAMPHIGHER) % 32768 << ", " << (mask >> TYP::MASK::CLAMPLOWER) % 32768 << endl;
  71.     return ((mask >> TYP::MASK::CLAMPHIGHER) % 32768 <= value && (mask >> TYP::MASK::CLAMPLOWER) % 32768 >= value);
  72. }
  73.  
  74. bool dbManager::TakeInt(const char* cmdline, int* output, int* offset)
  75. {
  76.     int end = 0;
  77.     if (!gotochar(cmdline, ',', &end) && !gotochar(cmdline, ')', &end)) // szukaj zakonczenia parametru, a jak ne to przecinka.
  78.         return false;
  79.  
  80.     char* strtonumbuff = (char*)malloc(sizeof(char)*end);
  81.     for(int i=0; i<end; i++)
  82.     {
  83.         strtonumbuff[i]=cmdline[i];
  84.     }
  85.     strtonumbuff[end]=0;
  86.     *output = atoi(strtonumbuff);
  87.  
  88.     //free(strtonumbuff);
  89.     // moves offset after ',' or ')'
  90.     if (offset!=NULL)
  91.         *offset +=end;
  92.  
  93.     return true;
  94. }
  95.  
  96. bool dbManager::TakeString(const char* cmdline, char** output, int* offset)
  97. {
  98.     int start = 0, end = 0;
  99.     if (!gotochar(cmdline, '"', &start) || !gotochar(cmdline+start, '"', &end))
  100.         return false;
  101.     start;
  102.     end+=start-1;
  103.     if (end>strlen(cmdline) || start>strlen(cmdline))
  104.         return false;
  105.  
  106.     *output = (char*) malloc(sizeof(char) * (end-start) +1);
  107.    
  108.     for (int i=start; i<end; i++)
  109.     {
  110.         (*output)[i-start] = cmdline[i];
  111.     }
  112.     (*output)[end-start] = 0;
  113.     *offset +=end+2; // skip '"' i ','
  114.     return true;
  115. }
  116.  
  117. void dbManager::PrintSyntax(int n)
  118. {
  119.     // wypisywanie skladni
  120.     cout << "dawaj mi: " << DB->TabName(n) <<"(" << DB->Tabs[n].column_names[0];
  121.     for (int i=1; i<DB->Tabs[n].column_count; i++)
  122.         cout << "," << DB->Tabs[n].column_names[i];
  123.     cout << ")" << endl;
  124. }
  125.  
  126. // ======public============
  127.  
  128. bool dbManager::ProcessCmd(const char* cmdline)
  129. {
  130.     int offset = skipws(cmdline); // obsluga ws na poczatku cmd
  131.     if (wordcmp(cmdline+offset, "dzien dobry", &offset) == 0)
  132.     {
  133.         cout << "?\n";
  134.     } else if (wordcmp(cmdline+offset, "koniec", &offset) == 0)
  135.     {
  136.         return false;
  137.     } else if (wordcmp(cmdline+offset, "dodaj", &offset) == 0)
  138.     {
  139.         Add(cmdline+offset);
  140.     }
  141.  
  142.     return true;
  143. }
  144.  
  145. bool dbManager::Add(const char* cmdline)
  146. {
  147.     int offset = skipws(cmdline);
  148.  
  149.     bool tablematches = false;
  150.     for (int itab=0; itab<DB->TabCount(); itab++)
  151.     {
  152.         if (wordcmp(cmdline+offset, DB->TabName(itab), &offset) == 0)
  153.         {
  154.             offset += skipws(cmdline+offset); // obsluga ws
  155.             tablematches = true;
  156.             PrintSyntax(itab);
  157.  
  158.             int record = 1;
  159.             // sprawdzanie czy nawias
  160.             while (gotochar(cmdline+offset, '(', &offset))
  161.             {
  162.                
  163.                 cout << "Rekord nr: " << record << " : ";
  164.                 // sprawdzanie jakiego typu komorki sa w db i wczytanie ich
  165.                 int buffint;
  166.                 for (int i=0; i< DB->ColumnCount(itab); i++)
  167.                 {
  168.                     switch(DB->ColumnType(itab, i) % TYP::MASK::START) // last 2 bytes
  169.                     {
  170.                     case TYP::INT: // dodawanie inta
  171.                         if (!TakeInt(cmdline+offset, &buffint, &offset))
  172.                         {
  173.                             cout << "cos zle z parametrami INT, anuluje dodawanie\n";
  174.                             return false;
  175.                         } else
  176.                         {
  177.                             cout << DB->Tabs[itab].column_names[i] << ": " << buffint << ", ";
  178.                             DB->AddInt(itab, i, buffint);
  179.                         }
  180.                         break;
  181.  
  182.                     case TYP::STRING:
  183.                         char* buffstr;
  184.                         if (!TakeString(cmdline+offset, &buffstr, &offset))
  185.                         {
  186.                             cout << "cos zle z parametrami STRING, anuluje dodawanie\n";
  187.                         } else
  188.                         {
  189.                             cout << DB->Tabs[itab].column_names[i] << ": \"" << buffstr << "\", ";
  190.                             DB->AddString(itab, i, buffstr);
  191.                         }
  192.                         break;
  193.  
  194.                     case TYP::CLAMP:
  195.                         if (!TakeInt(cmdline+offset, &buffint, &offset))
  196.                         {
  197.                             cout << "cos zle z parametrami INT, anuluje dodawanie\n";
  198.                             return false;
  199.                         } else
  200.                         {
  201.                             if (clamp(buffint,DB->ColumnType(itab, i)))
  202.                                 cout << DB->Tabs[itab].column_names[i] << ": " << buffint << ", ";
  203.                             else
  204.                                 cout << "za mala liczba";
  205.                         }
  206.                         break;
  207.  
  208.                     default:
  209.                         cout << "nie rozpoznano typu kolumny !\n" << endl;
  210.                     }
  211.                 }
  212.  
  213.                 offset--;
  214.                 offset += skipws(cmdline+offset); // obsluga ws
  215.  
  216.                 // start : debug
  217.                 cout << endl;          
  218.                 int closeoffset = 0;
  219.                 if (!gotochar(cmdline+offset, ')', &closeoffset))
  220.                 {
  221.                     cout <<"Brak zakonczenia parametru " << record << endl;
  222.                 } else if (closeoffset>1)
  223.                 {
  224.                     cout << "Za duzo wartosci w parametrze " << record << endl;
  225.                 };
  226.                 // end : debug
  227.  
  228.                 record++;
  229.                 offset++;
  230.                 offset += skipws(cmdline+offset); // obsluga ws
  231.             }
  232.  
  233.         }
  234.     }
  235.    
  236.     if (!tablematches)
  237.                 cout << " nie ma takiej tabeli \n";
  238.    
  239.     return true;
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement