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.25 - (hide annotations) (download) (as text)
Tue Sep 20 06:29:53 2005 UTC (18 years, 6 months ago) by maloninc
Branch: MAIN
CVS Tags: rel_1_3, rel_1_4, dev_1_3-0008, dev_1_3-0007, dev_1_5-0002, dev_1_5-0001, dev_1_4-0001, dev_1_4-0003, dev_1_4-0002, dev_1_4-0005, dev_1_4-0006
Changes since 1.24: +8 -2 lines
File MIME type: text/x-c++src
implement group by TYPE, and MAX_TOKEN=32

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

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