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.33 - (hide annotations) (download) (as text)
Fri Oct 20 11:58:52 2006 UTC (17 years, 5 months ago) by maloninc
Branch: MAIN
Changes since 1.32: +20 -5 lines
File MIME type: text/x-c++src
readAll when started, and proressbar

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

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