Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "dbManager.h"
- dbManager::dbManager(void)
- {
- }
- dbManager::~dbManager(void)
- {
- }
- int dbManager::wordcmp(const char* str1, const char* word, int *offset)
- {
- unsigned int len = strlen(word);
- // returns 0 if equals
- if (memcmp (str1, word, len)==0)
- {
- if (offset!=NULL)
- *offset +=len;
- return 0;
- }
- // if not any of above
- return -1;
- }
- int dbManager::skipws(const char* str)
- {
- int i = 0;
- while(str[i] == ' ')
- {
- i++;
- }
- // returns count of ws
- return i;
- }
- int dbManager::charfind(const char* str, char letter)
- {
- int len = strlen(str);
- for (int i =0; i<len; i++)
- {
- if (str[i] == letter)
- return i;
- }
- // nie znaleziono
- return -1;
- }
- bool dbManager::TakeInt(const char* cmdline, int* x, int* offset)
- {
- int end = charfind(cmdline, ',');
- if (end<0) // nie ma juz przecinka
- {
- end = charfind(cmdline, ')');
- if (end<0) // ani nawiasu
- return false;
- }
- char* strtonumbuff = (char*)malloc(sizeof(char)*end);
- for(int i=0; i<end; i++)
- {
- strtonumbuff[i]=cmdline[i];
- }
- strtonumbuff[end]=0;
- *x = atoi(strtonumbuff);
- //free(strtonumbuff);
- // moves offset after ',' or ')'
- if (offset!=NULL)
- *offset +=end+1;
- return true;
- }
- // ======public============
- bool dbManager::ProcessCmd(const char* cmdline)
- {
- int offset = skipws(cmdline); // obsluga ws na poczatku cmd
- if (wordcmp(cmdline+offset, "dzien dobry", &offset) == 0)
- {
- cout << "?\n";
- } else if (wordcmp(cmdline+offset, "koniec", &offset) == 0)
- {
- return false;
- } else if (wordcmp(cmdline+offset, "dodaj", &offset) == 0)
- {
- Add(cmdline+offset);
- }
- return true;
- }
- bool dbManager::Add(const char* cmdline)
- {
- int offset = skipws(cmdline);
- if (wordcmp(cmdline+offset, "liczby", &offset) == 0)
- {
- offset += skipws(cmdline+offset); // obsluga ws
- // sprawdzanie czy nawias
- if (wordcmp(cmdline+offset, "(", &offset) ==0)
- {
- // sprawdzanie jakiego typu komorki sa w db i wczytanie ich
- int* x = new int;
- while(TakeInt(cmdline+offset, x, &offset))
- {
- cout << *x << endl;
- }
- }
- else
- cout << "cos zle z parametrami dodaj liczby\n";
- } else
- cout << "wpisales jakies gowno -.-\n";
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement