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.31 - (show 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 #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 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 char* tokenList[MAX_TOKEN];
120
121 memset(tokenList, 0, sizeof(char*)*MAX_TOKEN);
122 if( makeSearchToken(searchStr, tokenList) == false) return list;
123
124 dir = new wxDir(*wikiDataDir);
125 if ( !dir->IsOpened() )
126 {
127 MN_FATAL_ERROR(wxT("wxDir has faild\n"));
128 return NULL;
129 }
130
131 bool cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES);
132 while(cont){
133
134 if( matchWithToken(fileName, tokenList) ) { /* match with token list */
135 wikiData = new WikiData(wikiDataDir, fileName);
136 list->Append(wikiData);
137 }
138
139 cont = dir->GetNext(fileName);
140 }
141
142 delete dir;
143 delete fileName;
144 for(i = 0; tokenList[i] != NULL; i++) free(tokenList[i]);
145
146 list->Sort(compWikiData);
147 return list;
148 }
149
150 /*
151 * add malon-type:xxx to search list
152 */
153 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 if( addSearchStr(typeToken) ) {
207 WikiList* wikiList = search(typeToken->mb_str());
208 addSearchList(typeToken, wikiList);
209 }
210 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 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 if(typeToken == NULL) return false;
273 toLower(typeToken);
274 toLower(buf);
275 if(strstr(buf, typeToken)) return true;
276 }
277 }
278 return false;
279 }
280
281 bool mnModel::addSearchStr(wxString* searchStr)
282 {
283 wxString *string;
284
285 if(searchStrList->Index(searchStr->c_str()) == wxNOT_FOUND){
286 string = new wxString(searchStr->c_str());
287 searchStrList->Add(*string, 1);
288 //searchStrList->Insert(*string, 0);
289 return true; /* Add */
290 }
291 return false; /* does'nt add because of duplicating */
292 }
293
294 void mnModel::addSearchList(wxString* searchStr, WikiList* list)
295 {
296 wikiHash[*searchStr] = list;
297 }
298
299 void mnModel::removeSearchStr(wxString searchStr)
300 {
301 if(searchStrList->Index(searchStr.c_str()) != wxNOT_FOUND) {
302 searchStrList->Remove(searchStr.c_str());
303 wikiHash[*searchStr] = NULL;
304 }
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
317 void mnModel::clearSearchStrList()
318 {
319 searchStrList->Clear();
320 }
321
322 void mnModel::clearSearchResultList()
323 {
324 wikiHash.clear();
325 }
326
327 const wxString* mnModel::getWikiDataDir()
328 {
329 return wikiDataDir;
330 }
331
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 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 if(wikiList == NULL) {
356 MN_FATAL_ERROR(wxT("wikiList is null"));
357 return;
358 }
359
360 node = wikiList->GetFirst();
361 while(node) {
362 data = node->GetData();
363 if(data == addData) return;
364 if( *(data->getSubject()) == *(addData->getOldSubject()) ) {
365 if(wikiList->DeleteObject(data)) {
366 //delete data; /* may be wrong.. */
367 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
380 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 /******* WikiData ************************/
419 WikiData::WikiData(wxString* dataDir, wxString* inFileName)
420 {
421 FILE* fp;
422 wxString fullPathName;
423 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 fileName = new wxString(inFileName->mb_str(), conv);
440 dataDirName = new wxString(dataDir->mb_str(), conv);
441 decodeStr = decode(fileName->mb_str());
442 inbufSize = strlen(decodeStr);
443 outbufSize = sizeof(outbuf);
444 iconv(codeSet, (ICONV_CONST char**)&decodeStr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
445 subject = new wxString((const char*)outbuf, conv);
446 oldSubject = new wxString((const char*)outbuf, conv);
447 iconv_close(codeSet);
448
449 date = NULL;
450
451 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 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 fclose(fp);
465
466 isWriteToFile = true;
467 }
468
469 WikiData::WikiData(wxString* dataDir) {
470 FILE* fp;
471 time_t now;
472 char buf[MAX_BUF_SIZE];
473 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
482 dataDirName = new wxString(dataDir->mb_str(), conv);
483
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 oldSubject = new wxString(buf, conv);
489
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
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
510 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 text = new wxString(buf, conv);
524
525 if(fp) fclose(fp);
526
527 isWriteToFile = false;
528 }
529
530 WikiData::~WikiData()
531 {
532 delete subject;
533 delete oldSubject;
534 delete fileName;
535 delete date;
536 delete text;
537 delete dataDirName;
538 }
539
540 const wxString* WikiData::getFileName()
541 {
542 return fileName;
543 }
544
545 const wxString* WikiData::getSubject()
546 {
547 return subject;
548 }
549
550 const wxString* WikiData::getOldSubject()
551 {
552 return oldSubject;
553 }
554
555 void WikiData::setOldSubjectFromCurrent()
556 {
557 oldSubject = new wxString(*subject);
558 }
559
560 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 oldSubject = subject;
576
577 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 iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
586 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 if(rename(oldFullPath, newFullPath) < 0) wxLogMessage(wxT("rename error: errno=[%d]"), errno);
596 }
597 else if(strcmp(oldFullPath, newFullPath)){
598 wxLogMessage(wxT("File has already exist. [%s]"), fileName->c_str());
599 fclose(fp);
600 }
601 else {
602 fclose(fp);
603 }
604
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 iconv_t codeSet;
620 char outbuf[MAX_BUF_SIZE];
621 char* inbufPtr;
622 char* outbufPtr;
623 int inbufSize;
624 int outbufSize;
625 wxCSConv conv(wxT(CODE_SET_SYSTEM));
626 wxString* tmpStr;
627
628 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 if(text && isWriteToFile == true) {
634 delete text;
635 text = NULL;
636 }
637 else if(text && isWriteToFile == false) {
638 return text;
639 }
640
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 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 *text += *tmpStr;
657 delete tmpStr;
658 }
659 iconv_close(codeSet);
660 fclose(fp);
661
662 return text;
663 }
664
665 void WikiData::modText(wxString* intext)
666 {
667 wxCSConv conv(wxT(CODE_SET_SYSTEM));
668 delete text;
669 //text = new wxString(intext->c_str());
670 //text = new wxString(*intext);
671 text = new wxString(intext->mb_str(), conv);
672 }
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 char* inbuf;
690 char* outbuf;
691 const char* inbufPtr;
692 char* outbufPtr;
693 int inbufSize;
694 int outbufSize;
695
696 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 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 memset(inbuf, 0, MAX_WIKI_TEXT_SIZE);
714 strcpy(inbuf,(const char*)text->mb_str());
715
716 //#ifdef __WXMAC__
717 // for(int i = 0; inbuf[i] != 0; i++) if(inbuf[i] == (char)MAC_BACKSLASH) inbuf[i] = '\\';
718 //#endif
719
720 inbufPtr = inbuf;
721 inbufSize = strlen(inbufPtr);
722 outbufPtr = outbuf;
723 outbufSize = MAX_WIKI_TEXT_SIZE;
724 memset(outbuf, 0, outbufSize);
725 iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
726 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 fwrite(outbuf, MAX_WIKI_TEXT_SIZE-outbufSize, 1, fp);
730 fclose(fp);
731 iconv_close(codeSet);
732
733 free(inbuf);
734 free(outbuf);
735
736 isWriteToFile = true;
737 }
738
739 /******* Tools ************************/
740
741 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 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