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.20 - (hide annotations) (download) (as text)
Tue Sep 13 11:17:03 2005 UTC (18 years, 6 months ago) by maloninc
Branch: MAIN
CVS Tags: dev_1_3-0004
Changes since 1.19: +68 -0 lines
File MIME type: text/x-c++src
implement group by TYPE

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.20 void mnModel::group()
122     {
123     wxCSConv conv(wxT(CODE_SET_SYSTEM));
124     char buf[MAX_BUF_SIZE];
125     wxDir* dir;
126     FILE* fp;
127     wxString* fileName = new wxString();
128     wxString fullPathName;
129     int typeTagLen = strlen(TYPE_TAG);
130     char* token;
131     char* inbufPtr;
132     int inbufSize;
133     char outbuf[MAX_BUF_SIZE];
134     char* outbufPtr = outbuf;
135     int outbufSize;
136     wxString* typeToken;
137     char* ptr;
138     int i;
139    
140     iconv_t codeSet = iconv_open(CODE_SET_SYSTEM, CODE_SET_EUC_JP);
141     if(codeSet == (iconv_t)-1) {
142     MN_FATAL_ERROR(wxT("failed iconv_open"));
143     }
144    
145     dir = new wxDir(*wikiDataDir);
146     if ( !dir->IsOpened() )
147     {
148     MN_FATAL_ERROR(wxT("wxDir has faild\n"));
149     return ;
150     }
151     bool cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES);
152     while(cont){
153     fullPathName = *wikiDataDir + wxT("/") + *fileName;
154     fp = fopen((const char*)fullPathName.mb_str(), "r");
155     if(fp == NULL) {
156     MN_FATAL_ERROR(wxT("fopen faild"));
157     }
158     while(1) {
159     memset(buf, 0, MAX_BUF_SIZE);
160     fgets(buf, MAX_BUF_SIZE, fp);
161     if(buf[0] == 0) break;
162     if(strstr(buf, TYPE_TAG)){
163     ptr = &buf[typeTagLen];
164     while((token = strtok(ptr, " /\r\n:"))!= NULL) {
165     memset(outbuf, 0, MAX_BUF_SIZE);
166     snprintf(outbuf, MAX_BUF_SIZE, "%s", TYPESEARCH_TAG);
167     inbufPtr = token;
168     inbufSize = strlen(token);
169     outbufPtr = &outbuf[strlen(TYPESEARCH_TAG)];
170     outbufSize = sizeof(outbuf) - strlen(TYPESEARCH_TAG);
171     iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
172    
173     typeToken = new wxString(outbuf, conv);
174     //wxLogMessage(wxT("[%s]"), typeToken->c_str());
175     addSearchStr(typeToken);
176     delete typeToken;
177     ptr = NULL;
178     }
179     }
180     }
181     fclose(fp);
182     cont = dir->GetNext(fileName);
183     }
184     delete dir;
185     delete fileName;
186     iconv_close(codeSet);
187     }
188    
189 maloninc 1.19 bool mnModel::normalSearch(char* tokenList[], FILE*fp, char* decodeFileNameBuf)
190     {
191     char buf[MAX_BUF_SIZE];
192     bool found;
193     int i;
194     while(1){
195     memset(buf, 0, MAX_BUF_SIZE);
196     fread(buf, MAX_BUF_SIZE-1, 1, fp);
197     if(buf[0] == 0) break;
198     toLower(buf);
199     found = TRUE;
200     for(i = 0; tokenList[i] != NULL; i++){
201     toLower(tokenList[i]);
202     if(strstr((const char*)buf, (const char*)tokenList[i]) || /* search in file context */
203     strstr((const char*)decodeFileNameBuf, (const char*)tokenList[i]) || /* search in file name */
204     strcmp((const char*)tokenList[i], (const char*)ALLMEMO_TAG) == 0) { /* SHOW ALL MEMO */
205     found = TRUE;
206     }
207     else {
208     found = FALSE;
209     break;
210     }
211     }
212    
213     if(found){ /* all tokens found */
214     break;
215     }
216     buf[0] = 0;
217     }
218    
219     return found;
220     }
221    
222     bool mnModel::typeSearch(char* typeStr, FILE*fp)
223     {
224     char buf[MAX_BUF_SIZE];
225     bool found;
226     int i;
227     char* typeToken;
228     char typeStrCopy[MAX_BUF_SIZE];
229    
230     snprintf(typeStrCopy, MAX_BUF_SIZE, "%s", typeStr);
231     while(1){
232     memset(buf, 0, MAX_BUF_SIZE);
233     fgets(buf, MAX_BUF_SIZE, fp);
234     if(buf[0] == 0) break;
235     if(strstr((const char*)buf, TYPE_TAG)){ /* search TYPE line */
236     typeToken = strtok(typeStrCopy, ":");
237     typeToken = strtok(NULL, ":"); /* second field separated by colon(:) */
238     toLower(typeToken);
239     toLower(buf);
240     if(strstr(buf, typeToken)) return true;
241     }
242     }
243     return false;
244     }
245    
246 maloninc 1.1 void mnModel::addSearchStr(wxString* searchStr)
247     {
248     wxString *string;
249    
250     if(searchStrList->Index(searchStr->c_str()) == wxNOT_FOUND){
251     string = new wxString(searchStr->c_str());
252 maloninc 1.17 //searchStrList->Add(*string, 1);
253     searchStrList->Insert(*string, 0);
254 maloninc 1.1 }
255     }
256    
257     void mnModel::removeSearchStr(wxString searchStr)
258     {
259     if(searchStrList->Index(searchStr.c_str()) != wxNOT_FOUND) {
260     searchStrList->Remove(searchStr.c_str());
261     }
262     }
263    
264     void mnModel::modSearchStr(wxString* oldStr, wxString* newStr)
265     {
266     int index;
267    
268     if((index = searchStrList->Index(oldStr->c_str())) != wxNOT_FOUND){
269     wxString& itemStr = searchStrList->Item(index);
270     itemStr.sprintf(wxT("%s"), newStr->c_str());
271     }
272     }
273 maloninc 1.4 const wxString* mnModel::getWikiDataDir()
274     {
275     return wikiDataDir;
276     }
277 maloninc 1.1
278     const wxArrayString* mnModel::getSearchStrList()
279     {
280     return searchStrList;
281     }
282    
283     WikiData* mnModel::newWikiData()
284     {
285     WikiData* data = new WikiData(wikiDataDir);
286    
287     return data;
288     }
289    
290     /******* WikiData ************************/
291     WikiData::WikiData(wxString* dataDir, const char* file, FILE* fp)
292     {
293     char* decodeStr;
294     char buf[MAX_BUF_SIZE];
295     char* inbuf;
296     int inbufSize;
297     char outbuf[MAX_BUF_SIZE];
298     char* outbufPtr = outbuf;
299     int outbufSize;
300     wxCSConv conv(wxT(CODE_SET_SYSTEM));
301    
302     iconv_t codeSet = iconv_open(CODE_SET_SYSTEM, CODE_SET_EUC_JP);
303     if(codeSet == (iconv_t)-1) {
304     MN_FATAL_ERROR(wxT("failed iconv_open"));
305     }
306    
307     text = NULL;
308     memset(outbuf, 0, MAX_BUF_SIZE);
309     fileName = new wxString((const char*)file, conv);
310     dataDirName = dataDir;
311     decodeStr = decode(file);
312     inbufSize = strlen(decodeStr);
313     outbufSize = sizeof(outbuf);
314 maloninc 1.5 iconv(codeSet, (ICONV_CONST char**)&decodeStr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
315 maloninc 1.1 subject = new wxString((const char*)outbuf, conv);
316     iconv_close(codeSet);
317    
318     date = NULL;
319    
320     rewind(fp);
321     while(fgets(buf, MAX_BUF_SIZE, fp)) {
322     if(strstr( (const char*)buf, (const char*)DATE_TAG)) {
323     strtok(buf, "\n");
324     strtok(buf, "\r");
325     date = new wxString((const char*)buf, conv);
326     break;
327     }
328     }
329     }
330    
331     WikiData::WikiData(wxString* dataDir) {
332     time_t now;
333     char buf[MAX_BUF_SIZE];
334     wxCSConv conv(wxT(CODE_SET_SYSTEM));
335    
336     dataDirName = dataDir;
337    
338     time(&now);
339     memset(buf, 0, sizeof(buf));
340     strftime(buf, sizeof(buf), "%Y/%m/%d-%H%M%S",localtime(&now));
341     subject = new wxString(buf, conv);
342    
343     fileName = new wxString(encode(buf), conv);
344     fileName->Append(wxT(EXT_TAG));
345    
346     memset(buf, 0, sizeof(buf));
347     strftime(buf, sizeof(buf), DATE_TAG "%Y/%m/%d %H:%M:%S",localtime(&now));
348     date = new wxString(buf, conv);
349    
350     memset(buf, 0, sizeof(buf));
351     strftime(buf, sizeof(buf), NEW_DATA,localtime(&now));
352     text = new wxString(buf, conv);
353    
354     }
355    
356     WikiData::~WikiData()
357     {
358     delete subject;
359     delete fileName;
360     delete date;
361     delete text;
362     }
363    
364     const wxString* WikiData::getFileName()
365     {
366     return fileName;
367     }
368    
369     const wxString* WikiData::getSubject()
370     {
371     return subject;
372     }
373    
374     void WikiData::modSubject(wxString* newSubject)
375     {
376     wxCSConv conv(wxT(CODE_SET_SYSTEM));
377     wxString* oldFileName = fileName;
378     wxString* oldSubject = subject;
379     char oldFullPath[MAX_BUF_SIZE];
380     char newFullPath[MAX_BUF_SIZE];
381     iconv_t codeSet;
382     char outbuf[MAX_BUF_SIZE];
383     char inbuf[MAX_BUF_SIZE];
384     const char* inbufPtr = inbuf;
385     char* outbufPtr = outbuf;
386     int inbufSize;
387     int outbufSize = sizeof(outbuf);
388     FILE* fp;
389    
390     memset(outbuf, 0, outbufSize);
391     memset(inbuf, 0, sizeof(inbuf));
392     strcpy(inbuf, (const char*)newSubject->mb_str());
393     inbufSize = strlen(inbuf);
394     codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);
395     if(codeSet == (iconv_t)-1) {
396     MN_FATAL_ERROR(wxT("failed iconv_open"));
397     }
398 maloninc 1.5 iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
399 maloninc 1.1 iconv_close(codeSet);
400     subject = new wxString(newSubject->c_str());
401     fileName = new wxString(encode(outbuf), conv);
402     fileName->Append(wxT(EXT_TAG));
403    
404     sprintf(oldFullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)oldFileName->mb_str());
405     sprintf(newFullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str());
406    
407     if((fp = fopen(newFullPath, "r")) == NULL) {
408 maloninc 1.13 if(rename(oldFullPath, newFullPath) < 0) wxLogMessage(wxT("rename error: errno=[%d]"), errno);
409 maloninc 1.1 }
410 maloninc 1.2 else if(strcmp(oldFullPath, newFullPath)){
411 maloninc 1.1 wxLogMessage(wxT("File has already exist. [%s]"), fileName->c_str());
412     fclose(fp);
413     }
414 maloninc 1.13 else {
415     fclose(fp);
416     }
417 maloninc 1.1
418     delete oldSubject;
419     delete oldFileName;
420     }
421    
422     const wxString* WikiData::getDate()
423     {
424     return date;
425     }
426    
427    
428     const wxString* WikiData::getText()
429     {
430     FILE* fp;
431     char buf[MAX_BUF_SIZE];
432     char fullPath[MAX_BUF_SIZE];
433 maloninc 1.11 iconv_t codeSet;
434     char outbuf[MAX_BUF_SIZE];
435     char* inbufPtr;
436     char* outbufPtr;
437     int inbufSize;
438     int outbufSize;
439 maloninc 1.1 wxCSConv conv(wxT(CODE_SET_SYSTEM));
440     wxString* tmpStr;
441    
442 maloninc 1.11 codeSet = iconv_open(CODE_SET_SYSTEM, CODE_SET_EUC_JP);
443     if(codeSet == (iconv_t)-1) {
444     MN_FATAL_ERROR(wxT("failed iconv_open"));
445     }
446    
447 maloninc 1.1 if(text) {
448 maloninc 1.11 iconv_close(codeSet);
449 maloninc 1.1 return text;
450     }
451    
452     text = new wxString();
453     sprintf(fullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str());
454     fp = fopen(fullPath, "r");
455     if(fp == NULL) {
456     MN_FATAL_ERROR(wxT("File open error."));
457     }
458    
459     while(fgets(buf, MAX_BUF_SIZE, fp)) {
460 maloninc 1.12 #ifdef __WXMAC__
461     for(int i = 0; buf[i] != 0; i++) if(buf[i] == (char)MAC_BACKSLASH) buf[i] = '\\';
462     #endif
463 maloninc 1.11 inbufPtr = buf;
464     inbufSize = sizeof(buf);
465     outbufPtr = outbuf;
466     outbufSize = sizeof(outbuf);
467     memset(outbuf, 0, outbufSize);
468     iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
469     tmpStr = new wxString((char*)outbuf, conv);
470 maloninc 1.1 *text += *tmpStr;
471     delete tmpStr;
472     }
473 maloninc 1.11 iconv_close(codeSet);
474 maloninc 1.1 fclose(fp);
475 maloninc 1.11
476 maloninc 1.1 return text;
477     }
478    
479     void WikiData::modText(wxString* intext)
480     {
481 maloninc 1.3 wxCSConv conv(wxT(CODE_SET_SYSTEM));
482 maloninc 1.1 delete text;
483 maloninc 1.3 //text = new wxString(intext->c_str());
484     text = new wxString(*intext);
485     //text = new wxString(intext->mb_str(), conv);
486 maloninc 1.1 }
487    
488     void WikiData::removeDataFile()
489     {
490     char fullPath[MAX_BUF_SIZE];
491    
492     sprintf(fullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str());
493     if(remove(fullPath)) {
494     MN_FATAL_ERROR(wxT("remove file error"));
495     }
496     }
497    
498     void WikiData::save()
499     {
500     char fullPath[MAX_BUF_SIZE];
501     FILE* fp;
502     iconv_t codeSet;
503     char inbuf[MAX_WIKI_TEXT_SIZE];
504     char outbuf[MAX_WIKI_TEXT_SIZE];
505     const char* inbufPtr;
506     char* outbufPtr;
507     int inbufSize;
508     int outbufSize;
509    
510     codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);
511     if(codeSet == (iconv_t)-1) {
512     MN_FATAL_ERROR(wxT("failed iconv_open"));
513     }
514    
515     sprintf(fullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str());
516     fp = fopen(fullPath, "wb");
517     if(fp == NULL) {
518     MN_FATAL_ERROR(wxT("File open error."));
519     }
520    
521     memset(inbuf, 0, sizeof(inbuf));
522     strcpy(inbuf,(const char*)text->mb_str());
523 maloninc 1.12
524     #ifdef __WXMAC__
525     for(int i = 0; inbuf[i] != 0; i++) if(inbuf[i] == (char)MAC_BACKSLASH) inbuf[i] = '\\';
526     #endif
527    
528 maloninc 1.1 inbufPtr = inbuf;
529     inbufSize = strlen(inbufPtr);
530     outbufPtr = outbuf;
531     outbufSize = sizeof(outbuf);
532     memset(outbuf, 0, outbufSize);
533 maloninc 1.5 iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
534 maloninc 1.1 fwrite(outbuf, sizeof(outbuf)-outbufSize, 1, fp);
535     fclose(fp);
536     iconv_close(codeSet);
537     }
538    
539     /******* Tools ************************/
540    
541 maloninc 1.8 static void toLower(char* string)
542     {
543     int i;
544    
545     for(i = 0; string[i] != 0; i++) {
546     string[i] = tolower(string[i]);
547     }
548     }
549    
550    
551 maloninc 1.1 static char* decode(const char* string)
552     {
553     static char buf[MAX_BUF_SIZE];
554     char c[5];
555     int i,j;
556     char* endPtr = NULL;
557    
558     j = 0;
559     memset(buf, 0, MAX_BUF_SIZE);
560     for(i = 0; string[i] != 0; i+=2) {
561     c[0] = '0'; c[1] = 'x';
562     c[2] = string[i]; c[3] = string[i+1]; c[4] = 0;
563     buf[j] = strtol(c, &endPtr, 0);
564     j++;
565     if(j >= MAX_BUF_SIZE) {
566     buf[MAX_BUF_SIZE] = 0;
567     break;
568     }
569     }
570    
571     return buf;
572     }
573    
574     static char* encode(const char* string)
575     {
576     static char buf[MAX_BUF_SIZE];
577     int i,j;
578    
579     j = 0;
580     memset(buf, 0, MAX_BUF_SIZE);
581     for(i = 0; string[i] != 0; i++) {
582     snprintf(&buf[j], 3, "%02X", (unsigned char)(string[i]));
583     j+=2;
584     if(j >= MAX_BUF_SIZE) {
585     buf[MAX_BUF_SIZE] = 0;
586     break;
587     }
588     }
589     return buf;
590     }
591    
592     /*
593     * wiki1 > wiki2 -> return < 0
594     * wiki1 = wiki2 -> return = 0
595     * wiki1 < wiki2 -> return > 0
596     */
597     static int compWikiData(const void* wiki1, const void* wiki2)
598     {
599     WikiData* data1 = *(WikiData**)wiki1;
600     WikiData* data2 = *(WikiData**)wiki2;
601     const wxString* str1 = NULL;
602     const wxString* str2 = NULL;
603    
604     str1 = data1->getDate();
605     str2 = data2->getDate();
606    
607     if(str1 != NULL && str2 != NULL) {
608     return (strcmp(str2->mb_str(), str1->mb_str()));
609     }
610     else{
611     return 0;
612     }
613     }
614    

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