HistoryAPI.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "HistoryAPI.h"
  2. #include "ml_history.h"
  3. static void saveQueryToList(nde_scanner_t s, historyRecordList *obj)
  4. {
  5. emptyRecentRecordList(obj);
  6. NDE_Scanner_First(s);
  7. int r;
  8. do
  9. {
  10. nde_field_t f = NDE_Scanner_GetFieldByID(s, HISTORYVIEW_COL_FILENAME);
  11. if (f)
  12. {
  13. allocRecentRecordList(obj, obj->Size + 1);
  14. if (!obj->Alloc) break;
  15. wchar_t *strval = NDE_StringField_GetString(f);
  16. ndestring_retain(strval);
  17. obj->Items[obj->Size].filename = strval;
  18. recentScannerRefToObjCacheNFN(s, obj);
  19. }
  20. r = NDE_Scanner_Next(s);
  21. }
  22. while (r && !NDE_Scanner_EOF(s));
  23. if (obj->Size && obj->Size < obj->Alloc - 1024)
  24. {
  25. size_t old_Alloc = obj->Alloc;
  26. obj->Alloc = obj->Size;
  27. historyRecord *data = (historyRecord*)realloc(obj->Items, sizeof(historyRecord) * obj->Alloc);
  28. if (data)
  29. {
  30. obj->Items=data;
  31. }
  32. else
  33. {
  34. data=(historyRecord*)malloc(sizeof(historyRecord)*obj->Alloc);
  35. if (data)
  36. {
  37. memcpy(data, obj->Items, sizeof(historyRecord)*old_Alloc);
  38. free(obj->Items);
  39. obj->Items=data;
  40. }
  41. else obj->Alloc = (int)old_Alloc;
  42. }
  43. }
  44. }
  45. historyRecordList *HistoryAPI::Query(const wchar_t *query)
  46. {
  47. if (!openDb())
  48. return 0;
  49. // run query
  50. EnterCriticalSection(&g_db_cs);
  51. nde_scanner_t s=NDE_Table_CreateScanner(g_table);
  52. NDE_Scanner_Query(s, query);
  53. historyRecordList obj;
  54. obj.Alloc = 0;
  55. obj.Items = NULL;
  56. obj.Size = 0;
  57. saveQueryToList(s, &obj);
  58. NDE_Table_DestroyScanner(g_table, s);
  59. LeaveCriticalSection(&g_db_cs);
  60. if (obj.Size)
  61. {
  62. historyRecordList *result = (historyRecordList *)malloc(sizeof(historyRecordList));
  63. memcpy(result, &obj, sizeof(historyRecordList));
  64. return result;
  65. }
  66. else
  67. {
  68. freeRecentRecordList(&obj);
  69. return 0;
  70. }
  71. }
  72. void HistoryAPI::FreeHistoryList(historyRecordList *historyList)
  73. {
  74. freeRecentRecordList(historyList);
  75. }
  76. #define CBCLASS HistoryAPI
  77. START_DISPATCH;
  78. CB(API_HISTORY_QUERY, Query)
  79. VCB(API_HISTORY_FREEHISTORYLIST, FreeHistoryList)
  80. END_DISPATCH;
  81. #undef CBCLASS