Develop and Download Open Source Software

Browse CVS Repository

Contents of /malonnote/mnModel.cpp

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


Revision 1.35 - (show annotations) (download) (as text)
Tue Oct 24 05:53:29 2006 UTC (17 years, 4 months ago) by maloninc
Branch: MAIN
Changes since 1.34: +4 -2 lines
File MIME type: text/x-c++src
fixed crash of readAll, and remove strip command from makefile

1 #include "mnDef.h"
2 #include "mnModel.h"
3 #include <wx/dir.h>
4 #include <wx/regex.h>
5 #include <wx/progdlg.h>
6 #include <iconv.h>
7 #include <ctype.h>
8
9 static void toLower(char* string);
10 static char* decode(const char* string);
11 static char* encode(const char* string);
12 static int compWikiData(const void* wiki1, const void* wiki2);
13
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 /*
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 {
43 iconv_t codeSet;
44 char outbuf[MAX_BUF_SIZE];
45 const char* inbufPtr = searchStr;
46 char* outbufPtr = outbuf;
47 int inbufSize = strlen(searchStr);
48 int outbufSize = sizeof(outbuf);
49 char* token;
50 int i;
51
52 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
60 /* searchStr to Tokens */
61 token = strtok(outbuf, " ");
62 if(token == NULL) return false;
63 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 if(i >= MAX_TOKEN) break;
71 }
72 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 return false; /* because of removed */
88 }
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 void mnModel::readAll(bool progBarFlag)
114 {
115 int fileCount = 0;
116 wxDir* dir;
117 WikiData* wikiData;
118 wxString* fileName = new wxString();
119 char* tokenList[MAX_TOKEN];
120 bool cont;
121 wxProgressDialog* progBar = NULL;
122
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
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 progBar = new wxProgressDialog(wxT("Loading Data"), (wxT("Now loading... ") + *wikiDataDir), fileCount);
141 }
142
143 fileCount = 0;
144 cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES);
145 while(cont){
146 fileCount++;
147 if (progBarFlag) progBar->Update(fileCount);
148 matchWithToken(fileName, tokenList); /* match with token list, but never match, just read. */
149 cont = dir->GetNext(fileName);
150 }
151
152 if (progBarFlag) {
153 delete progBar;
154 }
155 delete dir;
156 delete fileName;
157 }
158
159 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 char* tokenList[MAX_TOKEN];
167
168 memset(tokenList, 0, sizeof(char*)*MAX_TOKEN);
169 if( makeSearchToken(searchStr, tokenList) == false) return list;
170
171 dir = new wxDir(*wikiDataDir);
172 if ( !dir->IsOpened() )
173 {
174 MN_FATAL_ERROR(wxT("wxDir has faild\n"));
175 return NULL;
176 }
177
178 bool cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES);
179 while(cont){
180
181 if( matchWithToken(fileName, tokenList) ) { /* match with token list */
182 wikiData = new WikiData(wikiDataDir, fileName);
183 list->Append(wikiData);
184 }
185
186 cont = dir->GetNext(fileName);
187 }
188
189 delete dir;
190 delete fileName;
191 for(i = 0; tokenList[i] != NULL; i++) free(tokenList[i]);
192
193 list->Sort(compWikiData);
194 return list;
195 }
196
197 /*
198 * add malon-type:xxx to search list
199 */
200 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 if( addSearchStr(typeToken) ) {
254 WikiList* wikiList = search(typeToken->mb_str());
255 addSearchList(typeToken, wikiList);
256 }
257 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 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 if(typeToken == NULL) return false;
320 toLower(typeToken);
321 toLower(buf);
322 if(strstr(buf, typeToken)) return true;
323 }
324 }
325 return false;
326 }
327
328 bool mnModel::addSearchStr(wxString* searchStr)
329 {
330 wxString *string;
331
332 if(searchStrList->Index(searchStr->c_str()) == wxNOT_FOUND){
333 string = new wxString(searchStr->c_str());
334 searchStrList->Add(*string, 1);
335 //searchStrList->Insert(*string, 0);
336 return true; /* Add */
337 }
338 return false; /* does'nt add because of duplicating */
339 }
340
341 void mnModel::addSearchList(wxString* searchStr, WikiList* list)
342 {
343 wikiHash[*searchStr] = list;
344 }
345
346 void mnModel::removeSearchStr(wxString searchStr)
347 {
348 if(searchStrList->Index(searchStr.c_str()) != wxNOT_FOUND) {
349 searchStrList->Remove(searchStr.c_str());
350 wikiHash[*searchStr] = NULL;
351 }
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
364 void mnModel::clearSearchStrList()
365 {
366 searchStrList->Clear();
367 }
368
369 void mnModel::clearSearchResultList()
370 {
371 wikiHash.clear();
372 }
373
374 const wxString* mnModel::getWikiDataDir()
375 {
376 return wikiDataDir;
377 }
378
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 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 if(wikiList == NULL) {
403 MN_FATAL_ERROR(wxT("wikiList is null"));
404 return;
405 }
406
407 node = wikiList->GetFirst();
408 while(node) {
409 data = node->GetData();
410 if(data == addData) return;
411 if( *(data->getSubject()) == *(addData->getOldSubject()) ) {
412 if(wikiList->DeleteObject(data)) {
413 //delete data; /* may be wrong.. */
414 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
427 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 /******* WikiData ************************/
466 WikiData::WikiData(wxString* dataDir, wxString* inFileName)
467 {
468 FILE* fp;
469 wxString fullPathName;
470 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 fileName = new wxString(inFileName->mb_str(), conv);
487 dataDirName = new wxString(dataDir->mb_str(), conv);
488 decodeStr = decode(fileName->mb_str());
489 inbufSize = strlen(decodeStr);
490 outbufSize = sizeof(outbuf);
491 iconv(codeSet, (ICONV_CONST char**)&decodeStr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
492 subject = new wxString((const char*)outbuf, conv);
493 oldSubject = new wxString((const char*)outbuf, conv);
494 iconv_close(codeSet);
495
496 date = NULL;
497
498 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 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 fclose(fp);
512
513 isWriteToFile = true;
514 }
515
516 WikiData::WikiData(wxString* dataDir) {
517 FILE* fp;
518 time_t now;
519 char buf[MAX_BUF_SIZE];
520 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
529 dataDirName = new wxString(dataDir->mb_str(), conv);
530
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 oldSubject = new wxString(buf, conv);
536
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
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
557 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 text = new wxString(buf, conv);
571
572 if(fp) fclose(fp);
573
574 isWriteToFile = false;
575 }
576
577 WikiData::~WikiData()
578 {
579 delete subject;
580 delete oldSubject;
581 delete fileName;
582 delete date;
583 delete text;
584 delete dataDirName;
585 }
586
587 const wxString* WikiData::getFileName()
588 {
589 return fileName;
590 }
591
592 const wxString* WikiData::getSubject()
593 {
594 return subject;
595 }
596
597 const wxString* WikiData::getOldSubject()
598 {
599 return oldSubject;
600 }
601
602 void WikiData::setOldSubjectFromCurrent()
603 {
604 oldSubject = new wxString(*subject);
605 }
606
607 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 oldSubject = subject;
623
624 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 iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
633 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 if(rename(oldFullPath, newFullPath) < 0) wxLogMessage(wxT("rename error: errno=[%d]"), errno);
643 }
644 else if(strcmp(oldFullPath, newFullPath)){
645 wxLogMessage(wxT("File has already exist. [%s]"), fileName->c_str());
646 fclose(fp);
647 }
648 else {
649 fclose(fp);
650 }
651
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 iconv_t codeSet;
667 char outbuf[MAX_BUF_SIZE];
668 char* inbufPtr;
669 char* outbufPtr;
670 int inbufSize;
671 int outbufSize;
672 wxCSConv conv(wxT(CODE_SET_SYSTEM));
673 wxString* tmpStr;
674
675 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 if(text && isWriteToFile == true) {
681 delete text;
682 text = NULL;
683 }
684 else if(text && isWriteToFile == false) {
685 return text;
686 }
687
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 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 *text += *tmpStr;
704 delete tmpStr;
705 }
706 iconv_close(codeSet);
707 fclose(fp);
708
709 return text;
710 }
711
712 void WikiData::modText(wxString* intext)
713 {
714 wxCSConv conv(wxT(CODE_SET_SYSTEM));
715 delete text;
716 //text = new wxString(intext->c_str());
717 //text = new wxString(*intext);
718 text = new wxString(intext->mb_str(), conv);
719 }
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 char* inbuf;
737 char* outbuf;
738 const char* inbufPtr;
739 char* outbufPtr;
740 int inbufSize;
741 int outbufSize;
742
743 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 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 memset(inbuf, 0, MAX_WIKI_TEXT_SIZE);
761 strcpy(inbuf,(const char*)text->mb_str());
762
763 //#ifdef __WXMAC__
764 // for(int i = 0; inbuf[i] != 0; i++) if(inbuf[i] == (char)MAC_BACKSLASH) inbuf[i] = '\\';
765 //#endif
766
767 inbufPtr = inbuf;
768 inbufSize = strlen(inbufPtr);
769 outbufPtr = outbuf;
770 outbufSize = MAX_WIKI_TEXT_SIZE;
771 memset(outbuf, 0, outbufSize);
772 iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
773 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 fwrite(outbuf, MAX_WIKI_TEXT_SIZE-outbufSize, 1, fp);
777 fclose(fp);
778 iconv_close(codeSet);
779
780 free(inbuf);
781 free(outbuf);
782
783 isWriteToFile = true;
784 }
785
786 /******* Tools ************************/
787
788 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 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