int dbAddRow(int did, char_t *tablename) { int tid, size; dbTable_t *pTable; a_assert(tablename); tid = dbGetTableId(0, tablename); a_assert(tid >= 0); if ((tid >= 0) && (tid < dbMaxTables) && (dbListTables[tid] != NULL)) { pTable = dbListTables[tid]; } else { return DB_ERR_TABLE_NOT_FOUND; } a_assert(pTable); if (pTable) { trace(5, T("DB: Adding a row to table <%s>\n"), tablename); size = pTable->nColumns * max(sizeof(int), sizeof(char_t *)); return hAllocEntry((void***) &(pTable->rows), &(pTable->nRows), size); } return -1; }
int dbDeleteRow(int did, char_t *tablename, int row) { int tid, nColumns, nRows; dbTable_t *pTable; a_assert(tablename); tid = dbGetTableId(0, tablename); a_assert(tid >= 0); if ((tid >= 0) && (tid < dbMaxTables) && (dbListTables[tid] != NULL)) { pTable = dbListTables[tid]; } else { return DB_ERR_TABLE_NOT_FOUND; } nColumns = pTable->nColumns; nRows = pTable->nRows; if ((row >= 0) && (row < nRows)) { int *pRow = pTable->rows[row]; if (pRow) { int column = 0; /* * Free up any allocated strings */ while (column < nColumns) { if (pRow[column] && (pTable->columnTypes[column] == T_STRING)) { bfree(B_L, (char_t *)pRow[column]); } column++; } /* * Zero out the row for safety */ memset(pRow, 0, nColumns * max(sizeof(int), sizeof(char_t *))); bfreeSafe(B_L, pRow); pTable->nRows = hFree((void ***)&pTable->rows, row); trace(5, T("DB: Deleted row <%d> from table <%s>\n"), row, tablename); } return 0; } else { trace(3, T("DB: Unable to delete row <%d> from table <%s>\n"), row, tablename); } return -1; }
int dbGetTableNrow(int did, char_t *tablename) { int tid; a_assert(tablename); tid = dbGetTableId(did, tablename); if ((tid >= 0) && (tid < dbMaxTables) && (dbListTables[tid] != NULL)) { return (dbListTables[tid])->nRows; } else { return -1; } }
int dbWriteInt(int did, char_t *table, char_t *column, int row, int iData) { int tid, colIndex, *pRow; dbTable_t *pTable; a_assert(table); a_assert(column); /* * Make sure that this table exists */ tid = dbGetTableId(0, table); a_assert(tid >= 0); if (tid < 0) { return DB_ERR_TABLE_NOT_FOUND; } pTable = dbListTables[tid]; if (pTable) { /* * Make sure that the column exists */ colIndex = GetColumnIndex(tid, column); a_assert(colIndex >= 0); if (colIndex >= 0) { /* * Make sure that the row exists */ a_assert((row >= 0) && (row < pTable->nRows)); if ((row >= 0) && (row < pTable->nRows)) { pRow = pTable->rows[row]; if (pRow) { pRow[colIndex] = iData; return 0; } return DB_ERR_ROW_DELETED; } return DB_ERR_ROW_NOT_FOUND; } return DB_ERR_COL_NOT_FOUND; } return DB_ERR_TABLE_DELETED; }
int dbReadInt(int did, char_t *table, char_t *column, int row, int *returnValue) { int colIndex, *pRow, tid; dbTable_t *pTable; a_assert(table); a_assert(column); a_assert(returnValue); tid = dbGetTableId(0, table); a_assert(tid >= 0); /* * Return -6 if table is not found */ if (tid < 0) { return DB_ERR_TABLE_NOT_FOUND; } /* * Return -7 if table id has been deleted */ pTable = dbListTables[tid]; if (pTable == NULL) { return DB_ERR_TABLE_DELETED; } a_assert(row >= 0); if ((row >= 0) && (row < pTable->nRows)) { colIndex = GetColumnIndex(tid, column); a_assert(colIndex >= 0); if (colIndex >= 0) { pRow = pTable->rows[row]; if (pRow) { *returnValue = pRow[colIndex]; return 0; } return DB_ERR_ROW_DELETED; } return DB_ERR_COL_NOT_FOUND; } return DB_ERR_ROW_NOT_FOUND; }
int dbSetTableNrow(int did, char_t *tablename, int nNewRows) { int nRet, tid, nRows, nColumns; dbTable_t *pTable; a_assert(tablename); tid = dbGetTableId(0, tablename); a_assert(tid >= 0) ; if ((tid >= 0) && (tid < dbMaxTables) && (dbListTables[tid] != NULL)) { pTable = dbListTables[tid]; } else { return DB_ERR_TABLE_NOT_FOUND; } nRet = -1; a_assert(pTable); if (pTable) { nColumns = pTable->nColumns; nRows = pTable->nRows; nRet = 0; if (nRows >= nNewRows) { /* * If number of rows already allocated exceeds requested number, do nothing */ trace(4, T("DB: Ignoring row set to <%d> in table <%s>\n"), nNewRows, tablename); } else { trace(4, T("DB: Setting rows to <%d> in table <%s>\n"), nNewRows, tablename); while (pTable->nRows < nNewRows) { if (dbAddRow(did, tablename) < 0) { return -1; } } } } return nRet; }
int dbLoad(int did, char_t *filename, int flags) { gstat_t sbuf; char_t *buf, *keyword, *value, *path, *ptr; char_t *tablename; int fd, tid, row; dbTable_t *pTable; a_assert(did >= 0); fmtAlloc(&path, FNAMESIZE, T("%s/%s"), basicGetProductDir(), filename); trace(4, T("DB: About to read data file <%s>\n"), path); if (gstat(path, &sbuf) < 0) { trace(3, T("DB: Failed to stat persistent data file.\n")); bfree(B_L, path); return -1; } fd = gopen(path, O_RDONLY | O_BINARY, 0666); bfree(B_L, path); if (fd < 0) { trace(3, T("DB: No persistent data file present.\n")); return -1; } if (sbuf.st_size <= 0) { trace(3, T("DB: Persistent data file is empty.\n")); gclose(fd); return -1; } /* * Read entire file into temporary buffer */ buf = balloc(B_L, sbuf.st_size + 1); #ifdef CE if (readAscToUni(fd, &buf, sbuf.st_size) != (int)sbuf.st_size) { #else if (gread(fd, buf, sbuf.st_size) != (int)sbuf.st_size) { #endif trace(3, T("DB: Persistent data read failed.\n")); bfree(B_L, buf); gclose(fd); return -1; } gclose(fd); *(buf + sbuf.st_size) = '\0'; row = -1; tid = -1; pTable = NULL; ptr = gstrtok(buf, T("\n")); tablename = NULL; do { if (crack(ptr, &keyword, &value) < 0) { trace(5, T("DB: Failed to crack line %s\n"), ptr); continue; } a_assert(keyword && *keyword); if (gstrcmp(keyword, KEYWORD_TABLE) == 0) { /* * Table name found, check to see if it's registered */ if (tablename) { bfree(B_L, tablename); } tablename = bstrdup(B_L, value); tid = dbGetTableId(did, tablename); if (tid >= 0) { pTable = dbListTables[tid]; } else { pTable = NULL; } } else if (gstrcmp(keyword, KEYWORD_ROW) == 0) { /* * Row/Record indicator found, add a new row to table */ if (tid >= 0) { int nRows = dbGetTableNrow(did, tablename); if (dbSetTableNrow(did, tablename, nRows + 1) == 0) { row = nRows; } } } else if (row != -1) { /* * some other data found, assume it's a COLUMN=value */ int nColumn = GetColumnIndex(tid, keyword); if ((nColumn >= 0) && (pTable != NULL)) { int nColumnType = pTable->columnTypes[nColumn]; if (nColumnType == T_STRING) { dbWriteStr(did, tablename, keyword, row, value); } else { dbWriteInt(did, tablename, keyword, row, gstrtoi(value)); } } } } while ((ptr = gstrtok(NULL, T("\n"))) != NULL); if (tablename) { bfree(B_L, tablename); } bfree(B_L, buf); return 0; } /******************************************************************************/ /* * Return a table id given the table name */ int dbGetTableId(int did, char_t *tablename) { int tid; dbTable_t *pTable; a_assert(tablename); for (tid = 0; (tid < dbMaxTables); tid++) { if ((pTable = dbListTables[tid]) != NULL) { if (gstrcmp(tablename, pTable->name) == 0) { return tid; } } } return -1; }
int dbWriteStr(int did, char_t *table, char_t *column, int row, char_t *s) { int tid, colIndex; int *pRow; char_t *ptr; dbTable_t *pTable; a_assert(table); a_assert(column); tid = dbGetTableId(0, table); a_assert(tid >= 0); if (tid < 0) { return DB_ERR_TABLE_NOT_FOUND; } /* * Make sure that this table exists */ pTable = dbListTables[tid]; a_assert(pTable); if (!pTable) { return DB_ERR_TABLE_DELETED; } /* * Make sure that this column exists */ colIndex = GetColumnIndex(tid, column); if (colIndex < 0) { return DB_ERR_COL_NOT_FOUND; } /* * Make sure that this column is a string column */ if (pTable->columnTypes[colIndex] != T_STRING) { return DB_ERR_BAD_FORMAT; } /* * Make sure that the row exists */ a_assert((row >= 0) && (row < pTable->nRows)); if ((row >= 0) && (row < pTable->nRows)) { pRow = pTable->rows[row]; } else { return DB_ERR_ROW_NOT_FOUND; } if (!pRow) { return DB_ERR_ROW_DELETED; } /* * If the column already has a value, be sure to delete it to prevent * memory leaks. */ if (pRow[colIndex]) { bfree(B_L, (char_t *) pRow[colIndex]); } /* * Make sure we make a copy of the string to write into the column. * This allocated string will be deleted when the row is deleted. */ ptr = bstrdup(B_L, s); pRow[colIndex] = (int)ptr; return 0; }
int dbSearchStr(int did, char_t *tablename, char_t *colName, char_t *value, int flags) { int tid, nRows, nColumns, column; dbTable_t *pTable; a_assert(tablename); a_assert(colName); a_assert(value); tid = dbGetTableId(0, tablename); a_assert(tid >= 0); if ((tid >= 0) && (tid < dbMaxTables) && (dbListTables[tid] != NULL)) { pTable = dbListTables[tid]; } else { return DB_ERR_TABLE_NOT_FOUND; } nColumns = pTable->nColumns; nRows = pTable->nRows; column = GetColumnIndex(tid, colName); a_assert (column >= 0); if (column >= 0) { char_t *compareVal; int row, *pRow; /* * Scan through rows until we find a match. * Note that some of these rows may be deleted! */ row = 0; while (row < nRows) { pRow = pTable->rows[row]; if (pRow) { compareVal = (char_t *)(pRow[column]); if (compareVal && (gstrcmp(compareVal, value) == 0)) { return row; } /* Add by Dick Tam */ /* In order to protect everything inside a directory */ if (compareVal) { if(gstrncmp(compareVal, value,gstrlen(compareVal)) == 0) { return row; } } /* Add by Dick Tam End */ } row++; } } else { /* * Return -2 if search column was not found */ trace(3, T("DB: Unable to find column <%s> in table <%s>\n"), colName, tablename); return DB_ERR_COL_NOT_FOUND; } return -1; }
int dbSearchStr(int did, char_t *tablename, char_t *colName, char_t *value, int flags) { int tid, nRows, nColumns, column; int match = 0; dbTable_t *pTable; a_assert(tablename); a_assert(colName); a_assert(value); tid = dbGetTableId(0, tablename); a_assert(tid >= 0); if ((tid >= 0) && (tid < dbMaxTables) && (dbListTables[tid] != NULL)) { pTable = dbListTables[tid]; } else { return DB_ERR_TABLE_NOT_FOUND; } nColumns = pTable->nColumns; nRows = pTable->nRows; column = GetColumnIndex(tid, colName); a_assert (column >= 0); if (column >= 0) { char_t *compareVal; int row, *pRow; /* * Scan through rows until we find a match. * Note that some of these rows may be deleted! */ row = 0; while (row < nRows) { pRow = pTable->rows[row]; if (pRow) { compareVal = (char_t *)(pRow[column]); if (NULL != compareVal) { if (DB_CASE_INSENSITIVE == flags) { match = gstricmp(compareVal, value); } else { match = gstrcmp(compareVal, value); } if (0 == match) { return row; } } } row++; } } else { /* * Return -2 if search column was not found */ trace(3, T("DB: Unable to find column <%s> in table <%s>\n"), colName, tablename); return DB_ERR_COL_NOT_FOUND; } return -1; }