Removed for miss placement.
[chise/kage.git] / kagecgi / kagedb.c
1 //kagedb.c
2 //
3
4 #include "kage.h"
5 #include "kagecgi.h"
6 #include "sysdep.h"
7
8 int initDB(){
9         db_create(&kDatabase, NULL, 0);
10         kDatabase->open(kDatabase, databaseFileName, NULL, DB_HASH, DB_RDONLY, 0644);
11         return 0;
12 }
13
14 int closeDB(){
15         kDatabase->close(kDatabase, 0);
16         return 0;
17 }
18
19 void searchPartsData(const KGString *in, KGString *out){
20         DBT dbkey, dbdata;
21         char *start, *end, *buf;
22         KGString *temp, *temp2;
23         
24         //cut off the end '-0000' if 'in' end with it
25         temp = kg_string_new(in->str);
26 //      if(strncmp(temp->str + temp->len - 5, "-0000", 5) == 0) kg_string_set_size(temp, temp->len - 5);
27         if((temp->str)[0] == 'u'){
28                 if(strncmp(temp->str + temp->len - 4, "-j00", 4) == 0) kg_string_set_size(temp, temp->len - 4);
29                 else if(strncmp(temp->str + temp->len - 4, "-g00", 4) == 0) kg_string_set_size(temp, temp->len - 2);
30                 else if(strncmp(temp->str + temp->len - 4, "-t00", 4) == 0) kg_string_set_size(temp, temp->len - 2);
31                 else if(strncmp(temp->str + temp->len - 4, "-k00", 4) == 0) kg_string_set_size(temp, temp->len - 2);
32                 else if(strncmp(temp->str + temp->len - 4, "-v00", 4) == 0) kg_string_set_size(temp, temp->len - 2);
33                 else if(strncmp(temp->str + temp->len - 4, "-u00", 4) == 0) kg_string_set_size(temp, temp->len - 2);
34         }
35         
36         memset(&dbkey,0,sizeof(DBT));
37         memset(&dbdata,0,sizeof(DBT));
38         dbkey.data = temp->str;
39         dbkey.size = temp->len;
40         kg_string_set_size(out, 0);
41         temp2 = kg_string_new("");
42         kDatabase->get(kDatabase, NULL, &dbkey, &dbdata, 0);
43         if(dbdata.size != 0){
44                 buf = (char *)malloc(dbdata.size + 1);
45                 strncpy(buf, dbdata.data, dbdata.size + 1); 
46                 //first:search selected shotai
47                 if(kShotai == kMincho) start = strstr(buf, ",mincho,");
48                 else start = strstr(buf, ",gothic,");
49                 if(start != NULL){
50                         start = strchr((start+8), ',');
51                         if(start != NULL){
52                                 start = strchr((start+1), ',');
53                                 if(start != NULL){
54                                         end = strchr((start+1), ',');
55                                         if(end != NULL){
56                                                 kg_string_append_len(temp2, (start+1), end - start - 2 + 1);
57                                                 convert99(temp2, out);
58                                                 return;
59                                         }
60                                 }
61                         }
62                 }
63                 //second:search another shotai
64                 if(kShotai == kMincho) start = strstr(buf, ",gothic,");
65                 else start = strstr(buf, ",mincho,");
66                 if(start != NULL){
67                         start = strchr((start+8), ',');
68                         if(start != NULL){
69                                 start = strchr((start+1), ',');
70                                 if(start != NULL){
71                                         end = strchr((start+1), ',');
72                                         if(end != NULL){
73                                                 kg_string_append_len(temp2, (start+1), end - start - 2 + 1);
74                                                 convert99(temp2, out);
75                                                 return;
76                                         }
77                                 }
78                         }
79                 }
80                 free(buf);
81         }
82 }
83
84 void searchAliasData(const KGString *in, KGString *out){
85         DBT dbkey, dbdata;
86         char *start, *end, *buf;
87         KGString *temp;
88         
89         //cut off the end '-0000' if 'in' end with it
90         temp = kg_string_new(in->str);
91 //      if(strncmp(temp->str + temp->len - 5, "-0000", 5) == 0) kg_string_set_size(temp, temp->len - 5);
92         if((temp->str)[0] == 'u'){
93                 if(strncmp(temp->str + temp->len - 4, "-j00", 4) == 0) kg_string_set_size(temp, temp->len - 4);
94                 else if(strncmp(temp->str + temp->len - 4, "-g00", 4) == 0) kg_string_set_size(temp, temp->len - 2);
95                 else if(strncmp(temp->str + temp->len - 4, "-t00", 4) == 0) kg_string_set_size(temp, temp->len - 2);
96                 else if(strncmp(temp->str + temp->len - 4, "-k00", 4) == 0) kg_string_set_size(temp, temp->len - 2);
97                 else if(strncmp(temp->str + temp->len - 4, "-v00", 4) == 0) kg_string_set_size(temp, temp->len - 2);
98                 else if(strncmp(temp->str + temp->len - 4, "-u00", 4) == 0) kg_string_set_size(temp, temp->len - 2);
99         }
100         
101         memset(&dbkey,0,sizeof(DBT));
102         memset(&dbdata,0,sizeof(DBT));
103         dbkey.data = temp->str;
104         dbkey.size = temp->len;
105         kg_string_set_size(out, 0);
106         kDatabase->get(kDatabase, NULL, &dbkey, &dbdata, 0);
107         if(dbdata.size != 0){
108                 buf = (char *)malloc(dbdata.size + 1);
109                 strncpy(buf, dbdata.data, dbdata.size + 1); 
110                 start = strstr(buf, ",linkto,");
111                 if(start != NULL){
112                         start = strchr((start+8), ',');
113                         if(start != NULL){
114                                 start = strchr((start+1), ',');
115                                 if(start != NULL){
116                                         end = strchr((start+1), ',');
117                                         if(end != NULL){
118                                                 kg_string_append_len(out, (start+1), end - start - 2 + 1);
119                                         }
120                                 }
121                         }
122                 }
123                 free(buf);
124         }
125 }
126