The code I write is in C on Visual studio 2022.. I don't understand what part of my code is wrong, but it should be about string manipulation. The SplitStr splits string by another string (strtok does it by character).
#define SplitCount 20
TCHAR** SplitStr(TCHAR* pstr, TCHAR *psplitby)
{
if (pstr == NULL || psplitby == NULL)
return NULL;
TCHAR** res = malloc(SplitCount * sizeof(TCHAR*));
int i = 0, j = 0, c = 0;
TCHAR* splitby = _tcsdup(psplitby); // I need to duplicate here
int len = _tcslen(splitby);
TCHAR* str = _tcsdup(pstr); // and here
int strlen1 = _tcslen(str);
while (i < MAXSTR - strlen1) {
if (i + len < strlen1 && _tcsncmp(splitby, &str[i], len) == 0)
{
res[c] = malloc(MAXSTR * sizeof(TCHAR*));
if (res[c] == NULL)
break;
_tcsncpy_s(res[c], MAXSTR, &str[j], i-j);
i += len;
j = i;
c++;
i--;
}
i++;
}
res[c] = malloc(MAXSTR * sizeof(TCHAR*));
if (res[c] != NULL)
{
_tcsncpy_s(res[c], MAXSTR, &str[j], strlen1 - j);
c++;
}
res[c] = _tcsdup(_T("END"));
return res;
}
TCHAR* StrToLower(TCHAR* Str)
{
int i = 0;
TCHAR * tstr = malloc(MAXSTR * sizeof(TCHAR));
ZeroMemory(tstr, MAXSTR * sizeof(TCHAR));
if (tstr != NULL)
{
_tcsset_s(tstr, MAXSTR, _T('\0'));
for (; i < MAXSTR; i++)
{
tstr[i] = _totlower(Str[i]);
}
}
return tstr;
}
int SelectFromTable(TCHAR* Table, FIELDS fields[], TCHAR* Where);
int execute_sentence(TCHAR* psentence)
{
TCHAR * sentence = StrToLower(psentence);
StrTrim(sentence, _T(" "));
TCHAR* dlimiter = _T("?");
if (_tcsncmp(_T("select"), sentence, _tcslen(_T("select"))) == 0)
{
TCHAR ** split = SplitStr(sentence, _T("where"));
TCHAR* select = split[0];
StrTrim(select, _T(" "));
TCHAR* where = split[1];
StrTrim(where, _T(" "));
//_putts(where); // here value is OK
TCHAR ** split2 = SplitStr(select, _T("from"));
TCHAR * fields = split2[0] + _tcslen(_T("select"));
TCHAR * table = split2[1];
StrTrim(table, _T(" "));
_putts(where); // here value is distorted now if I don't use _tcsdup above
TCHAR ** split3 = SplitStr(fields, dlimiter);
TCHAR* thefield = split3[0];
StrTrim(thefield, _T(" "));
FIELDS fields_arr[Field_Count];
int c_field_pos = 0, i = 1;
while (_tcscmp(thefield, _T("END")) != 0)
{
StrTrim(thefield, _T(" "));
c_field_pos = 0;
if (thefield != NULL)
{
for (int i = 0; i < Field_Count; i++)
{
if (_tcsncmp(thefield, table_columns[i], _tcslen(table_columns[i])) == 0) {
fields_arr[c_field_pos] = (FIELDS)i;
c_field_pos++;
break;
}
}
}
thefield = split3[i];
i++;
}
fields_arr[c_field_pos] = F_END;
SelectFromTable(table, fields_arr, where);
}
return 0;
}
I never expected the need to duplicate, I even used a pointer and kept it intact, still problem exists..