1
0

nde_c.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. #include "nde_c.h"
  2. #include "nde.h"
  3. #ifdef _WIN32
  4. #include "../nu/AutoCharFn.h"
  5. #include "../nu/AutoWide.h"
  6. #endif
  7. /* Database */
  8. #ifdef _WIN32
  9. nde_database_t NDE_CreateDatabase(HINSTANCE hInstance)
  10. {
  11. return (nde_database_t)new Database(hInstance);
  12. }
  13. #else
  14. nde_database_t NDE_CreateDatabase()
  15. {
  16. return (nde_database_t)new Database();
  17. }
  18. #endif
  19. void NDE_DestroyDatabase(nde_database_t db)
  20. {
  21. delete (Database *)db;
  22. }
  23. #ifdef _WIN32
  24. nde_table_t NDE_Database_OpenTable(nde_database_t db, const wchar_t *filename, const wchar_t *indexname, int create, int cache)
  25. {
  26. Database *database = (Database *)db;
  27. if (database && filename)
  28. return (nde_table_t)database->OpenTable(filename, indexname, (BOOL)create, (BOOL)cache);
  29. else
  30. return 0;
  31. }
  32. #else
  33. nde_table_t NDE_Database_OpenTable(nde_database_t db, const char *filename, const char *indexname, int create, int cache)
  34. {
  35. Database *database = (Database *)db;
  36. if (database && filename)
  37. return (nde_table_t)database->OpenTable(filename, indexname, (BOOL)create, (BOOL)cache);
  38. else
  39. return 0;
  40. }
  41. #endif
  42. void NDE_Database_CloseTable(nde_database_t db, nde_table_t t)
  43. {
  44. Database *database = (Database *)db;
  45. Table *table = (Table *)t;
  46. if (database && table)
  47. {
  48. database->CloseTable(table);
  49. }
  50. }
  51. /* Table */
  52. #ifdef _WIN32
  53. nde_field_t NDE_Table_NewColumn(nde_table_t t, unsigned char id, const char *name, unsigned char type)
  54. {
  55. Table *table = (Table *)t;
  56. if (table)
  57. return (nde_field_t)table->NewColumn(id, AutoWide(name), type, FALSE);
  58. else
  59. return 0;
  60. }
  61. #else
  62. nde_field_t NDE_Table_NewColumn(nde_table_t t, unsigned char id, CFStringRef name, unsigned char type)
  63. {
  64. Table *table = (Table *)t;
  65. if (table)
  66. return (nde_field_t)table->NewColumn(id, name, type, FALSE);
  67. else
  68. return 0;
  69. }
  70. #endif
  71. void NDE_Table_PostColumns(nde_table_t t)
  72. {
  73. Table *table = (Table *)t;
  74. if (table)
  75. table->PostColumns();
  76. }
  77. #ifdef _WIN32
  78. void NDE_Table_AddIndexByID(nde_table_t t, unsigned char id, const char *name)
  79. {
  80. Table *table = (Table *)t;
  81. if (table)
  82. table->AddIndexById(id, AutoWide(name));
  83. }
  84. #else
  85. void NDE_Table_AddIndexByID(nde_table_t t, unsigned char id, CFStringRef name)
  86. {
  87. Table *table = (Table *)t;
  88. if (table)
  89. table->AddIndexById(id, name);
  90. }
  91. #endif
  92. nde_scanner_t NDE_Table_CreateScanner(nde_table_t t)
  93. {
  94. Table *table = (Table *)t;
  95. if (table)
  96. return (nde_scanner_t)table->NewScanner();
  97. else
  98. return 0;
  99. }
  100. void NDE_Table_DestroyScanner(nde_table_t t, nde_scanner_t s)
  101. {
  102. Table *table = (Table *)t;
  103. Scanner *scanner = (Scanner *)s;
  104. if (table && scanner)
  105. table->DeleteScanner(scanner);
  106. }
  107. void NDE_Table_Sync(nde_table_t t)
  108. {
  109. Table *table = (Table *)t;
  110. if (table)
  111. table->Sync();
  112. }
  113. #ifdef _WIN32
  114. void NDE_Table_Compact(nde_table_t t, int *progress)
  115. {
  116. Table *table = (Table *)t;
  117. if (table)
  118. table->Compact(progress);
  119. }
  120. #endif
  121. int NDE_Table_GetRecordsCount(nde_table_t t)
  122. {
  123. Table *table = (Table *)t;
  124. if (table)
  125. return table->GetRecordsCount();
  126. else
  127. return 0;
  128. }
  129. nde_field_t NDE_Table_GetColumnByID(nde_table_t t, unsigned char id)
  130. {
  131. Table *table = (Table *)t;
  132. if (table)
  133. return (nde_field_t)table->GetColumnById(id);
  134. else
  135. return 0;
  136. }
  137. #ifdef _WIN32
  138. nde_field_t NDE_Table_GetColumnByName(nde_table_t t, const char *name)
  139. {
  140. Table *table = (Table *)t;
  141. if (table && name)
  142. return (nde_field_t)table->GetColumnByName(AutoWide(name));
  143. else
  144. return 0;
  145. }
  146. #else
  147. nde_field_t NDE_Table_GetColumnByName(nde_table_t t, CFStringRef name)
  148. {
  149. Table *table = (Table *)t;
  150. if (table && name)
  151. return (nde_field_t)table->GetColumnByName(name);
  152. else
  153. return 0;
  154. }
  155. #endif
  156. void NDE_Table_SetColumnSearchableByID(nde_table_t t, unsigned char id, int searchable)
  157. {
  158. Table *table = (Table *)t;
  159. if (table)
  160. table->SetFieldSearchableById(id, !!searchable);
  161. }
  162. /* Scanner */
  163. #ifdef _WIN32
  164. int NDE_Scanner_Query(nde_scanner_t s, const wchar_t *query)
  165. {
  166. Scanner *scanner = (Scanner *)s;
  167. if (scanner)
  168. return scanner->Query(query);
  169. else
  170. return 0;
  171. }
  172. void NDE_Scanner_Search(nde_scanner_t s, const wchar_t *search_term)
  173. {
  174. Scanner *scanner = (Scanner *)s;
  175. if (scanner)
  176. scanner->Search(search_term);
  177. }
  178. const wchar_t *NDE_Scanner_GetLastQuery(nde_scanner_t s)
  179. {
  180. Scanner *scanner = (Scanner *)s;
  181. if (scanner)
  182. return scanner->GetLastQuery();
  183. else
  184. return 0;
  185. }
  186. #elif defined(__APPLE__)
  187. int NDE_Scanner_Query(nde_scanner_t s, const char *query)
  188. {
  189. Scanner *scanner = (Scanner *)s;
  190. if (scanner)
  191. return scanner->Query(query);
  192. else
  193. return 0;
  194. }
  195. CFStringRef NDE_Scanner_GetLastQuery(nde_scanner_t s)
  196. {
  197. Scanner *scanner = (Scanner *)s;
  198. if (scanner)
  199. return scanner->GetLastQuery();
  200. else
  201. return 0;
  202. }
  203. #endif
  204. int NDE_Scanner_GetRecordsCount(nde_scanner_t s)
  205. {
  206. Scanner *scanner = (Scanner *)s;
  207. if (scanner)
  208. return scanner->GetRecordsCount();
  209. else
  210. return 0;
  211. }
  212. void NDE_Scanner_New(nde_scanner_t s)
  213. {
  214. Scanner *scanner = (Scanner *)s;
  215. if (scanner)
  216. scanner->New();
  217. }
  218. void NDE_Scanner_Post(nde_scanner_t s)
  219. {
  220. Scanner *scanner = (Scanner *)s;
  221. if (scanner)
  222. scanner->Post();
  223. }
  224. void NDE_Scanner_First(nde_scanner_t s, int *killswitch)
  225. {
  226. Scanner *scanner = (Scanner *)s;
  227. if (scanner)
  228. scanner->First(killswitch);
  229. }
  230. void NDE_Scanner_Delete(nde_scanner_t s)
  231. {
  232. Scanner *scanner = (Scanner *)s;
  233. if (scanner)
  234. scanner->Delete();
  235. }
  236. void NDE_Scanner_Edit(nde_scanner_t s)
  237. {
  238. Scanner *scanner = (Scanner *)s;
  239. if (scanner)
  240. scanner->Edit();
  241. }
  242. int NDE_Scanner_EOF(nde_scanner_t s)
  243. {
  244. Scanner *scanner = (Scanner *)s;
  245. if (scanner)
  246. return scanner->Eof();
  247. else
  248. return 1;
  249. }
  250. int NDE_Scanner_BOF(nde_scanner_t s)
  251. {
  252. Scanner *scanner = (Scanner *)s;
  253. if (scanner)
  254. return scanner->Bof();
  255. else
  256. return 1;
  257. }
  258. nde_field_t NDE_Scanner_NewFieldByID(nde_scanner_t s, unsigned char id)
  259. {
  260. Scanner *scanner = (Scanner *)s;
  261. if (scanner)
  262. return (nde_field_t)scanner->NewFieldById(id, PERM_READWRITE);
  263. else
  264. return 0;
  265. }
  266. #ifdef _WIN32
  267. nde_field_t NDE_Scanner_NewFieldByName(nde_scanner_t s, const char *name)
  268. {
  269. Scanner *scanner = (Scanner *)s;
  270. if (scanner)
  271. return (nde_field_t)scanner->NewFieldByName(AutoWide(name), PERM_READWRITE);
  272. else
  273. return 0;
  274. }
  275. #else
  276. nde_field_t NDE_Scanner_NewFieldByName(nde_scanner_t s, CFStringRef name)
  277. {
  278. Scanner *scanner = (Scanner *)s;
  279. if (scanner)
  280. return (nde_field_t)scanner->NewFieldByName(name, PERM_READWRITE);
  281. else
  282. return 0;
  283. }
  284. #endif
  285. nde_field_t NDE_Scanner_GetFieldByID(nde_scanner_t s, unsigned char id)
  286. {
  287. Scanner *scanner = (Scanner *)s;
  288. if (scanner)
  289. return (nde_field_t)scanner->GetFieldById(id);
  290. else
  291. return 0;
  292. }
  293. #ifdef _WIN32
  294. nde_field_t NDE_Scanner_GetFieldByName(nde_scanner_t s, const char *name)
  295. {
  296. Scanner *scanner = (Scanner *)s;
  297. if (scanner && name)
  298. return (nde_field_t)scanner->GetFieldByName(AutoWide(name));
  299. else
  300. return 0;
  301. }
  302. #else
  303. nde_field_t NDE_Scanner_GetFieldByName(nde_scanner_t s, CFStringRef name)
  304. {
  305. Scanner *scanner = (Scanner *)s;
  306. if (scanner && name)
  307. return (nde_field_t)scanner->GetFieldByName(name);
  308. else
  309. return 0;
  310. }
  311. #endif
  312. void NDE_Scanner_AddFilterByID(nde_scanner_t s, unsigned char id, nde_field_t f, unsigned char filter_operation)
  313. {
  314. Scanner *scanner = (Scanner *)s;
  315. Field *field = (Field *)f;
  316. if (scanner && field)
  317. scanner->AddFilterById(id, field, filter_operation);
  318. }
  319. int NDE_Scanner_LocateInteger(nde_scanner_t s, unsigned char id, int from, int value, int *nskip, int compare_mode)
  320. {
  321. Scanner *scanner = (Scanner *)s;
  322. if (scanner)
  323. {
  324. IntegerField field(value);
  325. return scanner->LocateByIdEx(id, from, &field, nskip, compare_mode);
  326. }
  327. else
  328. return 0;
  329. }
  330. #ifdef _WIN32
  331. int NDE_Scanner_LocateString(nde_scanner_t s, unsigned char id, int from, const wchar_t *value, int *nskip, int compare_mode)
  332. {
  333. Scanner *scanner = (Scanner *)s;
  334. if (scanner)
  335. {
  336. StringField field(value);
  337. return scanner->LocateByIdEx(id, from, &field, nskip, compare_mode);
  338. }
  339. else
  340. return 0;
  341. }
  342. int NDE_Scanner_LocateNDEString(nde_scanner_t s, unsigned char id, int from, wchar_t *value, int *nskip, int compare_mode)
  343. {
  344. Scanner *scanner = (Scanner *)s;
  345. if (scanner)
  346. {
  347. StringField field(value, STRING_IS_NDESTRING);
  348. return scanner->LocateByIdEx(id, from, &field, nskip, compare_mode);
  349. }
  350. else
  351. return 0;
  352. }
  353. #endif
  354. #ifdef _WIN32
  355. int NDE_Scanner_LocateFilename(nde_scanner_t s, unsigned char id, int from, const wchar_t *value, int *nskip, int compare_mode)
  356. {
  357. Scanner *scanner = (Scanner *)s;
  358. if (scanner)
  359. {
  360. FilenameField field(value);
  361. return scanner->LocateByIdEx(id, from, &field, nskip, compare_mode);
  362. }
  363. else
  364. return 0;
  365. }
  366. int NDE_Scanner_LocateNDEFilename(nde_scanner_t s, unsigned char id, int from, wchar_t *value, int *nskip, int compare_mode)
  367. {
  368. Scanner *scanner = (Scanner *)s;
  369. if (scanner)
  370. {
  371. FilenameField field(value, STRING_IS_NDESTRING);
  372. return scanner->LocateByIdEx(id, from, &field, nskip, compare_mode);
  373. }
  374. else
  375. return 0;
  376. }
  377. #endif
  378. int NDE_Scanner_LocateField(nde_scanner_t s, unsigned char id, int from, nde_field_t f, int *nskip, int compare_mode)
  379. {
  380. Scanner *scanner = (Scanner *)s;
  381. Field *field = (Field *)f;
  382. if (scanner && field)
  383. {
  384. return scanner->LocateByIdEx(id, from, field, nskip, compare_mode);
  385. }
  386. else
  387. return 0;
  388. }
  389. void NDE_Scanner_DeleteField(nde_scanner_t s, nde_field_t f)
  390. {
  391. Scanner *scanner = (Scanner *)s;
  392. Field *field = (Field *)f;
  393. if (scanner && field)
  394. {
  395. scanner->DeleteField(field);
  396. }
  397. }
  398. void NDE_Scanner_RemoveFilters(nde_scanner_t s)
  399. {
  400. Scanner *scanner = (Scanner *)s;
  401. if (scanner)
  402. scanner->RemoveFilters();
  403. }
  404. int NDE_Scanner_Next(nde_scanner_t s, int *killswitch)
  405. {
  406. Scanner *scanner = (Scanner *)s;
  407. if (scanner)
  408. return scanner->Next(killswitch);
  409. else
  410. return 0;
  411. }
  412. /* Filter functions */
  413. unsigned char NDE_Filter_GetID(nde_filter_t f)
  414. {
  415. Filter *filter = (Filter *)f;
  416. if (filter)
  417. return filter->GetId();
  418. else
  419. return FILTERS_INVALID; // right value but I'm not sure if it's the best constant name to use
  420. }
  421. NDE_API unsigned char NDE_Filter_GetOp(nde_filter_t f)
  422. {
  423. Filter *filter = (Filter *)f;
  424. if (filter)
  425. return filter->GetOp();
  426. else
  427. return FILTERS_INVALID; // right value but I'm not sure if it's the best constant name to use
  428. }
  429. NDE_API nde_field_t NDE_Filter_GetData(nde_filter_t f)
  430. {
  431. Filter *filter = (Filter *)f;
  432. if (filter)
  433. return (nde_field_t)filter->Data();
  434. else
  435. return 0;
  436. }
  437. /* Field functions */
  438. unsigned char NDE_Field_GetType(nde_field_t f)
  439. {
  440. Field *field = (Field *)f;
  441. if (field)
  442. return field->GetType();
  443. else
  444. return FIELD_UNKNOWN;
  445. }
  446. unsigned char NDE_Field_GetID(nde_field_t f)
  447. {
  448. Field *field = (Field *)f;
  449. if (field)
  450. return field->GetFieldId();
  451. else
  452. return FIELD_UNKNOWN;
  453. }
  454. /* StringField functions */
  455. #ifdef _WIN32
  456. void NDE_StringField_SetNDEString(nde_field_t f, wchar_t *nde_string)
  457. {
  458. StringField *field = (StringField *)(Field *)f;
  459. if (field)
  460. field->SetNDEString(nde_string);
  461. }
  462. wchar_t *NDE_StringField_GetString(nde_field_t f)
  463. {
  464. StringField *field = (StringField *)(Field *)f;
  465. if (field)
  466. return field->GetStringW();
  467. else
  468. return 0;
  469. }
  470. #ifdef _WIN32
  471. void NDE_StringField_SetString(nde_field_t f, const wchar_t *str)
  472. {
  473. StringField *field = (StringField *)(Field *)f;
  474. if (field)
  475. field->SetStringW(str);
  476. }
  477. #endif
  478. #elif defined(__APPLE__)
  479. void NDE_StringField_SetString(nde_field_t f, CFStringRef nde_string)
  480. {
  481. StringField *field = (StringField *)(Field *)f;
  482. if (field)
  483. field->SetNDEString(nde_string);
  484. }
  485. CFStringRef NDE_StringField_GetString(nde_field_t f)
  486. {
  487. StringField *field = (StringField *)(Field *)f;
  488. if (field)
  489. return field->GetString();
  490. else
  491. return 0;
  492. }
  493. #endif
  494. /* IntegerField functions */
  495. void NDE_IntegerField_SetValue(nde_field_t f, int value)
  496. {
  497. IntegerField *field = (IntegerField *)(Field *)f;
  498. if (field)
  499. field->SetValue(value);
  500. }
  501. int NDE_IntegerField_GetValue(nde_field_t f)
  502. {
  503. IntegerField *field = (IntegerField *)(Field *)f;
  504. if (field)
  505. return field->GetValue();
  506. else
  507. return 0;
  508. }
  509. nde_field_t NDE_IntegerField_Create(int value)
  510. {
  511. return (nde_field_t)new IntegerField(value);
  512. }
  513. /* BinaryField */
  514. void *NDE_BinaryField_GetData(nde_field_t f, size_t *length)
  515. {
  516. BinaryField *field = (BinaryField *)(Field *)f;
  517. if (field)
  518. return (void *)field->GetData(length);
  519. else
  520. return 0;
  521. }
  522. #ifdef __APPLE__
  523. CFDataRef NDE_BinaryField_GetCFData(nde_field_t f)
  524. {
  525. BinaryField *field = (BinaryField *)(Field *)f;
  526. if (field)
  527. return field->GetCFData();
  528. else
  529. return 0;
  530. }
  531. #endif
  532. void NDE_BinaryField_SetData(nde_field_t f, const void *data, size_t length)
  533. {
  534. BinaryField *field = (BinaryField *)(Field *)f;
  535. if (field)
  536. field->SetData((const uint8_t *)data, length);
  537. }
  538. /* Int128Field */
  539. void NDE_Int128Field_SetValue(nde_field_t f, const void *value)
  540. {
  541. Int128Field *field = (Int128Field *)(Field *)f;
  542. if (field && value)
  543. field->SetValue(value);
  544. }
  545. /* ColumnField */
  546. #ifdef _WIN32
  547. const wchar_t *NDE_ColumnField_GetFieldName(nde_field_t f)
  548. {
  549. ColumnField *field = (ColumnField *)(Field *)f;
  550. if (field)
  551. return field->GetFieldName();
  552. else
  553. return 0;
  554. }
  555. #else
  556. CFStringRef NDE_ColumnField_GetFieldName(nde_field_t f)
  557. {
  558. ColumnField *field = (ColumnField *)(Field *)f;
  559. if (field)
  560. return field->GetFieldName();
  561. else
  562. return 0;
  563. }
  564. #endif
  565. unsigned char NDE_ColumnField_GetDataType(nde_field_t f)
  566. {
  567. ColumnField *field = (ColumnField *)(Field *)f;
  568. if (field)
  569. return field->GetDataType();
  570. else
  571. return FIELD_UNKNOWN;
  572. }
  573. #ifdef _WIN32
  574. __time64_t NDE_Time_ApplyConversion(__time64_t value, const wchar_t *str, class TimeParse *tp)
  575. {
  576. IntegerField f(value);
  577. f.ApplyConversion(str, tp);
  578. return f.GetValue();
  579. }
  580. #endif