no longer need ffmpeg patch0 which was for Termux
[goodguy/cinelerra.git] / cinelerra-5.1 / db / tx.C
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <fcntl.h>
6 #include <errno.h>
7
8 #include "txs.h"
9
10 // create, modify, list db as set of text lines
11 //  ./a.out new /tmp/x.db
12 //  ./a.out add /tmp/x.db < /data/add.txt
13 //  ./a.out del /tmp/x.db < /data/del.txt
14 //  ./a.out get /tmp/x.db < /data/get.txt
15 //  ./a.out lst /tmp/x.db > /data/dat.txt
16
17 /* first, paste this
18 ./xsch theDb txs <<eof
19 CREATE TABLE item (
20   item_id int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
21   data varchar(255) binary NOT NULL
22 );
23
24 CREATE UNIQUE INDEX item_data ON item (data);
25 eof
26 */
27 // then, compile using:
28 // c++ -ggdb -O2 tx.C txs.C tdb.C
29
30 theDb *db;
31 typedef ItemLoc::ikey_Item_data ItemData;
32 typedef ItemLoc::rkey_Item_data DataItem;
33 class ItemKey : public ItemObj::t_Data {
34 public:
35   ItemKey(char *key, int len) : ItemObj::t_Data((unsigned char *)key,len) {}
36 };
37
38 char line[4096];
39
40 int main(int ac, char **av)
41 {
42   if( !strcmp(av[1],"new") ) {
43     remove(av[2]);
44     db = new theDb();
45     db->create(av[2]);
46     delete db;
47     return 0;
48   }
49
50   db = new theDb();
51   if( db->open(av[2]) < 0 ) exit(1);
52
53   int64_t n =  0;
54
55   if( !strcmp(av[1],"add") ) {
56     while( fgets(line,sizeof(line),stdin) ) {
57       if( !line[0] ) continue;
58       int l = strlen(line);
59       line[l-1] = 0;
60       int ret = ItemData(db->item,ItemKey(line,l)).Find();
61       if( !ret ) continue; // duplicate
62       db->item.Allocate();
63       db->item.Data((unsigned char *)line, l);
64       db->item.Construct();
65       ++n;
66     }
67     db->commit();
68   }
69   else if( !strcmp(av[1],"del") ) {
70     while( fgets(line,sizeof(line),stdin) ) {
71       if( !line[0] ) continue;
72       int l = strlen(line);
73       line[l-1] = 0;
74       int ret = ItemData(db->item,ItemKey(line,l)).Find();
75       if( ret ) continue; // not found
76       db->item.Destruct();
77       db->item.Deallocate();
78       ++n;
79     }
80     db->commit();
81   }
82   else if( !strcmp(av[1],"get") ) {
83     while( fgets(line,sizeof(line),stdin) ) {
84       if( !line[0] ) continue;
85       int l = strlen(line);
86       line[l-1] = 0;
87       int ret = ItemData(db->item,ItemKey(line,l)).Find();
88       if( ret ) continue;
89       printf("%s\n", (char*)db->item.Data());
90       ++n;
91     }
92   }
93   else if( !strcmp(av[1],"lst") ) {
94     if( !DataItem(db->item).First() ) do {
95       printf("%s\n", (char*)db->item.Data());
96       ++n;
97     } while( !DataItem(db->item).Next() );
98   }
99   else {
100     fprintf(stderr, "unknown cmd %s\n  must be new,add,del,get,lst\n", av[1]);
101     exit(1);
102   }
103
104   fprintf(stderr, "%jd items input\n", n);
105   fprintf(stderr, "%d items in db\n", db->Item.Count());
106   db->close();
107   delete db;
108   return 0;
109 }
110