Develop and Download Open Source Software

Browse CVS Repository

Diff of /malonnote/mnModel.cpp

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

revision 1.1.1.1 by maloninc, Thu Jul 28 03:25:06 2005 UTC revision 1.36 by maloninc, Tue Oct 24 06:46:32 2006 UTC
# Line 2  Line 2 
2  #include "mnModel.h"  #include "mnModel.h"
3  #include <wx/dir.h>  #include <wx/dir.h>
4  #include <wx/regex.h>  #include <wx/regex.h>
5    #include <wx/progdlg.h>
6  #include <iconv.h>  #include <iconv.h>
7    #include <ctype.h>
8    
9  /* プロトタイプ */  static void   toLower(char* string);
10  static char*  decode(const char* string);  static char*  decode(const char* string);
11  static char*  encode(const char* string);  static char*  encode(const char* string);
12  static int compWikiData(const void* wiki1, const void* wiki2);  static int    compWikiData(const void* wiki1, const void* wiki2);
13    
14    
15  /******* WikiList ************************/  /******* WikiList ************************/
# Line 30  mnModel::~mnModel() Line 32  mnModel::~mnModel()
32          delete wikiDataDir;          delete wikiDataDir;
33  }  }
34    
35    /*
36     * iconv encode and create token list,
37     * so you must free tokenLis after you used
38     *
39     * if failed to make token list, return FALSE.
40     */
41    bool mnModel::makeSearchToken(const char* searchStr, char* tokenList[])
42    {
43            iconv_t     codeSet;
44            char        outbuf[MAX_BUF_SIZE];
45            const char* inbufPtr   = searchStr;
46            char*       outbufPtr  = outbuf;
47            int         inbufSize  = strlen(searchStr);
48            int         outbufSize = sizeof(outbuf);
49            char*       token;
50            int         i;
51    
52            memset(outbuf, 0, outbufSize);
53            codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);
54            if(codeSet == (iconv_t)-1) {
55                    MN_FATAL_ERROR(wxT("failed iconv_open"));
56            }
57            iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
58            iconv_close(codeSet);
59    
60            /* searchStr to Tokens */
61            token = strtok(outbuf, " ");
62            if(token == NULL) return false;
63            tokenList[0] = (char*)malloc(strlen(token)+1);
64            snprintf(tokenList[0], strlen(token)+1, "%s", token);
65            i = 1;
66            while((token = strtok(NULL, " ")) != NULL) {
67                    tokenList[i] = (char*)malloc(strlen(token)+1);
68                    snprintf(tokenList[i], strlen(token)+1, "%s", token);
69                    i++;
70                    if(i >= MAX_TOKEN) break;
71            }
72            return true;
73    }
74    
75    bool mnModel::matchWithToken(wxString* fileName, char* tokenList[])
76    {
77            const char* decodeFileName;
78            char decodeFileNameBuf[MAX_BUF_SIZE];
79        wxString    fullPathName;
80            FILE*       fp;
81            bool        ans = false;
82            bool        found;
83    
84        fullPathName = *wikiDataDir + wxT("/") + *fileName;
85            fp = fopen((const char*)fullPathName.mb_str(), "r");
86            if(fp == NULL) {
87                    return false;  /* because of removed */
88            }
89    
90            /* TYPE search */
91            if(strstr(tokenList[0], TYPESEARCH_TAG) == tokenList[0])
92            {
93                    found = typeSearch(tokenList[0], fp);
94                    if(found){
95                            ans = true;
96                    }
97            }
98            /* Normal search */
99            else{
100                    decodeFileName = decode(fileName->mb_str());
101                    snprintf(decodeFileNameBuf, MAX_BUF_SIZE, "%s", decodeFileName);
102                    toLower(decodeFileNameBuf);
103                    found = normalSearch(tokenList, fp, decodeFileNameBuf);
104                    if(found){
105                            ans = true;
106                    }
107            }
108            fclose(fp);
109    
110            return ans;
111    }
112    
113    void mnModel::readAll(bool progBarFlag)
114    {
115            int         fileCount = 0;
116            wxDir*      dir;
117            WikiData*   wikiData;
118        wxString*   fileName = new wxString();
119            char*       tokenList[MAX_TOKEN];
120            bool        cont;
121            wxProgressDialog* progBar = NULL;
122    
123            memset(tokenList, 0, sizeof(char*)*MAX_TOKEN);
124            tokenList[0] = "";
125    
126            dir = new wxDir(*wikiDataDir);
127        if ( !dir->IsOpened() )
128        {
129                    MN_FATAL_ERROR(wxT("wxDir has faild\n"));
130            return ;
131        }
132      
133            /* count number of files */
134            if (progBarFlag) {
135                    cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES);
136                    while(cont){
137                            fileCount++;
138                            cont = dir->GetNext(fileName);
139                    }
140                    progBar = new wxProgressDialog(wxT("Searching Data"), (wxT("Now searching... ") + *wikiDataDir), fileCount);
141            }
142    
143            fileCount = 0;
144        cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES);
145            while(cont){
146                    fileCount++;
147                    if (progBarFlag) progBar->Update(fileCount);
148                    matchWithToken(fileName, tokenList); /* match with token list, but never match, just read. */
149            cont = dir->GetNext(fileName);
150            }
151    
152            if (progBarFlag) {
153                    delete progBar;
154            }
155            delete dir;
156            delete fileName;
157    }
158    
159  WikiList* mnModel::search(const char* searchStr)  WikiList* mnModel::search(const char* searchStr)
160  {  {
161            int         i;
162          wxDir*      dir;          wxDir*      dir;
         FILE*       fp;  
     wxString    fullPathName;  
     char        buf[MAX_BUF_SIZE];  
163          WikiData*   wikiData;          WikiData*   wikiData;
164      WikiList*   list = new WikiList();      WikiList*   list = new WikiList();
165      wxString*   fileName = new wxString();      wxString*   fileName = new wxString();
166          wxRegEx*        regStr;          char*       tokenList[MAX_TOKEN];
167          iconv_t     codeSet;  
168            memset(tokenList, 0, sizeof(char*)*MAX_TOKEN);
169            if( makeSearchToken(searchStr, tokenList) == false) return list;
170    
171            dir = new wxDir(*wikiDataDir);
172        if ( !dir->IsOpened() )
173        {
174                    MN_FATAL_ERROR(wxT("wxDir has faild\n"));
175            return NULL;
176        }
177    
178        bool cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES);
179            while(cont){
180    
181                    if( matchWithToken(fileName, tokenList) ) { /* match with token list */
182                            wikiData = new WikiData(wikiDataDir, fileName);
183                            list->Append(wikiData);
184                    }
185    
186            cont = dir->GetNext(fileName);
187            }
188    
189            delete dir;
190            delete fileName;
191            for(i = 0; tokenList[i] != NULL; i++) free(tokenList[i]);
192    
193            list->Sort(compWikiData);
194            return list;
195    }
196    
197    /*
198     *  add malon-type:xxx to search list
199     */
200    void mnModel::group()
201    {
202            wxCSConv    conv(wxT(CODE_SET_SYSTEM));
203        char        buf[MAX_BUF_SIZE];
204            wxDir*      dir;
205            FILE*       fp;
206        wxString*   fileName = new wxString();
207        wxString    fullPathName;
208            int         typeTagLen = strlen(TYPE_TAG);
209            char*       token;
210            char*           inbufPtr;
211            int         inbufSize;
212          char        outbuf[MAX_BUF_SIZE];          char        outbuf[MAX_BUF_SIZE];
         const char*       inbufPtr  = searchStr;  
213          char*       outbufPtr = outbuf;          char*       outbufPtr = outbuf;
214          int         inbufSize = strlen(searchStr);          int         outbufSize;
215          int         outbufSize = sizeof(outbuf);          wxString*   typeToken;
216          const char* decodeFileName;          char*       ptr;
217            int         i;
218    
219          memset(outbuf, 0, outbufSize);          iconv_t codeSet = iconv_open(CODE_SET_SYSTEM, CODE_SET_EUC_JP);
         codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);  
220          if(codeSet == (iconv_t)-1) {          if(codeSet == (iconv_t)-1) {
221                  MN_FATAL_ERROR(wxT("failed iconv_open"));                  MN_FATAL_ERROR(wxT("failed iconv_open"));
222          }          }
         iconv(codeSet, (const char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);  
         iconv_close(codeSet);  
223    
224          dir = new wxDir(*wikiDataDir);          dir = new wxDir(*wikiDataDir);
225      if ( !dir->IsOpened() )      if ( !dir->IsOpened() )
226      {      {
227                  MN_FATAL_ERROR(wxT("wxDir has faild\n"));                  MN_FATAL_ERROR(wxT("wxDir has faild\n"));
228          return NULL;          return ;
229      }      }
230      bool cont = dir->GetFirst(fileName, wxEmptyString, wxDIR_FILES);      bool cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES);
231          while(cont){          while(cont){
232          fullPathName = *wikiDataDir + wxT("/") + *fileName;          fullPathName = *wikiDataDir + wxT("/") + *fileName;
233                  fp = fopen((const char*)fullPathName.mb_str(), "r");                  fp = fopen((const char*)fullPathName.mb_str(), "r");
234                  if(fp == NULL) {                  if(fp == NULL) {
235                          MN_FATAL_ERROR(wxT("fopen faild"));                          MN_FATAL_ERROR(wxT("fopen faild"));
236                  }                  }
237          while(1){                  while(1) {
238                  memset(buf, 0, MAX_BUF_SIZE);                          memset(buf, 0, MAX_BUF_SIZE);
239              fread(buf, MAX_BUF_SIZE-1, 1, fp);                          fgets(buf, MAX_BUF_SIZE, fp);
240              if(buf[0] == 0) break;                          if(buf[0] == 0) break;
241                          decodeFileName = decode(fileName->mb_str());                          if(strstr(buf, TYPE_TAG)){
242                  if(strstr((const char*)buf, (const char*)outbuf) ||                                  ptr = &buf[typeTagLen];
243                                  strstr((const char*)decodeFileName, (const char*)outbuf)) {                                  while((token = strtok(ptr, " /\r\n:"))!= NULL) {
244                                  wikiData = new WikiData(wikiDataDir, (const char*)fileName->mb_str(), fp);                                          memset(outbuf, 0, MAX_BUF_SIZE);
245                                  list->Append(wikiData);                                          snprintf(outbuf, MAX_BUF_SIZE, "%s", TYPESEARCH_TAG);
246                                  break;                                          inbufPtr = token;
247                                            inbufSize = strlen(token);
248                                            outbufPtr = &outbuf[strlen(TYPESEARCH_TAG)];
249                                            outbufSize = sizeof(outbuf) - strlen(TYPESEARCH_TAG);
250                                            iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
251    
252                                            typeToken = new wxString(outbuf, conv);
253                                            if( addSearchStr(typeToken) ) {
254                                                    WikiList* wikiList = search(typeToken->mb_str());
255                                                    addSearchList(typeToken, wikiList);
256                                            }
257                                            delete typeToken;
258                                            ptr = NULL;
259                                    }
260                          }                          }
             buf[0] = 0;  
261                  }                  }
262                  fclose(fp);                  fclose(fp);
263          cont = dir->GetNext(fileName);          cont = dir->GetNext(fileName);
264          }          }
265          delete dir;          delete dir;
266            delete fileName;
267            iconv_close(codeSet);
268    }
269    
270          list->Sort(compWikiData);  bool mnModel::normalSearch(char* tokenList[], FILE*fp, char* decodeFileNameBuf)
271          return list;  {
272        char        buf[MAX_BUF_SIZE];
273            bool        found;
274            int         i;
275            while(1){
276            memset(buf, 0, MAX_BUF_SIZE);
277            fread(buf, MAX_BUF_SIZE-1, 1, fp);
278            if(buf[0] == 0) break;
279                    toLower(buf);
280                    found = TRUE;
281                    for(i = 0; tokenList[i] != NULL; i++){
282                            toLower(tokenList[i]);
283                            if(strstr((const char*)buf, (const char*)tokenList[i]) ||                 /* search in file context */
284                                    strstr((const char*)decodeFileNameBuf, (const char*)tokenList[i]) ||  /* search in file name    */
285                                    strcmp((const char*)tokenList[i], (const char*)ALLMEMO_TAG) == 0) {   /* SHOW  ALL MEMO         */
286                                    found = TRUE;
287                            }
288                            else {
289                                    found = FALSE;
290                                    break;
291                            }
292                    }
293    
294                    if(found){ /* all tokens found */
295                            break;
296                    }
297            buf[0] = 0;
298            }
299    
300            return found;
301    }
302    
303    bool mnModel::typeSearch(char* typeStr, FILE*fp)
304    {
305        char        buf[MAX_BUF_SIZE];
306            bool        found;
307            int         i;
308            char*       typeToken;
309            char        typeStrCopy[MAX_BUF_SIZE];
310    
311            snprintf(typeStrCopy, MAX_BUF_SIZE, "%s", typeStr);
312            while(1){
313            memset(buf, 0, MAX_BUF_SIZE);
314            fgets(buf, MAX_BUF_SIZE, fp);
315            if(buf[0] == 0) break;
316                    if(strstr((const char*)buf, TYPE_TAG)){  /* search TYPE line */
317                            typeToken = strtok(typeStrCopy, ":");
318                            typeToken = strtok(NULL, ":");    /* second field separated by colon(:) */
319                            if(typeToken == NULL) return false;
320                            toLower(typeToken);
321                            toLower(buf);
322                            if(strstr(buf, typeToken)) return true;
323                    }
324            }
325            return false;
326  }  }
327    
328  void mnModel::addSearchStr(wxString* searchStr)  bool mnModel::addSearchStr(wxString* searchStr)
329  {  {
330          wxString *string;          wxString *string;
331    
332          if(searchStrList->Index(searchStr->c_str()) == wxNOT_FOUND){          if(searchStrList->Index(searchStr->c_str()) == wxNOT_FOUND){
333                  string = new wxString(searchStr->c_str());                  string = new wxString(searchStr->c_str());
334                  searchStrList->Add(*string, 1);                  searchStrList->Add(*string, 1);
335                    //searchStrList->Insert(*string, 0);
336                    return true;  /* Add */
337          }          }
338            return false; /* does'nt add because of duplicating */
339    }
340    
341    void mnModel::addSearchList(wxString* searchStr, WikiList* list)
342    {
343            wikiHash[*searchStr] = list;
344  }  }
345    
346  void mnModel::removeSearchStr(wxString searchStr)  void mnModel::removeSearchStr(wxString searchStr)
347  {  {
348          if(searchStrList->Index(searchStr.c_str()) != wxNOT_FOUND) {          if(searchStrList->Index(searchStr.c_str()) != wxNOT_FOUND) {
349                  searchStrList->Remove(searchStr.c_str());                  searchStrList->Remove(searchStr.c_str());
350                    wikiHash[*searchStr] = NULL;
351          }          }
352  }  }
353    
# Line 119  void mnModel::modSearchStr(wxString* old Line 361  void mnModel::modSearchStr(wxString* old
361          }          }
362  }  }
363    
364    void mnModel::clearSearchStrList()
365    {
366            searchStrList->Clear();
367    }
368    
369    void mnModel::clearSearchResultList()
370    {
371            wikiHash.clear();
372    }
373    
374    const wxString* mnModel::getWikiDataDir()
375    {
376            return wikiDataDir;
377    }
378    
379  const wxArrayString* mnModel::getSearchStrList()  const wxArrayString* mnModel::getSearchStrList()
380  {  {
381          return searchStrList;          return searchStrList;
# Line 130  WikiData* mnModel::newWikiData() Line 387  WikiData* mnModel::newWikiData()
387    
388          return data;          return data;
389  }  }
390    const WikiList* mnModel::getSearchResultList(wxString* searchStr)
391    {
392            return wikiHash[*searchStr];
393    }
394    
395    /* add "addData's" clone. if already exist same data, overwrite it*/
396    void mnModel::addSearchResultList(wxString* searchStr, WikiData* addData)
397    {
398            WikiList* wikiList = wikiHash[*searchStr];
399            WikiList::Node* node;
400            WikiData* data;
401    
402            if(wikiList == NULL) {
403                    MN_FATAL_ERROR(wxT("wikiList is null"));
404                    return;
405            }
406    
407            node = wikiList->GetFirst();
408            while(node) {
409                    data = node->GetData();
410                    if(data == addData) return;
411                    if( *(data->getSubject()) == *(addData->getOldSubject()) ) {
412                            if(wikiList->DeleteObject(data)) {
413                                    //delete data; /* may be wrong.. */
414                                    break;
415                            }
416                            else {
417                                    MN_FATAL_ERROR(wxT("Can't find delete data"));
418                            }
419                    }
420                    node = node->GetNext();
421            }
422            WikiData* copy = new WikiData((wxString*)wikiDataDir, (wxString*)addData->getFileName());
423            wikiList->Append(copy);
424            wikiList->Sort(compWikiData);
425    }
426    
427    bool mnModel::delSearchResultList(wxString* searchStr, WikiData* delData)
428    {
429            WikiList* wikiList = wikiHash[*searchStr];
430            WikiList::Node* node;
431            WikiData* data;
432            bool      found = false;
433    
434            if(wikiList == NULL) {
435                    MN_FATAL_ERROR(wxT("wikiList is null"));
436                    return false;
437            }
438    
439            node = wikiList->GetFirst();
440            while(node) {
441                    data = node->GetData();
442                    if(data == delData) {
443                            if(!wikiList->DeleteObject(data)) {
444                                    MN_FATAL_ERROR(wxT("Can't find delete data"));
445                            }
446                            found = true;
447                            break;
448                    }
449                    else if( *(data->getSubject()) == *(delData->getOldSubject()) ) {
450                            if(wikiList->DeleteObject(data)) {
451                                    //delete data;  /* may be wrong.. */
452                                    found = true;
453                                    break;
454                            }
455                            else {
456                                    MN_FATAL_ERROR(wxT("Can't find delete data"));
457                            }
458                    }
459                    node = node->GetNext();
460            }
461            wikiList->Sort(compWikiData);
462            return found;
463    }
464    
465  /******* WikiData ************************/  /******* WikiData ************************/
466  WikiData::WikiData(wxString* dataDir, const char* file, FILE* fp)  WikiData::WikiData(wxString* dataDir, wxString* inFileName)
467  {  {
468            FILE* fp;
469        wxString    fullPathName;
470          char* decodeStr;          char* decodeStr;
471          char  buf[MAX_BUF_SIZE];          char  buf[MAX_BUF_SIZE];
472          char* inbuf;          char* inbuf;
# Line 150  WikiData::WikiData(wxString* dataDir, co Line 483  WikiData::WikiData(wxString* dataDir, co
483    
484          text = NULL;          text = NULL;
485          memset(outbuf, 0, MAX_BUF_SIZE);          memset(outbuf, 0, MAX_BUF_SIZE);
486          fileName = new wxString((const char*)file, conv);          fileName = new wxString(inFileName->mb_str(), conv);
487          dataDirName = dataDir;          dataDirName = new wxString(dataDir->mb_str(), conv);
488          decodeStr = decode(file);          decodeStr = decode(fileName->mb_str());
489          inbufSize = strlen(decodeStr);          inbufSize = strlen(decodeStr);
490          outbufSize = sizeof(outbuf);          outbufSize = sizeof(outbuf);
491          iconv(codeSet, (const char**)&decodeStr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);          iconv(codeSet, (ICONV_CONST char**)&decodeStr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
492          subject  = new wxString((const char*)outbuf, conv);          subject  = new wxString((const char*)outbuf, conv);
493            oldSubject  = new wxString((const char*)outbuf, conv);
494          iconv_close(codeSet);          iconv_close(codeSet);
495    
496          date     = NULL;          date     = NULL;
497    
498      rewind(fp);      fullPathName = *dataDir + wxT("/") + *fileName;
499            fp = fopen((const char*)fullPathName.mb_str(), "r");
500            if(fp == NULL) {
501                    MN_FATAL_ERROR(wxT("fopen faild"));
502            }
503          while(fgets(buf, MAX_BUF_SIZE, fp)) {          while(fgets(buf, MAX_BUF_SIZE, fp)) {
504                  if(strstr( (const char*)buf, (const char*)DATE_TAG)) {                  if(strstr( (const char*)buf, (const char*)DATE_TAG)) {
505                          strtok(buf, "\n");                          strtok(buf, "\n");
# Line 170  WikiData::WikiData(wxString* dataDir, co Line 508  WikiData::WikiData(wxString* dataDir, co
508                          break;                          break;
509                  }                  }
510          }          }
511            fclose(fp);
512    
513            isWriteToFile = true;
514  }  }
515    
516  WikiData::WikiData(wxString* dataDir) {  WikiData::WikiData(wxString* dataDir) {
517            FILE*      fp;
518          time_t     now;          time_t     now;
519          char       buf[MAX_BUF_SIZE];          char       buf[MAX_BUF_SIZE];
520          wxCSConv    conv(wxT(CODE_SET_SYSTEM));          char       fname[MAX_BUF_SIZE];
521            char       templateBuf[MAX_BUF_SIZE];
522            char*      inbufPtr;
523            int        inbufSize;
524            char       outbuf[MAX_BUF_SIZE];
525            char*      outbufPtr;
526            int        outbufSize;
527            wxCSConv   conv(wxT(CODE_SET_SYSTEM));
528    
529          dataDirName = dataDir;          dataDirName = new wxString(dataDir->mb_str(), conv);
530    
531      time(&now);      time(&now);
532          memset(buf, 0, sizeof(buf));          memset(buf, 0, sizeof(buf));
533          strftime(buf, sizeof(buf), "%Y/%m/%d-%H%M%S",localtime(&now));          strftime(buf, sizeof(buf), "%Y/%m/%d-%H%M%S",localtime(&now));
534          subject  = new wxString(buf, conv);          subject  = new wxString(buf, conv);
535            oldSubject  = new wxString(buf, conv);
536    
537          fileName = new wxString(encode(buf), conv);          fileName = new wxString(encode(buf), conv);
538          fileName->Append(wxT(EXT_TAG));          fileName->Append(wxT(EXT_TAG));
# Line 190  WikiData::WikiData(wxString* dataDir) { Line 540  WikiData::WikiData(wxString* dataDir) {
540          memset(buf, 0, sizeof(buf));          memset(buf, 0, sizeof(buf));
541          strftime(buf, sizeof(buf), DATE_TAG "%Y/%m/%d %H:%M:%S",localtime(&now));          strftime(buf, sizeof(buf), DATE_TAG "%Y/%m/%d %H:%M:%S",localtime(&now));
542          date    = new wxString(buf, conv);          date    = new wxString(buf, conv);
543    
544            /* try to open template file */
545            snprintf(fname, sizeof(fname), "%s/%s", (const char*)(dataDir->mb_str()), NEW_DATA_TEMPLATE);
546            fp = fopen(fname, "r");
547            if(fp == NULL){
548                    memset(buf, 0, sizeof(buf));
549                    strftime(buf, sizeof(buf), NEW_DATA,localtime(&now));
550            }
551            else {
552                    memset(buf, 0, sizeof(buf));
553                    memset(templateBuf, 0, sizeof(templateBuf));
554                    memset(outbuf, 0, sizeof(outbuf));
555                    fread(templateBuf, sizeof(templateBuf), 1, fp);
556                    
557          memset(buf, 0, sizeof(buf));                  iconv_t codeSet = iconv_open(CODE_SET_SYSTEM, CODE_SET_EUC_JP);
558          strftime(buf, sizeof(buf), NEW_DATA,localtime(&now));                  if(codeSet == (iconv_t)-1) {
559                            MN_FATAL_ERROR(wxT("failed iconv_open"));
560                    }
561                    inbufPtr = templateBuf;
562                outbufPtr = outbuf;
563                    inbufSize = strlen(templateBuf);
564                    outbufSize = sizeof(outbuf);
565                    iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
566                    
567                    strftime(buf, sizeof(buf), outbuf,localtime(&now));
568    
569            }
570          text    = new wxString(buf, conv);          text    = new wxString(buf, conv);
571    
572            if(fp) fclose(fp);
573    
574            isWriteToFile = false;
575  }  }
576    
577  WikiData::~WikiData()  WikiData::~WikiData()
578  {  {
579          delete subject;          delete subject;
580            delete oldSubject;
581          delete fileName;          delete fileName;
582          delete date;          delete date;
583          delete text;          delete text;
584            delete dataDirName;
585  }  }
586    
587  const wxString* WikiData::getFileName()  const wxString* WikiData::getFileName()
# Line 215  const wxString* WikiData::getSubject() Line 594  const wxString* WikiData::getSubject()
594          return subject;          return subject;
595  }  }
596    
597    const wxString* WikiData::getOldSubject()
598    {
599            return oldSubject;
600    }
601    
602    void WikiData::setOldSubjectFromCurrent()
603    {
604            oldSubject = new wxString(*subject);
605    }
606    
607  void WikiData::modSubject(wxString* newSubject)  void WikiData::modSubject(wxString* newSubject)
608  {  {
609          wxCSConv    conv(wxT(CODE_SET_SYSTEM));          wxCSConv    conv(wxT(CODE_SET_SYSTEM));
610          wxString*   oldFileName = fileName;          wxString*   oldFileName = fileName;
         wxString*   oldSubject  = subject;  
611          char        oldFullPath[MAX_BUF_SIZE];          char        oldFullPath[MAX_BUF_SIZE];
612          char        newFullPath[MAX_BUF_SIZE];          char        newFullPath[MAX_BUF_SIZE];
613          iconv_t     codeSet;          iconv_t     codeSet;
# Line 231  void WikiData::modSubject(wxString* newS Line 619  void WikiData::modSubject(wxString* newS
619          int         outbufSize = sizeof(outbuf);          int         outbufSize = sizeof(outbuf);
620          FILE*       fp;          FILE*       fp;
621    
622            oldSubject = subject;
623    
624          memset(outbuf, 0, outbufSize);          memset(outbuf, 0, outbufSize);
625          memset(inbuf,  0, sizeof(inbuf));          memset(inbuf,  0, sizeof(inbuf));
626          strcpy(inbuf, (const char*)newSubject->mb_str());          strcpy(inbuf, (const char*)newSubject->mb_str());
# Line 239  void WikiData::modSubject(wxString* newS Line 629  void WikiData::modSubject(wxString* newS
629          if(codeSet == (iconv_t)-1) {          if(codeSet == (iconv_t)-1) {
630                  MN_FATAL_ERROR(wxT("failed iconv_open"));                  MN_FATAL_ERROR(wxT("failed iconv_open"));
631          }          }
632          iconv(codeSet, (const char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);          iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
633          iconv_close(codeSet);          iconv_close(codeSet);
634          subject  = new wxString(newSubject->c_str());          subject  = new wxString(newSubject->c_str());
635          fileName = new wxString(encode(outbuf), conv);          fileName = new wxString(encode(outbuf), conv);
# Line 249  void WikiData::modSubject(wxString* newS Line 639  void WikiData::modSubject(wxString* newS
639          sprintf(newFullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str());          sprintf(newFullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str());
640    
641          if((fp = fopen(newFullPath, "r")) == NULL) {          if((fp = fopen(newFullPath, "r")) == NULL) {
642                  rename(oldFullPath, newFullPath);                  if(rename(oldFullPath, newFullPath) < 0) wxLogMessage(wxT("rename error: errno=[%d]"), errno);
643          }          }
644          else {          else if(strcmp(oldFullPath, newFullPath)){
645                  wxLogMessage(wxT("File has already exist. [%s]"), fileName->c_str());                  wxLogMessage(wxT("File has already exist. [%s]"), fileName->c_str());
646                  fclose(fp);                  fclose(fp);
647          }          }
648            else {
649                    fclose(fp);
650            }
651    
         delete oldSubject;  
652          delete oldFileName;          delete oldFileName;
653  }  }
654    
# Line 285  const wxString* WikiData::getText() Line 677  const wxString* WikiData::getText()
677                  MN_FATAL_ERROR(wxT("failed iconv_open"));                  MN_FATAL_ERROR(wxT("failed iconv_open"));
678          }          }
679    
680          if(text) {          if(text && isWriteToFile == true) {
681                  iconv_close(codeSet);                  delete text;
682                    text = NULL;
683            }
684            else if(text && isWriteToFile == false) {
685                  return text;                  return text;
686          }          }
687    
# Line 303  const wxString* WikiData::getText() Line 698  const wxString* WikiData::getText()
698                  outbufPtr = outbuf;                  outbufPtr = outbuf;
699                  outbufSize = sizeof(outbuf);                  outbufSize = sizeof(outbuf);
700                  memset(outbuf, 0, outbufSize);                  memset(outbuf, 0, outbufSize);
701                  iconv(codeSet, (const char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);                  iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
702                  tmpStr = new wxString((char*)outbuf, conv);                  tmpStr = new wxString((char*)outbuf, conv);
703                  *text += *tmpStr;                  *text += *tmpStr;
704                  delete tmpStr;                  delete tmpStr;
# Line 316  const wxString* WikiData::getText() Line 711  const wxString* WikiData::getText()
711    
712  void WikiData::modText(wxString* intext)  void WikiData::modText(wxString* intext)
713  {  {
714            wxCSConv conv(wxT(CODE_SET_SYSTEM));
715          delete text;          delete text;
716          text = new wxString(intext->c_str());          //text = new wxString(intext->c_str());
717            //text = new wxString(*intext);
718            text = new wxString(intext->mb_str(), conv);
719  }  }
720    
721  void WikiData::removeDataFile()  void WikiData::removeDataFile()
# Line 335  void WikiData::save() Line 733  void WikiData::save()
733          char fullPath[MAX_BUF_SIZE];          char fullPath[MAX_BUF_SIZE];
734          FILE* fp;          FILE* fp;
735          iconv_t     codeSet;          iconv_t     codeSet;
736          char        inbuf[MAX_WIKI_TEXT_SIZE];          char*        inbuf;
737          char        outbuf[MAX_WIKI_TEXT_SIZE];          char*        outbuf;
738          const char*       inbufPtr;          const char*  inbufPtr;
739          char*       outbufPtr;          char*       outbufPtr;
740          int         inbufSize;          int         inbufSize;
741          int         outbufSize;          int         outbufSize;
742    
743            inbuf  = (char*)malloc(MAX_WIKI_TEXT_SIZE);
744            outbuf = (char*)malloc(MAX_WIKI_TEXT_SIZE);
745            if(inbuf == NULL || outbuf == NULL) {
746                    MN_FATAL_ERROR(wxT("It's too big data. Max size is 5MB."));
747            }
748    
749          codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);          codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);
750          if(codeSet == (iconv_t)-1) {          if(codeSet == (iconv_t)-1) {
751                  MN_FATAL_ERROR(wxT("failed iconv_open"));                  MN_FATAL_ERROR(wxT("failed iconv_open"));
# Line 353  void WikiData::save() Line 757  void WikiData::save()
757                  MN_FATAL_ERROR(wxT("File open error."));                  MN_FATAL_ERROR(wxT("File open error."));
758          }          }
759    
760          memset(inbuf, 0, sizeof(inbuf));          memset(inbuf, 0, MAX_WIKI_TEXT_SIZE);
761          strcpy(inbuf,(const char*)text->mb_str());          strcpy(inbuf,(const char*)text->mb_str());
762    
763    //#ifdef __WXMAC__
764    //      for(int i = 0; inbuf[i] != 0; i++) if(inbuf[i] == (char)MAC_BACKSLASH) inbuf[i] = '\\';
765    //#endif
766    
767          inbufPtr = inbuf;          inbufPtr = inbuf;
768          inbufSize = strlen(inbufPtr);          inbufSize = strlen(inbufPtr);
769          outbufPtr = outbuf;          outbufPtr = outbuf;
770          outbufSize = sizeof(outbuf);          outbufSize = MAX_WIKI_TEXT_SIZE;
771          memset(outbuf, 0, outbufSize);          memset(outbuf, 0, outbufSize);
772          iconv(codeSet, (const char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);          iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
773          fwrite(outbuf, sizeof(outbuf)-outbufSize, 1, fp);          if(inbufSize != 0) { // iconv error
774                    wxMessageBox(wxT("Fail to save, because this memo include KISHU-IZON-MOJI.\nPlease remove KISHU-IZON-MOJI, and try again"), wxT("Fail to save"), wxOK|wxICON_WARNING);
775            }
776            fwrite(outbuf, MAX_WIKI_TEXT_SIZE-outbufSize, 1, fp);
777          fclose(fp);          fclose(fp);
778          iconv_close(codeSet);          iconv_close(codeSet);
779            
780            free(inbuf);
781            free(outbuf);
782            
783            isWriteToFile = true;
784  }  }
785    
786  /******* Tools ************************/  /******* Tools ************************/
787    
788    static void toLower(char* string)
789    {
790            int i;
791    
792            for(i = 0; string[i] != 0; i++) {
793                    string[i] = tolower(string[i]);
794            }
795    }
796    
797    
798  static char* decode(const char* string)  static char* decode(const char* string)
799  {  {
800          static char buf[MAX_BUF_SIZE];          static char buf[MAX_BUF_SIZE];

Legend:
Removed from v.1.1.1.1  
changed lines
  Added in v.1.36

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