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.32 - (show annotations) (download) (as text)
Fri Oct 20 09:36:08 2006 UTC (17 years, 5 months ago) by maloninc
Branch: MAIN
Changes since 1.31: +30 -0 lines
File MIME type: text/x-c++src
readAll

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

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