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.31 - (hide annotations) (download) (as text)
Tue Jan 3 11:24:44 2006 UTC (18 years, 2 months ago) by maloninc
Branch: MAIN
CVS Tags: rel_1_7_1
Changes since 1.30: +1 -0 lines
File MIME type: text/x-c++src
modify wxrc command option

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

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