Develop and Download Open Source Software

Browse CVS Repository

Annotation of /malonnote/mnModel.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.19 - (hide annotations) (download) (as text)
Tue Sep 13 09:11:37 2005 UTC (18 years, 6 months ago) by maloninc
Branch: MAIN
Changes since 1.18: +71 -24 lines
File MIME type: text/x-c++src
implement type searching.

1 maloninc 1.1 #include "mnDef.h"
2     #include "mnModel.h"
3     #include <wx/dir.h>
4     #include <wx/regex.h>
5     #include <iconv.h>
6    
7 maloninc 1.8 static void toLower(char* string);
8 maloninc 1.1 static char* decode(const char* string);
9     static char* encode(const char* string);
10 maloninc 1.8 static int compWikiData(const void* wiki1, const void* wiki2);
11 maloninc 1.1
12    
13     /******* WikiList ************************/
14     #include <wx/listimpl.cpp>
15     WX_DEFINE_LIST(WikiList);
16    
17    
18     /******* mnModel ************************/
19    
20     mnModel::mnModel(const char* dataDir)
21     {
22     wxCSConv conv(wxT(CODE_SET_SYSTEM));
23    
24     wikiDataDir = new wxString(dataDir, conv);
25     searchStrList = new wxArrayString();
26     }
27    
28     mnModel::~mnModel()
29     {
30     delete wikiDataDir;
31     }
32    
33    
34     WikiList* mnModel::search(const char* searchStr)
35     {
36 maloninc 1.9 int i;
37 maloninc 1.1 wxDir* dir;
38     FILE* fp;
39     wxString fullPathName;
40     WikiData* wikiData;
41     WikiList* list = new WikiList();
42     wxString* fileName = new wxString();
43 maloninc 1.19 const char* decodeFileName;
44     char decodeFileNameBuf[MAX_BUF_SIZE];
45 maloninc 1.7 iconv_t codeSet;
46     char outbuf[MAX_BUF_SIZE];
47     const char* inbufPtr = searchStr;
48     char* outbufPtr = outbuf;
49     int inbufSize = strlen(searchStr);
50     int outbufSize = sizeof(outbuf);
51 maloninc 1.9 char* token;
52     char* tokenList[32];
53     bool found;
54 maloninc 1.1
55 maloninc 1.9 memset(tokenList, 0, sizeof(char*)*32);
56 maloninc 1.7 memset(outbuf, 0, outbufSize);
57     codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);
58     if(codeSet == (iconv_t)-1) {
59     MN_FATAL_ERROR(wxT("failed iconv_open"));
60     }
61     iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
62     iconv_close(codeSet);
63 maloninc 1.1
64 maloninc 1.9 /* searchStr to Tokens */
65     token = strtok(outbuf, " ");
66 maloninc 1.16 if(token == NULL) return list;
67 maloninc 1.9 tokenList[0] = (char*)malloc(strlen(token)+1);
68     snprintf(tokenList[0], strlen(token)+1, "%s", token);
69     i = 1;
70     while((token = strtok(NULL, " ")) != NULL) {
71     tokenList[i] = (char*)malloc(strlen(token)+1);
72     snprintf(tokenList[i], strlen(token)+1, "%s", token);
73     i++;
74     }
75    
76 maloninc 1.1 dir = new wxDir(*wikiDataDir);
77     if ( !dir->IsOpened() )
78     {
79     MN_FATAL_ERROR(wxT("wxDir has faild\n"));
80     return NULL;
81     }
82 maloninc 1.4 bool cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES);
83 maloninc 1.1 while(cont){
84     fullPathName = *wikiDataDir + wxT("/") + *fileName;
85     fp = fopen((const char*)fullPathName.mb_str(), "r");
86     if(fp == NULL) {
87     MN_FATAL_ERROR(wxT("fopen faild"));
88     }
89 maloninc 1.9
90 maloninc 1.19 /* TYPE search */
91     if(strstr(tokenList[0], TYPESEARCH_TAG) == tokenList[0])
92     {
93     found = typeSearch(tokenList[0], fp);
94     if(found){
95     wikiData = new WikiData(wikiDataDir, (const char*)fileName->mb_str(), fp);
96     list->Append(wikiData);
97     }
98     }
99     /* Normal search */
100     else{
101 maloninc 1.1 decodeFileName = decode(fileName->mb_str());
102 maloninc 1.8 snprintf(decodeFileNameBuf, MAX_BUF_SIZE, "%s", decodeFileName);
103     toLower(decodeFileNameBuf);
104 maloninc 1.19 found = normalSearch(tokenList, fp, decodeFileNameBuf);
105 maloninc 1.9 if(found){
106 maloninc 1.1 wikiData = new WikiData(wikiDataDir, (const char*)fileName->mb_str(), fp);
107     list->Append(wikiData);
108     }
109     }
110     fclose(fp);
111     cont = dir->GetNext(fileName);
112     }
113     delete dir;
114 maloninc 1.9 delete fileName;
115     for(i = 0; tokenList[i] != NULL; i++) free(tokenList[i]);
116 maloninc 1.1
117     list->Sort(compWikiData);
118     return list;
119     }
120    
121 maloninc 1.19 bool mnModel::normalSearch(char* tokenList[], FILE*fp, char* decodeFileNameBuf)
122     {
123     char buf[MAX_BUF_SIZE];
124     bool found;
125     int i;
126     while(1){
127     memset(buf, 0, MAX_BUF_SIZE);
128     fread(buf, MAX_BUF_SIZE-1, 1, fp);
129     if(buf[0] == 0) break;
130     toLower(buf);
131     found = TRUE;
132     for(i = 0; tokenList[i] != NULL; i++){
133     toLower(tokenList[i]);
134     if(strstr((const char*)buf, (const char*)tokenList[i]) || /* search in file context */
135     strstr((const char*)decodeFileNameBuf, (const char*)tokenList[i]) || /* search in file name */
136     strcmp((const char*)tokenList[i], (const char*)ALLMEMO_TAG) == 0) { /* SHOW ALL MEMO */
137     found = TRUE;
138     }
139     else {
140     found = FALSE;
141     break;
142     }
143     }
144    
145     if(found){ /* all tokens found */
146     break;
147     }
148     buf[0] = 0;
149     }
150    
151     return found;
152     }
153    
154     bool mnModel::typeSearch(char* typeStr, FILE*fp)
155     {
156     char buf[MAX_BUF_SIZE];
157     bool found;
158     int i;
159     char* typeToken;
160     char typeStrCopy[MAX_BUF_SIZE];
161    
162     snprintf(typeStrCopy, MAX_BUF_SIZE, "%s", typeStr);
163     while(1){
164     memset(buf, 0, MAX_BUF_SIZE);
165     fgets(buf, MAX_BUF_SIZE, fp);
166     if(buf[0] == 0) break;
167     if(strstr((const char*)buf, TYPE_TAG)){ /* search TYPE line */
168     typeToken = strtok(typeStrCopy, ":");
169     typeToken = strtok(NULL, ":"); /* second field separated by colon(:) */
170     toLower(typeToken);
171     toLower(buf);
172     if(strstr(buf, typeToken)) return true;
173     }
174     }
175     return false;
176     }
177    
178 maloninc 1.1 void mnModel::addSearchStr(wxString* searchStr)
179     {
180     wxString *string;
181    
182     if(searchStrList->Index(searchStr->c_str()) == wxNOT_FOUND){
183     string = new wxString(searchStr->c_str());
184 maloninc 1.17 //searchStrList->Add(*string, 1);
185     searchStrList->Insert(*string, 0);
186 maloninc 1.1 }
187     }
188    
189     void mnModel::removeSearchStr(wxString searchStr)
190     {
191     if(searchStrList->Index(searchStr.c_str()) != wxNOT_FOUND) {
192     searchStrList->Remove(searchStr.c_str());
193     }
194     }
195    
196     void mnModel::modSearchStr(wxString* oldStr, wxString* newStr)
197     {
198     int index;
199    
200     if((index = searchStrList->Index(oldStr->c_str())) != wxNOT_FOUND){
201     wxString& itemStr = searchStrList->Item(index);
202     itemStr.sprintf(wxT("%s"), newStr->c_str());
203     }
204     }
205 maloninc 1.4 const wxString* mnModel::getWikiDataDir()
206     {
207     return wikiDataDir;
208     }
209 maloninc 1.1
210     const wxArrayString* mnModel::getSearchStrList()
211     {
212     return searchStrList;
213     }
214    
215     WikiData* mnModel::newWikiData()
216     {
217     WikiData* data = new WikiData(wikiDataDir);
218    
219     return data;
220     }
221    
222     /******* WikiData ************************/
223     WikiData::WikiData(wxString* dataDir, const char* file, FILE* fp)
224     {
225     char* decodeStr;
226     char buf[MAX_BUF_SIZE];
227     char* inbuf;
228     int inbufSize;
229     char outbuf[MAX_BUF_SIZE];
230     char* outbufPtr = outbuf;
231     int outbufSize;
232     wxCSConv conv(wxT(CODE_SET_SYSTEM));
233    
234     iconv_t codeSet = iconv_open(CODE_SET_SYSTEM, CODE_SET_EUC_JP);
235     if(codeSet == (iconv_t)-1) {
236     MN_FATAL_ERROR(wxT("failed iconv_open"));
237     }
238    
239     text = NULL;
240     memset(outbuf, 0, MAX_BUF_SIZE);
241     fileName = new wxString((const char*)file, conv);
242     dataDirName = dataDir;
243     decodeStr = decode(file);
244     inbufSize = strlen(decodeStr);
245     outbufSize = sizeof(outbuf);
246 maloninc 1.5 iconv(codeSet, (ICONV_CONST char**)&decodeStr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
247 maloninc 1.1 subject = new wxString((const char*)outbuf, conv);
248     iconv_close(codeSet);
249    
250     date = NULL;
251    
252     rewind(fp);
253     while(fgets(buf, MAX_BUF_SIZE, fp)) {
254     if(strstr( (const char*)buf, (const char*)DATE_TAG)) {
255     strtok(buf, "\n");
256     strtok(buf, "\r");
257     date = new wxString((const char*)buf, conv);
258     break;
259     }
260     }
261     }
262    
263     WikiData::WikiData(wxString* dataDir) {
264     time_t now;
265     char buf[MAX_BUF_SIZE];
266     wxCSConv conv(wxT(CODE_SET_SYSTEM));
267    
268     dataDirName = dataDir;
269    
270     time(&now);
271     memset(buf, 0, sizeof(buf));
272     strftime(buf, sizeof(buf), "%Y/%m/%d-%H%M%S",localtime(&now));
273     subject = new wxString(buf, conv);
274    
275     fileName = new wxString(encode(buf), conv);
276     fileName->Append(wxT(EXT_TAG));
277    
278     memset(buf, 0, sizeof(buf));
279     strftime(buf, sizeof(buf), DATE_TAG "%Y/%m/%d %H:%M:%S",localtime(&now));
280     date = new wxString(buf, conv);
281    
282     memset(buf, 0, sizeof(buf));
283     strftime(buf, sizeof(buf), NEW_DATA,localtime(&now));
284     text = new wxString(buf, conv);
285    
286     }
287    
288     WikiData::~WikiData()
289     {
290     delete subject;
291     delete fileName;
292     delete date;
293     delete text;
294     }
295    
296     const wxString* WikiData::getFileName()
297     {
298     return fileName;
299     }
300    
301     const wxString* WikiData::getSubject()
302     {
303     return subject;
304     }
305    
306     void WikiData::modSubject(wxString* newSubject)
307     {
308     wxCSConv conv(wxT(CODE_SET_SYSTEM));
309     wxString* oldFileName = fileName;
310     wxString* oldSubject = subject;
311     char oldFullPath[MAX_BUF_SIZE];
312     char newFullPath[MAX_BUF_SIZE];
313     iconv_t codeSet;
314     char outbuf[MAX_BUF_SIZE];
315     char inbuf[MAX_BUF_SIZE];
316     const char* inbufPtr = inbuf;
317     char* outbufPtr = outbuf;
318     int inbufSize;
319     int outbufSize = sizeof(outbuf);
320     FILE* fp;
321    
322     memset(outbuf, 0, outbufSize);
323     memset(inbuf, 0, sizeof(inbuf));
324     strcpy(inbuf, (const char*)newSubject->mb_str());
325     inbufSize = strlen(inbuf);
326     codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);
327     if(codeSet == (iconv_t)-1) {
328     MN_FATAL_ERROR(wxT("failed iconv_open"));
329     }
330 maloninc 1.5 iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
331 maloninc 1.1 iconv_close(codeSet);
332     subject = new wxString(newSubject->c_str());
333     fileName = new wxString(encode(outbuf), conv);
334     fileName->Append(wxT(EXT_TAG));
335    
336     sprintf(oldFullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)oldFileName->mb_str());
337     sprintf(newFullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str());
338    
339     if((fp = fopen(newFullPath, "r")) == NULL) {
340 maloninc 1.13 if(rename(oldFullPath, newFullPath) < 0) wxLogMessage(wxT("rename error: errno=[%d]"), errno);
341 maloninc 1.1 }
342 maloninc 1.2 else if(strcmp(oldFullPath, newFullPath)){
343 maloninc 1.1 wxLogMessage(wxT("File has already exist. [%s]"), fileName->c_str());
344     fclose(fp);
345     }
346 maloninc 1.13 else {
347     fclose(fp);
348     }
349 maloninc 1.1
350     delete oldSubject;
351     delete oldFileName;
352     }
353    
354     const wxString* WikiData::getDate()
355     {
356     return date;
357     }
358    
359    
360     const wxString* WikiData::getText()
361     {
362     FILE* fp;
363     char buf[MAX_BUF_SIZE];
364     char fullPath[MAX_BUF_SIZE];
365 maloninc 1.11 iconv_t codeSet;
366     char outbuf[MAX_BUF_SIZE];
367     char* inbufPtr;
368     char* outbufPtr;
369     int inbufSize;
370     int outbufSize;
371 maloninc 1.1 wxCSConv conv(wxT(CODE_SET_SYSTEM));
372     wxString* tmpStr;
373    
374 maloninc 1.11 codeSet = iconv_open(CODE_SET_SYSTEM, CODE_SET_EUC_JP);
375     if(codeSet == (iconv_t)-1) {
376     MN_FATAL_ERROR(wxT("failed iconv_open"));
377     }
378    
379 maloninc 1.1 if(text) {
380 maloninc 1.11 iconv_close(codeSet);
381 maloninc 1.1 return text;
382     }
383    
384     text = new wxString();
385     sprintf(fullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str());
386     fp = fopen(fullPath, "r");
387     if(fp == NULL) {
388     MN_FATAL_ERROR(wxT("File open error."));
389     }
390    
391     while(fgets(buf, MAX_BUF_SIZE, fp)) {
392 maloninc 1.12 #ifdef __WXMAC__
393     for(int i = 0; buf[i] != 0; i++) if(buf[i] == (char)MAC_BACKSLASH) buf[i] = '\\';
394     #endif
395 maloninc 1.11 inbufPtr = buf;
396     inbufSize = sizeof(buf);
397     outbufPtr = outbuf;
398     outbufSize = sizeof(outbuf);
399     memset(outbuf, 0, outbufSize);
400     iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
401     tmpStr = new wxString((char*)outbuf, conv);
402 maloninc 1.1 *text += *tmpStr;
403     delete tmpStr;
404     }
405 maloninc 1.11 iconv_close(codeSet);
406 maloninc 1.1 fclose(fp);
407 maloninc 1.11
408 maloninc 1.1 return text;
409     }
410    
411     void WikiData::modText(wxString* intext)
412     {
413 maloninc 1.3 wxCSConv conv(wxT(CODE_SET_SYSTEM));
414 maloninc 1.1 delete text;
415 maloninc 1.3 //text = new wxString(intext->c_str());
416     text = new wxString(*intext);
417     //text = new wxString(intext->mb_str(), conv);
418 maloninc 1.1 }
419    
420     void WikiData::removeDataFile()
421     {
422     char fullPath[MAX_BUF_SIZE];
423    
424     sprintf(fullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str());
425     if(remove(fullPath)) {
426     MN_FATAL_ERROR(wxT("remove file error"));
427     }
428     }
429    
430     void WikiData::save()
431     {
432     char fullPath[MAX_BUF_SIZE];
433     FILE* fp;
434     iconv_t codeSet;
435     char inbuf[MAX_WIKI_TEXT_SIZE];
436     char outbuf[MAX_WIKI_TEXT_SIZE];
437     const char* inbufPtr;
438     char* outbufPtr;
439     int inbufSize;
440     int outbufSize;
441    
442     codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);
443     if(codeSet == (iconv_t)-1) {
444     MN_FATAL_ERROR(wxT("failed iconv_open"));
445     }
446    
447     sprintf(fullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str());
448     fp = fopen(fullPath, "wb");
449     if(fp == NULL) {
450     MN_FATAL_ERROR(wxT("File open error."));
451     }
452    
453     memset(inbuf, 0, sizeof(inbuf));
454     strcpy(inbuf,(const char*)text->mb_str());
455 maloninc 1.12
456     #ifdef __WXMAC__
457     for(int i = 0; inbuf[i] != 0; i++) if(inbuf[i] == (char)MAC_BACKSLASH) inbuf[i] = '\\';
458     #endif
459    
460 maloninc 1.1 inbufPtr = inbuf;
461     inbufSize = strlen(inbufPtr);
462     outbufPtr = outbuf;
463     outbufSize = sizeof(outbuf);
464     memset(outbuf, 0, outbufSize);
465 maloninc 1.5 iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
466 maloninc 1.1 fwrite(outbuf, sizeof(outbuf)-outbufSize, 1, fp);
467     fclose(fp);
468     iconv_close(codeSet);
469     }
470    
471     /******* Tools ************************/
472    
473 maloninc 1.8 static void toLower(char* string)
474     {
475     int i;
476    
477     for(i = 0; string[i] != 0; i++) {
478     string[i] = tolower(string[i]);
479     }
480     }
481    
482    
483 maloninc 1.1 static char* decode(const char* string)
484     {
485     static char buf[MAX_BUF_SIZE];
486     char c[5];
487     int i,j;
488     char* endPtr = NULL;
489    
490     j = 0;
491     memset(buf, 0, MAX_BUF_SIZE);
492     for(i = 0; string[i] != 0; i+=2) {
493     c[0] = '0'; c[1] = 'x';
494     c[2] = string[i]; c[3] = string[i+1]; c[4] = 0;
495     buf[j] = strtol(c, &endPtr, 0);
496     j++;
497     if(j >= MAX_BUF_SIZE) {
498     buf[MAX_BUF_SIZE] = 0;
499     break;
500     }
501     }
502    
503     return buf;
504     }
505    
506     static char* encode(const char* string)
507     {
508     static char buf[MAX_BUF_SIZE];
509     int i,j;
510    
511     j = 0;
512     memset(buf, 0, MAX_BUF_SIZE);
513     for(i = 0; string[i] != 0; i++) {
514     snprintf(&buf[j], 3, "%02X", (unsigned char)(string[i]));
515     j+=2;
516     if(j >= MAX_BUF_SIZE) {
517     buf[MAX_BUF_SIZE] = 0;
518     break;
519     }
520     }
521     return buf;
522     }
523    
524     /*
525     * wiki1 > wiki2 -> return < 0
526     * wiki1 = wiki2 -> return = 0
527     * wiki1 < wiki2 -> return > 0
528     */
529     static int compWikiData(const void* wiki1, const void* wiki2)
530     {
531     WikiData* data1 = *(WikiData**)wiki1;
532     WikiData* data2 = *(WikiData**)wiki2;
533     const wxString* str1 = NULL;
534     const wxString* str2 = NULL;
535    
536     str1 = data1->getDate();
537     str2 = data2->getDate();
538    
539     if(str1 != NULL && str2 != NULL) {
540     return (strcmp(str2->mb_str(), str1->mb_str()));
541     }
542     else{
543     return 0;
544     }
545     }
546    

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26