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.36 - (hide annotations) (download) (as text)
Tue Oct 24 06:46:32 2006 UTC (17 years, 4 months ago) by maloninc
Branch: MAIN
CVS Tags: HEAD
Changes since 1.35: +1 -1 lines
File MIME type: text/x-c++src
add fast search option menu, modify readme

1 maloninc 1.1 #include "mnDef.h"
2     #include "mnModel.h"
3     #include <wx/dir.h>
4     #include <wx/regex.h>
5 maloninc 1.33 #include <wx/progdlg.h>
6 maloninc 1.1 #include <iconv.h>
7 maloninc 1.31 #include <ctype.h>
8 maloninc 1.1
9 maloninc 1.8 static void toLower(char* string);
10 maloninc 1.1 static char* decode(const char* string);
11     static char* encode(const char* string);
12 maloninc 1.8 static int compWikiData(const void* wiki1, const void* wiki2);
13 maloninc 1.1
14    
15     /******* WikiList ************************/
16     #include <wx/listimpl.cpp>
17     WX_DEFINE_LIST(WikiList);
18    
19    
20     /******* mnModel ************************/
21    
22     mnModel::mnModel(const char* dataDir)
23     {
24     wxCSConv conv(wxT(CODE_SET_SYSTEM));
25    
26     wikiDataDir = new wxString(dataDir, conv);
27     searchStrList = new wxArrayString();
28     }
29    
30     mnModel::~mnModel()
31     {
32     delete wikiDataDir;
33     }
34    
35 maloninc 1.23 /*
36     * iconv encode and create token list,
37     * so you must free tokenLis after you used
38     *
39     * if failed to make token list, return FALSE.
40     */
41     bool mnModel::makeSearchToken(const char* searchStr, char* tokenList[])
42 maloninc 1.1 {
43 maloninc 1.7 iconv_t codeSet;
44     char outbuf[MAX_BUF_SIZE];
45 maloninc 1.23 const char* inbufPtr = searchStr;
46     char* outbufPtr = outbuf;
47     int inbufSize = strlen(searchStr);
48 maloninc 1.7 int outbufSize = sizeof(outbuf);
49 maloninc 1.9 char* token;
50 maloninc 1.23 int i;
51 maloninc 1.1
52 maloninc 1.7 memset(outbuf, 0, outbufSize);
53     codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);
54     if(codeSet == (iconv_t)-1) {
55     MN_FATAL_ERROR(wxT("failed iconv_open"));
56     }
57     iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
58     iconv_close(codeSet);
59 maloninc 1.1
60 maloninc 1.9 /* searchStr to Tokens */
61     token = strtok(outbuf, " ");
62 maloninc 1.23 if(token == NULL) return false;
63 maloninc 1.9 tokenList[0] = (char*)malloc(strlen(token)+1);
64     snprintf(tokenList[0], strlen(token)+1, "%s", token);
65     i = 1;
66     while((token = strtok(NULL, " ")) != NULL) {
67     tokenList[i] = (char*)malloc(strlen(token)+1);
68     snprintf(tokenList[i], strlen(token)+1, "%s", token);
69     i++;
70 maloninc 1.25 if(i >= MAX_TOKEN) break;
71 maloninc 1.9 }
72 maloninc 1.23 return true;
73     }
74    
75     bool mnModel::matchWithToken(wxString* fileName, char* tokenList[])
76     {
77     const char* decodeFileName;
78     char decodeFileNameBuf[MAX_BUF_SIZE];
79     wxString fullPathName;
80     FILE* fp;
81     bool ans = false;
82     bool found;
83    
84     fullPathName = *wikiDataDir + wxT("/") + *fileName;
85     fp = fopen((const char*)fullPathName.mb_str(), "r");
86     if(fp == NULL) {
87 maloninc 1.24 return false; /* because of removed */
88 maloninc 1.23 }
89    
90     /* TYPE search */
91     if(strstr(tokenList[0], TYPESEARCH_TAG) == tokenList[0])
92     {
93     found = typeSearch(tokenList[0], fp);
94     if(found){
95     ans = true;
96     }
97     }
98     /* Normal search */
99     else{
100     decodeFileName = decode(fileName->mb_str());
101     snprintf(decodeFileNameBuf, MAX_BUF_SIZE, "%s", decodeFileName);
102     toLower(decodeFileNameBuf);
103     found = normalSearch(tokenList, fp, decodeFileNameBuf);
104     if(found){
105     ans = true;
106     }
107     }
108     fclose(fp);
109    
110     return ans;
111     }
112    
113 maloninc 1.34 void mnModel::readAll(bool progBarFlag)
114 maloninc 1.32 {
115 maloninc 1.33 int fileCount = 0;
116 maloninc 1.32 wxDir* dir;
117     WikiData* wikiData;
118     wxString* fileName = new wxString();
119     char* tokenList[MAX_TOKEN];
120 maloninc 1.33 bool cont;
121 maloninc 1.35 wxProgressDialog* progBar = NULL;
122 maloninc 1.32
123     memset(tokenList, 0, sizeof(char*)*MAX_TOKEN);
124     tokenList[0] = "";
125    
126     dir = new wxDir(*wikiDataDir);
127     if ( !dir->IsOpened() )
128     {
129     MN_FATAL_ERROR(wxT("wxDir has faild\n"));
130     return ;
131     }
132 maloninc 1.33
133     /* count number of files */
134     if (progBarFlag) {
135     cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES);
136     while(cont){
137     fileCount++;
138     cont = dir->GetNext(fileName);
139     }
140 maloninc 1.36 progBar = new wxProgressDialog(wxT("Searching Data"), (wxT("Now searching... ") + *wikiDataDir), fileCount);
141 maloninc 1.33 }
142 maloninc 1.32
143 maloninc 1.33 fileCount = 0;
144     cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES);
145 maloninc 1.32 while(cont){
146 maloninc 1.33 fileCount++;
147     if (progBarFlag) progBar->Update(fileCount);
148 maloninc 1.32 matchWithToken(fileName, tokenList); /* match with token list, but never match, just read. */
149     cont = dir->GetNext(fileName);
150     }
151    
152 maloninc 1.35 if (progBarFlag) {
153     delete progBar;
154     }
155 maloninc 1.32 delete dir;
156     delete fileName;
157     }
158    
159 maloninc 1.23 WikiList* mnModel::search(const char* searchStr)
160     {
161     int i;
162     wxDir* dir;
163     WikiData* wikiData;
164     WikiList* list = new WikiList();
165     wxString* fileName = new wxString();
166 maloninc 1.25 char* tokenList[MAX_TOKEN];
167 maloninc 1.23
168 maloninc 1.25 memset(tokenList, 0, sizeof(char*)*MAX_TOKEN);
169 maloninc 1.23 if( makeSearchToken(searchStr, tokenList) == false) return list;
170 maloninc 1.9
171 maloninc 1.1 dir = new wxDir(*wikiDataDir);
172     if ( !dir->IsOpened() )
173     {
174     MN_FATAL_ERROR(wxT("wxDir has faild\n"));
175     return NULL;
176     }
177 maloninc 1.23
178 maloninc 1.4 bool cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES);
179 maloninc 1.1 while(cont){
180 maloninc 1.23
181     if( matchWithToken(fileName, tokenList) ) { /* match with token list */
182     wikiData = new WikiData(wikiDataDir, fileName);
183     list->Append(wikiData);
184 maloninc 1.1 }
185 maloninc 1.9
186 maloninc 1.1 cont = dir->GetNext(fileName);
187     }
188 maloninc 1.23
189 maloninc 1.1 delete dir;
190 maloninc 1.9 delete fileName;
191     for(i = 0; tokenList[i] != NULL; i++) free(tokenList[i]);
192 maloninc 1.1
193     list->Sort(compWikiData);
194     return list;
195     }
196    
197 maloninc 1.24 /*
198     * add malon-type:xxx to search list
199     */
200 maloninc 1.20 void mnModel::group()
201     {
202     wxCSConv conv(wxT(CODE_SET_SYSTEM));
203     char buf[MAX_BUF_SIZE];
204     wxDir* dir;
205     FILE* fp;
206     wxString* fileName = new wxString();
207     wxString fullPathName;
208     int typeTagLen = strlen(TYPE_TAG);
209     char* token;
210     char* inbufPtr;
211     int inbufSize;
212     char outbuf[MAX_BUF_SIZE];
213     char* outbufPtr = outbuf;
214     int outbufSize;
215     wxString* typeToken;
216     char* ptr;
217     int i;
218    
219     iconv_t codeSet = iconv_open(CODE_SET_SYSTEM, CODE_SET_EUC_JP);
220     if(codeSet == (iconv_t)-1) {
221     MN_FATAL_ERROR(wxT("failed iconv_open"));
222     }
223    
224     dir = new wxDir(*wikiDataDir);
225     if ( !dir->IsOpened() )
226     {
227     MN_FATAL_ERROR(wxT("wxDir has faild\n"));
228     return ;
229     }
230     bool cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES);
231     while(cont){
232     fullPathName = *wikiDataDir + wxT("/") + *fileName;
233     fp = fopen((const char*)fullPathName.mb_str(), "r");
234     if(fp == NULL) {
235     MN_FATAL_ERROR(wxT("fopen faild"));
236     }
237     while(1) {
238     memset(buf, 0, MAX_BUF_SIZE);
239     fgets(buf, MAX_BUF_SIZE, fp);
240     if(buf[0] == 0) break;
241     if(strstr(buf, TYPE_TAG)){
242     ptr = &buf[typeTagLen];
243     while((token = strtok(ptr, " /\r\n:"))!= NULL) {
244     memset(outbuf, 0, MAX_BUF_SIZE);
245     snprintf(outbuf, MAX_BUF_SIZE, "%s", TYPESEARCH_TAG);
246     inbufPtr = token;
247     inbufSize = strlen(token);
248     outbufPtr = &outbuf[strlen(TYPESEARCH_TAG)];
249     outbufSize = sizeof(outbuf) - strlen(TYPESEARCH_TAG);
250     iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
251    
252     typeToken = new wxString(outbuf, conv);
253 maloninc 1.24 if( addSearchStr(typeToken) ) {
254     WikiList* wikiList = search(typeToken->mb_str());
255     addSearchList(typeToken, wikiList);
256     }
257 maloninc 1.20 delete typeToken;
258     ptr = NULL;
259     }
260     }
261     }
262     fclose(fp);
263     cont = dir->GetNext(fileName);
264     }
265     delete dir;
266     delete fileName;
267     iconv_close(codeSet);
268     }
269    
270 maloninc 1.19 bool mnModel::normalSearch(char* tokenList[], FILE*fp, char* decodeFileNameBuf)
271     {
272     char buf[MAX_BUF_SIZE];
273     bool found;
274     int i;
275     while(1){
276     memset(buf, 0, MAX_BUF_SIZE);
277     fread(buf, MAX_BUF_SIZE-1, 1, fp);
278     if(buf[0] == 0) break;
279     toLower(buf);
280     found = TRUE;
281     for(i = 0; tokenList[i] != NULL; i++){
282     toLower(tokenList[i]);
283     if(strstr((const char*)buf, (const char*)tokenList[i]) || /* search in file context */
284     strstr((const char*)decodeFileNameBuf, (const char*)tokenList[i]) || /* search in file name */
285     strcmp((const char*)tokenList[i], (const char*)ALLMEMO_TAG) == 0) { /* SHOW ALL MEMO */
286     found = TRUE;
287     }
288     else {
289     found = FALSE;
290     break;
291     }
292     }
293    
294     if(found){ /* all tokens found */
295     break;
296     }
297     buf[0] = 0;
298     }
299    
300     return found;
301     }
302    
303     bool mnModel::typeSearch(char* typeStr, FILE*fp)
304     {
305     char buf[MAX_BUF_SIZE];
306     bool found;
307     int i;
308     char* typeToken;
309     char typeStrCopy[MAX_BUF_SIZE];
310    
311     snprintf(typeStrCopy, MAX_BUF_SIZE, "%s", typeStr);
312     while(1){
313     memset(buf, 0, MAX_BUF_SIZE);
314     fgets(buf, MAX_BUF_SIZE, fp);
315     if(buf[0] == 0) break;
316     if(strstr((const char*)buf, TYPE_TAG)){ /* search TYPE line */
317     typeToken = strtok(typeStrCopy, ":");
318     typeToken = strtok(NULL, ":"); /* second field separated by colon(:) */
319 maloninc 1.21 if(typeToken == NULL) return false;
320 maloninc 1.19 toLower(typeToken);
321     toLower(buf);
322     if(strstr(buf, typeToken)) return true;
323     }
324     }
325     return false;
326     }
327    
328 maloninc 1.24 bool mnModel::addSearchStr(wxString* searchStr)
329 maloninc 1.1 {
330     wxString *string;
331    
332     if(searchStrList->Index(searchStr->c_str()) == wxNOT_FOUND){
333     string = new wxString(searchStr->c_str());
334 maloninc 1.22 searchStrList->Add(*string, 1);
335     //searchStrList->Insert(*string, 0);
336 maloninc 1.24 return true; /* Add */
337 maloninc 1.1 }
338 maloninc 1.24 return false; /* does'nt add because of duplicating */
339 maloninc 1.1 }
340    
341 maloninc 1.23 void mnModel::addSearchList(wxString* searchStr, WikiList* list)
342     {
343     wikiHash[*searchStr] = list;
344     }
345    
346 maloninc 1.1 void mnModel::removeSearchStr(wxString searchStr)
347     {
348     if(searchStrList->Index(searchStr.c_str()) != wxNOT_FOUND) {
349     searchStrList->Remove(searchStr.c_str());
350 maloninc 1.24 wikiHash[*searchStr] = NULL;
351 maloninc 1.1 }
352     }
353    
354     void mnModel::modSearchStr(wxString* oldStr, wxString* newStr)
355     {
356     int index;
357    
358     if((index = searchStrList->Index(oldStr->c_str())) != wxNOT_FOUND){
359     wxString& itemStr = searchStrList->Item(index);
360     itemStr.sprintf(wxT("%s"), newStr->c_str());
361     }
362     }
363 maloninc 1.24
364     void mnModel::clearSearchStrList()
365     {
366     searchStrList->Clear();
367     }
368    
369     void mnModel::clearSearchResultList()
370     {
371     wikiHash.clear();
372     }
373    
374 maloninc 1.4 const wxString* mnModel::getWikiDataDir()
375     {
376     return wikiDataDir;
377     }
378 maloninc 1.1
379     const wxArrayString* mnModel::getSearchStrList()
380     {
381     return searchStrList;
382     }
383    
384     WikiData* mnModel::newWikiData()
385     {
386     WikiData* data = new WikiData(wikiDataDir);
387    
388     return data;
389     }
390 maloninc 1.23 const WikiList* mnModel::getSearchResultList(wxString* searchStr)
391     {
392     return wikiHash[*searchStr];
393     }
394    
395     /* add "addData's" clone. if already exist same data, overwrite it*/
396     void mnModel::addSearchResultList(wxString* searchStr, WikiData* addData)
397     {
398     WikiList* wikiList = wikiHash[*searchStr];
399     WikiList::Node* node;
400     WikiData* data;
401    
402 maloninc 1.24 if(wikiList == NULL) {
403     MN_FATAL_ERROR(wxT("wikiList is null"));
404     return;
405 maloninc 1.23 }
406    
407 maloninc 1.24 node = wikiList->GetFirst();
408 maloninc 1.23 while(node) {
409     data = node->GetData();
410     if(data == addData) return;
411 maloninc 1.24 if( *(data->getSubject()) == *(addData->getOldSubject()) ) {
412 maloninc 1.23 if(wikiList->DeleteObject(data)) {
413 maloninc 1.24 //delete data; /* may be wrong.. */
414 maloninc 1.23 break;
415     }
416     else {
417     MN_FATAL_ERROR(wxT("Can't find delete data"));
418     }
419     }
420     node = node->GetNext();
421     }
422     WikiData* copy = new WikiData((wxString*)wikiDataDir, (wxString*)addData->getFileName());
423     wikiList->Append(copy);
424     wikiList->Sort(compWikiData);
425     }
426 maloninc 1.1
427 maloninc 1.24 bool mnModel::delSearchResultList(wxString* searchStr, WikiData* delData)
428     {
429     WikiList* wikiList = wikiHash[*searchStr];
430     WikiList::Node* node;
431     WikiData* data;
432     bool found = false;
433    
434     if(wikiList == NULL) {
435     MN_FATAL_ERROR(wxT("wikiList is null"));
436     return false;
437     }
438    
439     node = wikiList->GetFirst();
440     while(node) {
441     data = node->GetData();
442     if(data == delData) {
443     if(!wikiList->DeleteObject(data)) {
444     MN_FATAL_ERROR(wxT("Can't find delete data"));
445     }
446     found = true;
447     break;
448     }
449     else if( *(data->getSubject()) == *(delData->getOldSubject()) ) {
450     if(wikiList->DeleteObject(data)) {
451     //delete data; /* may be wrong.. */
452     found = true;
453     break;
454     }
455     else {
456     MN_FATAL_ERROR(wxT("Can't find delete data"));
457     }
458     }
459     node = node->GetNext();
460     }
461     wikiList->Sort(compWikiData);
462     return found;
463     }
464    
465 maloninc 1.1 /******* WikiData ************************/
466 maloninc 1.23 WikiData::WikiData(wxString* dataDir, wxString* inFileName)
467 maloninc 1.1 {
468 maloninc 1.23 FILE* fp;
469     wxString fullPathName;
470 maloninc 1.1 char* decodeStr;
471     char buf[MAX_BUF_SIZE];
472     char* inbuf;
473     int inbufSize;
474     char outbuf[MAX_BUF_SIZE];
475     char* outbufPtr = outbuf;
476     int outbufSize;
477     wxCSConv conv(wxT(CODE_SET_SYSTEM));
478    
479     iconv_t codeSet = iconv_open(CODE_SET_SYSTEM, CODE_SET_EUC_JP);
480     if(codeSet == (iconv_t)-1) {
481     MN_FATAL_ERROR(wxT("failed iconv_open"));
482     }
483    
484     text = NULL;
485     memset(outbuf, 0, MAX_BUF_SIZE);
486 maloninc 1.24 fileName = new wxString(inFileName->mb_str(), conv);
487     dataDirName = new wxString(dataDir->mb_str(), conv);
488 maloninc 1.23 decodeStr = decode(fileName->mb_str());
489 maloninc 1.1 inbufSize = strlen(decodeStr);
490     outbufSize = sizeof(outbuf);
491 maloninc 1.5 iconv(codeSet, (ICONV_CONST char**)&decodeStr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
492 maloninc 1.1 subject = new wxString((const char*)outbuf, conv);
493 maloninc 1.24 oldSubject = new wxString((const char*)outbuf, conv);
494 maloninc 1.1 iconv_close(codeSet);
495    
496     date = NULL;
497    
498 maloninc 1.23 fullPathName = *dataDir + wxT("/") + *fileName;
499     fp = fopen((const char*)fullPathName.mb_str(), "r");
500     if(fp == NULL) {
501     MN_FATAL_ERROR(wxT("fopen faild"));
502     }
503 maloninc 1.1 while(fgets(buf, MAX_BUF_SIZE, fp)) {
504     if(strstr( (const char*)buf, (const char*)DATE_TAG)) {
505     strtok(buf, "\n");
506     strtok(buf, "\r");
507     date = new wxString((const char*)buf, conv);
508     break;
509     }
510     }
511 maloninc 1.23 fclose(fp);
512 maloninc 1.30
513     isWriteToFile = true;
514 maloninc 1.1 }
515    
516     WikiData::WikiData(wxString* dataDir) {
517 maloninc 1.27 FILE* fp;
518 maloninc 1.1 time_t now;
519     char buf[MAX_BUF_SIZE];
520 maloninc 1.27 char fname[MAX_BUF_SIZE];
521     char templateBuf[MAX_BUF_SIZE];
522     char* inbufPtr;
523     int inbufSize;
524     char outbuf[MAX_BUF_SIZE];
525     char* outbufPtr;
526     int outbufSize;
527     wxCSConv conv(wxT(CODE_SET_SYSTEM));
528 maloninc 1.1
529 maloninc 1.24 dataDirName = new wxString(dataDir->mb_str(), conv);
530 maloninc 1.1
531     time(&now);
532     memset(buf, 0, sizeof(buf));
533     strftime(buf, sizeof(buf), "%Y/%m/%d-%H%M%S",localtime(&now));
534     subject = new wxString(buf, conv);
535 maloninc 1.24 oldSubject = new wxString(buf, conv);
536 maloninc 1.1
537     fileName = new wxString(encode(buf), conv);
538     fileName->Append(wxT(EXT_TAG));
539    
540     memset(buf, 0, sizeof(buf));
541     strftime(buf, sizeof(buf), DATE_TAG "%Y/%m/%d %H:%M:%S",localtime(&now));
542     date = new wxString(buf, conv);
543 maloninc 1.27
544     /* try to open template file */
545     snprintf(fname, sizeof(fname), "%s/%s", (const char*)(dataDir->mb_str()), NEW_DATA_TEMPLATE);
546     fp = fopen(fname, "r");
547     if(fp == NULL){
548     memset(buf, 0, sizeof(buf));
549     strftime(buf, sizeof(buf), NEW_DATA,localtime(&now));
550     }
551     else {
552     memset(buf, 0, sizeof(buf));
553     memset(templateBuf, 0, sizeof(templateBuf));
554     memset(outbuf, 0, sizeof(outbuf));
555     fread(templateBuf, sizeof(templateBuf), 1, fp);
556 maloninc 1.1
557 maloninc 1.27 iconv_t codeSet = iconv_open(CODE_SET_SYSTEM, CODE_SET_EUC_JP);
558     if(codeSet == (iconv_t)-1) {
559     MN_FATAL_ERROR(wxT("failed iconv_open"));
560     }
561     inbufPtr = templateBuf;
562     outbufPtr = outbuf;
563     inbufSize = strlen(templateBuf);
564     outbufSize = sizeof(outbuf);
565     iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
566    
567     strftime(buf, sizeof(buf), outbuf,localtime(&now));
568    
569     }
570 maloninc 1.1 text = new wxString(buf, conv);
571    
572 maloninc 1.27 if(fp) fclose(fp);
573 maloninc 1.30
574     isWriteToFile = false;
575 maloninc 1.1 }
576    
577     WikiData::~WikiData()
578     {
579     delete subject;
580 maloninc 1.24 delete oldSubject;
581 maloninc 1.1 delete fileName;
582     delete date;
583     delete text;
584 maloninc 1.24 delete dataDirName;
585 maloninc 1.1 }
586    
587     const wxString* WikiData::getFileName()
588     {
589     return fileName;
590     }
591    
592     const wxString* WikiData::getSubject()
593     {
594     return subject;
595     }
596    
597 maloninc 1.24 const wxString* WikiData::getOldSubject()
598     {
599     return oldSubject;
600     }
601    
602 maloninc 1.25 void WikiData::setOldSubjectFromCurrent()
603     {
604     oldSubject = new wxString(*subject);
605     }
606    
607 maloninc 1.1 void WikiData::modSubject(wxString* newSubject)
608     {
609     wxCSConv conv(wxT(CODE_SET_SYSTEM));
610     wxString* oldFileName = fileName;
611     char oldFullPath[MAX_BUF_SIZE];
612     char newFullPath[MAX_BUF_SIZE];
613     iconv_t codeSet;
614     char outbuf[MAX_BUF_SIZE];
615     char inbuf[MAX_BUF_SIZE];
616     const char* inbufPtr = inbuf;
617     char* outbufPtr = outbuf;
618     int inbufSize;
619     int outbufSize = sizeof(outbuf);
620     FILE* fp;
621    
622 maloninc 1.24 oldSubject = subject;
623    
624 maloninc 1.1 memset(outbuf, 0, outbufSize);
625     memset(inbuf, 0, sizeof(inbuf));
626     strcpy(inbuf, (const char*)newSubject->mb_str());
627     inbufSize = strlen(inbuf);
628     codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);
629     if(codeSet == (iconv_t)-1) {
630     MN_FATAL_ERROR(wxT("failed iconv_open"));
631     }
632 maloninc 1.5 iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
633 maloninc 1.1 iconv_close(codeSet);
634     subject = new wxString(newSubject->c_str());
635     fileName = new wxString(encode(outbuf), conv);
636     fileName->Append(wxT(EXT_TAG));
637    
638     sprintf(oldFullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)oldFileName->mb_str());
639     sprintf(newFullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str());
640    
641     if((fp = fopen(newFullPath, "r")) == NULL) {
642 maloninc 1.13 if(rename(oldFullPath, newFullPath) < 0) wxLogMessage(wxT("rename error: errno=[%d]"), errno);
643 maloninc 1.1 }
644 maloninc 1.2 else if(strcmp(oldFullPath, newFullPath)){
645 maloninc 1.1 wxLogMessage(wxT("File has already exist. [%s]"), fileName->c_str());
646     fclose(fp);
647     }
648 maloninc 1.13 else {
649     fclose(fp);
650     }
651 maloninc 1.1
652     delete oldFileName;
653     }
654    
655     const wxString* WikiData::getDate()
656     {
657     return date;
658     }
659    
660    
661     const wxString* WikiData::getText()
662     {
663     FILE* fp;
664     char buf[MAX_BUF_SIZE];
665     char fullPath[MAX_BUF_SIZE];
666 maloninc 1.11 iconv_t codeSet;
667     char outbuf[MAX_BUF_SIZE];
668     char* inbufPtr;
669     char* outbufPtr;
670     int inbufSize;
671     int outbufSize;
672 maloninc 1.1 wxCSConv conv(wxT(CODE_SET_SYSTEM));
673     wxString* tmpStr;
674    
675 maloninc 1.11 codeSet = iconv_open(CODE_SET_SYSTEM, CODE_SET_EUC_JP);
676     if(codeSet == (iconv_t)-1) {
677     MN_FATAL_ERROR(wxT("failed iconv_open"));
678     }
679    
680 maloninc 1.30 if(text && isWriteToFile == true) {
681 maloninc 1.29 delete text;
682     text = NULL;
683 maloninc 1.1 }
684 maloninc 1.30 else if(text && isWriteToFile == false) {
685     return text;
686     }
687 maloninc 1.1
688     text = new wxString();
689     sprintf(fullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str());
690     fp = fopen(fullPath, "r");
691     if(fp == NULL) {
692     MN_FATAL_ERROR(wxT("File open error."));
693     }
694    
695     while(fgets(buf, MAX_BUF_SIZE, fp)) {
696 maloninc 1.11 inbufPtr = buf;
697     inbufSize = sizeof(buf);
698     outbufPtr = outbuf;
699     outbufSize = sizeof(outbuf);
700     memset(outbuf, 0, outbufSize);
701     iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
702     tmpStr = new wxString((char*)outbuf, conv);
703 maloninc 1.1 *text += *tmpStr;
704     delete tmpStr;
705     }
706 maloninc 1.11 iconv_close(codeSet);
707 maloninc 1.1 fclose(fp);
708 maloninc 1.11
709 maloninc 1.1 return text;
710     }
711    
712     void WikiData::modText(wxString* intext)
713     {
714 maloninc 1.3 wxCSConv conv(wxT(CODE_SET_SYSTEM));
715 maloninc 1.1 delete text;
716 maloninc 1.3 //text = new wxString(intext->c_str());
717 maloninc 1.24 //text = new wxString(*intext);
718     text = new wxString(intext->mb_str(), conv);
719 maloninc 1.1 }
720    
721     void WikiData::removeDataFile()
722     {
723     char fullPath[MAX_BUF_SIZE];
724    
725     sprintf(fullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str());
726     if(remove(fullPath)) {
727     MN_FATAL_ERROR(wxT("remove file error"));
728     }
729     }
730    
731     void WikiData::save()
732     {
733     char fullPath[MAX_BUF_SIZE];
734     FILE* fp;
735     iconv_t codeSet;
736 maloninc 1.29 char* inbuf;
737     char* outbuf;
738     const char* inbufPtr;
739 maloninc 1.1 char* outbufPtr;
740     int inbufSize;
741     int outbufSize;
742    
743 maloninc 1.29 inbuf = (char*)malloc(MAX_WIKI_TEXT_SIZE);
744     outbuf = (char*)malloc(MAX_WIKI_TEXT_SIZE);
745     if(inbuf == NULL || outbuf == NULL) {
746     MN_FATAL_ERROR(wxT("It's too big data. Max size is 5MB."));
747     }
748    
749 maloninc 1.1 codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);
750     if(codeSet == (iconv_t)-1) {
751     MN_FATAL_ERROR(wxT("failed iconv_open"));
752     }
753    
754     sprintf(fullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str());
755     fp = fopen(fullPath, "wb");
756     if(fp == NULL) {
757     MN_FATAL_ERROR(wxT("File open error."));
758     }
759    
760 maloninc 1.29 memset(inbuf, 0, MAX_WIKI_TEXT_SIZE);
761 maloninc 1.1 strcpy(inbuf,(const char*)text->mb_str());
762 maloninc 1.12
763 maloninc 1.28 //#ifdef __WXMAC__
764     // for(int i = 0; inbuf[i] != 0; i++) if(inbuf[i] == (char)MAC_BACKSLASH) inbuf[i] = '\\';
765     //#endif
766 maloninc 1.12
767 maloninc 1.1 inbufPtr = inbuf;
768     inbufSize = strlen(inbufPtr);
769     outbufPtr = outbuf;
770 maloninc 1.29 outbufSize = MAX_WIKI_TEXT_SIZE;
771 maloninc 1.1 memset(outbuf, 0, outbufSize);
772 maloninc 1.5 iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
773 maloninc 1.26 if(inbufSize != 0) { // iconv error
774     wxMessageBox(wxT("Fail to save, because this memo include KISHU-IZON-MOJI.\nPlease remove KISHU-IZON-MOJI, and try again"), wxT("Fail to save"), wxOK|wxICON_WARNING);
775     }
776 maloninc 1.29 fwrite(outbuf, MAX_WIKI_TEXT_SIZE-outbufSize, 1, fp);
777 maloninc 1.1 fclose(fp);
778     iconv_close(codeSet);
779 maloninc 1.29
780     free(inbuf);
781     free(outbuf);
782 maloninc 1.30
783     isWriteToFile = true;
784 maloninc 1.1 }
785    
786     /******* Tools ************************/
787    
788 maloninc 1.8 static void toLower(char* string)
789     {
790     int i;
791    
792     for(i = 0; string[i] != 0; i++) {
793     string[i] = tolower(string[i]);
794     }
795     }
796    
797    
798 maloninc 1.1 static char* decode(const char* string)
799     {
800     static char buf[MAX_BUF_SIZE];
801     char c[5];
802     int i,j;
803     char* endPtr = NULL;
804    
805     j = 0;
806     memset(buf, 0, MAX_BUF_SIZE);
807     for(i = 0; string[i] != 0; i+=2) {
808     c[0] = '0'; c[1] = 'x';
809     c[2] = string[i]; c[3] = string[i+1]; c[4] = 0;
810     buf[j] = strtol(c, &endPtr, 0);
811     j++;
812     if(j >= MAX_BUF_SIZE) {
813     buf[MAX_BUF_SIZE] = 0;
814     break;
815     }
816     }
817    
818     return buf;
819     }
820    
821     static char* encode(const char* string)
822     {
823     static char buf[MAX_BUF_SIZE];
824     int i,j;
825    
826     j = 0;
827     memset(buf, 0, MAX_BUF_SIZE);
828     for(i = 0; string[i] != 0; i++) {
829     snprintf(&buf[j], 3, "%02X", (unsigned char)(string[i]));
830     j+=2;
831     if(j >= MAX_BUF_SIZE) {
832     buf[MAX_BUF_SIZE] = 0;
833     break;
834     }
835     }
836     return buf;
837     }
838    
839     /*
840     * wiki1 > wiki2 -> return < 0
841     * wiki1 = wiki2 -> return = 0
842     * wiki1 < wiki2 -> return > 0
843     */
844     static int compWikiData(const void* wiki1, const void* wiki2)
845     {
846     WikiData* data1 = *(WikiData**)wiki1;
847     WikiData* data2 = *(WikiData**)wiki2;
848     const wxString* str1 = NULL;
849     const wxString* str2 = NULL;
850    
851     str1 = data1->getDate();
852     str2 = data2->getDate();
853    
854     if(str1 != NULL && str2 != NULL) {
855     return (strcmp(str2->mb_str(), str1->mb_str()));
856     }
857     else{
858     return 0;
859     }
860     }
861    

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