ulong CCmdLog::set(ulong id, int code, int type, ulong flag) { LPLOGGER logger = nullptr; if (id <= 0) return 0; EnterCriticalSection(&m_csShare); // Initialize the sorted data if it hasn't initialize yet .. if (Issortedinit(&m_Table.sorted) == 0) { if (Createsorteddata ( // Make sure it's already initialized. &m_Table.sorted, // Descriptor of sorted data sizeof(LOGGER), // Size of single data item 10, // Initial number of allocated items (SORTFUNC *)CCmdLog::SortProc, // Sorting function (DESTFUNC *)CCmdLog::DestProc, // Data destructor 0 ) != 0) { LeaveCriticalSection(&m_csShare); return 0; } } // Get the logger .. void *temp = Findsorteddata(&m_Table.sorted, id, 0); logger = reinterpret_cast<LPLOGGER>(temp); if (logger != nullptr) { logger->code = code; id = logger->addr; if (type) logger->type = type; if (flag) logger->flag = flag; } else id = 0; LeaveCriticalSection(&m_csShare); // Now redraw the window and return ... Updatetable(&m_Table, false); return id; }
ulong CCmdLog::clear(void) { m_Table.offset = m_Table.sorted.selected = m_Table.xshift = 0; EnterCriticalSection(&m_csShare); ulong begin = -1, count = 0, end; LPLOGGER plog; m_nNextId = 0; for (int pos = 0; pos < m_Table.sorted.n; ++pos) { plog = reinterpret_cast<LPLOGGER> ( Getsortedbyindex(&m_Table.sorted, pos) ); // logger->addr = pos; if (plog->flag & LOGGER_FLAG_IDLE) { m_Table.custommode -= plog->rows; ++ count; } if (begin == -1) { if (plog->flag & LOGGER_FLAG_IDLE) { begin = plog->addr; } } else { if (plog->flag & LOGGER_FLAG_BUSY) { end = plog->addr; pos -= count + 1; Addtolist(1,1,text("begin: %x, end: %x"),begin,end); Deletesorteddatarange(&m_Table.sorted, begin, end); if (!m_nNextId) m_nNextId = begin; begin = -1; } } } if (begin != -1) { if (!m_nNextId) m_nNextId = begin; end = plog->addr + 1; Deletesorteddatarange(&m_Table.sorted, begin, end); } LeaveCriticalSection(&m_csShare); Updatetable(&m_Table, false); return count; }
ulong CCmdLog::erase(ulong id) { EnterCriticalSection(&m_csShare); void *temp = Findsorteddata(&m_Table.sorted, id, 0); LPLOGGER plog = reinterpret_cast<LPLOGGER>(temp); if (plog != nullptr) { Deletesorteddata(&m_Table.sorted, plog->addr, 0); Updatetable(&m_Table, false); } else id = 0; LeaveCriticalSection(&m_csShare); return id ; }
ulong CCmdLog::msg(ulong id, const std::wstring& msg, bool append) { std::wstring *pmsg; LPLOGGER plog; if (id <= 0) return 0; EnterCriticalSection(&m_csShare); // Initialize the sorted data if it hasn't initialize yet .. if (Issortedinit(&m_Table.sorted) == 0) { if (Createsorteddata ( // Make sure it's already initialized. &m_Table.sorted, // Descriptor of sorted data sizeof(LOGGER), // Size of single data item 10, // Initial number of allocated items (SORTFUNC *)CCmdLog::SortProc, // Sorting function (DESTFUNC *)CCmdLog::DestProc, // Data destructor 0 ) != 0) { LeaveCriticalSection(&m_csShare); return 0; } } // Get the logger .. void *temp = Findsorteddata(&m_Table.sorted, id, 0); if ((plog = reinterpret_cast<LPLOGGER>(temp)) != nullptr) { m_Table.custommode -= plog->rows; pmsg = reinterpret_cast<std::wstring*>(plog->msge); if (pmsg == nullptr) pmsg = new std::wstring(); if (append == false) { pmsg->clear(); plog->rows = 0; } else if (pmsg->back() != text('\n')) --plog->rows; // Extract the message ... std::wstring::const_iterator itr, eitr = msg.cend(); for (itr = msg.cbegin(); itr != eitr; ++itr) { // We don't support old mac format ... if (*itr == text('\t')) { pmsg->append(8, text(' ')); continue; } if (*itr == text('\r')) continue; if (*itr == text('\n')) ++ plog->rows; pmsg->push_back(*itr); } if (pmsg->back() != text('\n')) ++ plog->rows; plog->msge = pmsg; // Set the string ... m_Table.custommode += plog->rows; } else id = 0; // return 0 if the logger doesn't exist. LeaveCriticalSection(&m_csShare); // Redrawing the logger window and return .. if (m_Table.offset <= 0) Updatetable(&m_Table, false); else PostMessage(m_Table.hw,WM_VSCROLL,SB_PAGEDOWN,0); return id; }
ulong CCmdLog::log(ulong id, int code, int type, const wchar *msg) { std::wstring *pmsg = nullptr; void *temp = nullptr; LPLOGGER plog = nullptr; ulong ulNewId = 0; EnterCriticalSection(&m_csShare); if (Issortedinit(&m_Table.sorted) == 0) { if (Createsorteddata ( // Initialize the sorted data &m_Table.sorted, // Descriptor of sorted data sizeof(LOGGER), // Size of single data item 10, // Initial number of allocated items (SORTFUNC *)CCmdLog::SortProc, // Sorting function (DESTFUNC *)CCmdLog::DestProc, // Data destructor 0 ) != 0) { LeaveCriticalSection(&m_csShare); return 0; } } if (id <= 0) { // Add new record .. // Can't add logger any more ... if (m_Table.custommode < MAXINT) { // Find a new id .. while (Findsorteddata(&m_Table.sorted,m_nNextId,0)) ++ m_nNextId; // Initialize a new item ... time_t timer; time(&timer); LOGGER logger = { m_nNextId, 1, type, 0, LOGGER_FLAG_IDLE, timer, code, nullptr }; // Initialize a new string.. (it's suck without c++11) std::wstring *pmsg = new std::wstring(); for (size_t npos = 0; msg[npos]; ++npos) { // We don't support old mac format ... if (msg[npos] == text('\r')) continue; if (msg[npos] == text('\n')) ++logger.rows; pmsg->push_back(msg[npos]); } if (pmsg->back() != text('\n')) { pmsg->push_back(text('\n')); ++logger.rows; } logger.msge = pmsg; // Set the string .... // Add a new item and return it's id .. temp = Addsorteddata(&m_Table.sorted, &logger); plog = reinterpret_cast<LPLOGGER>(temp); if (plog != nullptr) ulNewId = plog->addr; else { ulNewId = 0; delete logger.msge; } // Update the line count .. if (plog) m_Table.custommode += plog->rows; } } else { // Modify existed record . temp = Findsorteddata(&m_Table.sorted, id, 0); if ((plog = reinterpret_cast<LPLOGGER>(temp)) != nullptr) { m_Table.custommode -= plog->rows; // Update the logger .. plog->code = code; plog->type = type; plog->rows = 0; pmsg = reinterpret_cast<std::wstring*>(plog->msge); if (pmsg == nullptr) pmsg = new std::wstring(); else pmsg->clear(); // Clear the message first.. for (size_t npos = 0; msg[npos]; ++npos) { // We don't support old mac format ... if (msg[npos] == text('\r')) continue; if (msg[npos] == text('\n')) ++plog->rows; pmsg->push_back(msg[npos]); } if (pmsg->back() != text('\n')) { pmsg->push_back(text('\n')); ++plog->rows; } plog->msge = pmsg; // Set the string ........ m_Table.custommode += plog->rows; // Return this id . ulNewId = plog->addr; } } LeaveCriticalSection(&m_csShare); // Redrawing the logger window and return .. if (m_Table.offset <= 0) Updatetable(&m_Table, false); else PostMessage(m_Table.hw,WM_VSCROLL,SB_PAGEDOWN,0); return ulNewId; }
int main(void) { //#ifdef _DEBUG //_CrtSetDbgFlag(_crtDbgFlag|_CRTDBG_LEAK_CHECK_DF); //char* pDumMemoryLeak = new char[20]; //::mystrcpy(pDumMemoryLeak,"Dum Leak"); //#endif int i=1; char buffer[LINE_MAX]; char* command=buffer; // check command char tableindex[TABLE_MAX][TABLE_NAME_MAX]={0}; //Index of Table Data*** database = (Data***)malloc(sizeof(Data**) * TABLE_MAX); //Data if(database == NULL) {printf("error in creating Table\n"); exit(1);} int j1=0; for(j1=0;j1<TABLE_MAX;j1++) database[j1] = NULL; //Initialize every table to NULL Column** columnindex= (Column**) malloc( sizeof(Column*) * TABLE_MAX) ; //Index of Column if(database == NULL) {printf("error in creating Column\n"); exit(1);} int j2=0; for(j2=0;j2<TABLE_MAX;j2++) columnindex[j2] = NULL; //Initialize every column to NULL printscreen(); //print screen while(i) { printf("DB1=>"); gets(buffer); command=buffer; while(isspace(*command)){command++;}//ignore ' ' if(*(command)++!='-') { printf("command should be started by - \n"); continue; } if(*command == 'c') //create database { command++; while(isspace((*command))){command++;}//ignore ' ' if(createtable(command,tableindex,columnindex,database) == 0) printf("Error in Initializing the table and create it!\n"); else printf("Succeed in Initializing the table!\n"); continue; } if(*command=='i') // Import DB { command++; while(isspace((*command))){command++;}//ignore ' ' int i=0; char tablename[TABLE_NAME_MAX]; char filename[20]; while(*command!=' ') { tablename[i++]=*(command++); } tablename[i]='\0'; while(isspace((*command))){command++;}//ignore ' ' i=0; while(*command!='\0') { filename[i++]=*(command++); } filename[i]='\0'; printf("%d items has been imported\n", ImportTable(tablename,filename,tableindex,columnindex,database)); continue; } if(*command=='s') { command++; while(isspace((*command))){command++;}///ignore ' ' printf("------%d Items has been searched !--------------\n",tablesearch(command,tableindex,columnindex,database) ); continue; } if(*command == 'u') //ignore ' ' { command++; while(isspace((*command))){command++;}///ignore ' ' printf("-------%d Items has been updated!---------------\n",Updatetable(tableindex,columnindex,database,command)); printf("The result is printed to the File 074090_update.txt \n"); continue; } if(*command == 'd') //delete DB { command++; while(isspace((*command))){command++;}///ignore ' ' printf("-------%d Items has been Deleted!---------------\n",Deletetable(command,tableindex,columnindex,database) ); printf("The result is printed to the File 074090_delete \n"); continue; } if(*command == 'h') //help { help(); continue; } if(*command == 'q') //Quit free Memrory { quit(database,columnindex,tableindex); break; } else { puts("Commands can't ben supported!\n"); } } printf("\nThank you for using!"); return 0; }