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.11 by maloninc, Fri Aug 12 07:59:51 2005 UTC revision 1.31 by maloninc, Tue Jan 3 11:24:44 2006 UTC
# Line 3  Line 3 
3  #include <wx/dir.h>  #include <wx/dir.h>
4  #include <wx/regex.h>  #include <wx/regex.h>
5  #include <iconv.h>  #include <iconv.h>
6    #include <ctype.h>
7    
8  static void   toLower(char* string);  static void   toLower(char* string);
9  static char*  decode(const char* string);  static char*  decode(const char* string);
# Line 30  mnModel::~mnModel() Line 31  mnModel::~mnModel()
31          delete wikiDataDir;          delete wikiDataDir;
32  }  }
33    
34    /*
35  WikiList* mnModel::search(const char* searchStr)   * 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  {  {
         int         i;  
         wxDir*      dir;  
         FILE*       fp;  
     wxString    fullPathName;  
     char        buf[MAX_BUF_SIZE];  
         WikiData*   wikiData;  
     WikiList*   list = new WikiList();  
     wxString*   fileName = new wxString();  
42          iconv_t     codeSet;          iconv_t     codeSet;
43          char        outbuf[MAX_BUF_SIZE];          char        outbuf[MAX_BUF_SIZE];
44          const char*       inbufPtr  = searchStr;          const char* inbufPtr   = searchStr;
45          char*       outbufPtr = outbuf;          char*       outbufPtr  = outbuf;
46          int         inbufSize = strlen(searchStr);          int         inbufSize  = strlen(searchStr);
47          int         outbufSize = sizeof(outbuf);          int         outbufSize = sizeof(outbuf);
         const char* decodeFileName;  
         char decodeFileNameBuf[MAX_BUF_SIZE];  
48          char*       token;          char*       token;
49          char*       tokenList[32];          int         i;
         bool        found;  
50    
         memset(tokenList, 0, sizeof(char*)*32);  
51          memset(outbuf, 0, outbufSize);          memset(outbuf, 0, outbufSize);
52          codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);          codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);
53          if(codeSet == (iconv_t)-1) {          if(codeSet == (iconv_t)-1) {
# Line 64  WikiList* mnModel::search(const char* se Line 58  WikiList* mnModel::search(const char* se
58    
59          /* searchStr to Tokens */          /* searchStr to Tokens */
60          token = strtok(outbuf, " ");          token = strtok(outbuf, " ");
61            if(token == NULL) return false;
62          tokenList[0] = (char*)malloc(strlen(token)+1);          tokenList[0] = (char*)malloc(strlen(token)+1);
63          snprintf(tokenList[0], strlen(token)+1, "%s", token);          snprintf(tokenList[0], strlen(token)+1, "%s", token);
64          i = 1;          i = 1;
# Line 71  WikiList* mnModel::search(const char* se Line 66  WikiList* mnModel::search(const char* se
66                  tokenList[i] = (char*)malloc(strlen(token)+1);                  tokenList[i] = (char*)malloc(strlen(token)+1);
67                  snprintf(tokenList[i], strlen(token)+1, "%s", token);                  snprintf(tokenList[i], strlen(token)+1, "%s", token);
68                  i++;                  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);          dir = new wxDir(*wikiDataDir);
125      if ( !dir->IsOpened() )      if ( !dir->IsOpened() )
# Line 79  WikiList* mnModel::search(const char* se Line 127  WikiList* mnModel::search(const char* se
127                  MN_FATAL_ERROR(wxT("wxDir has faild\n"));                  MN_FATAL_ERROR(wxT("wxDir has faild\n"));
128          return NULL;          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);      bool cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES);
184          while(cont){          while(cont){
185          fullPathName = *wikiDataDir + wxT("/") + *fileName;          fullPathName = *wikiDataDir + wxT("/") + *fileName;
# Line 86  WikiList* mnModel::search(const char* se Line 187  WikiList* mnModel::search(const char* se
187                  if(fp == NULL) {                  if(fp == NULL) {
188                          MN_FATAL_ERROR(wxT("fopen faild"));                          MN_FATAL_ERROR(wxT("fopen faild"));
189                  }                  }
190                    while(1) {
191          while(1){                          memset(buf, 0, MAX_BUF_SIZE);
192                  memset(buf, 0, MAX_BUF_SIZE);                          fgets(buf, MAX_BUF_SIZE, fp);
193              fread(buf, MAX_BUF_SIZE-1, 1, fp);                          if(buf[0] == 0) break;
194              if(buf[0] == 0) break;                          if(strstr(buf, TYPE_TAG)){
195                          decodeFileName = decode(fileName->mb_str());                                  ptr = &buf[typeTagLen];
196                          snprintf(decodeFileNameBuf, MAX_BUF_SIZE, "%s", decodeFileName);                                  while((token = strtok(ptr, " /\r\n:"))!= NULL) {
197                          toLower(buf);                                          memset(outbuf, 0, MAX_BUF_SIZE);
198                          toLower(outbuf);                                          snprintf(outbuf, MAX_BUF_SIZE, "%s", TYPESEARCH_TAG);
199                          toLower(decodeFileNameBuf);                                          inbufPtr = token;
200                          found = TRUE;                                          inbufSize = strlen(token);
201                          for(i = 0; tokenList[i] != NULL; i++){                                          outbufPtr = &outbuf[strlen(TYPESEARCH_TAG)];
202                                  if(strstr((const char*)buf, (const char*)tokenList[i]) ||                                          outbufSize = sizeof(outbuf) - strlen(TYPESEARCH_TAG);
203                                          strstr((const char*)decodeFileName, (const char*)tokenList[i])) {                                          iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
204                                          found = TRUE;  
205                                  }                                          typeToken = new wxString(outbuf, conv);
206                                  else {                                          if( addSearchStr(typeToken) ) {
207                                          found = FALSE;                                                  WikiList* wikiList = search(typeToken->mb_str());
208                                          break;                                                  addSearchList(typeToken, wikiList);
209                                            }
210                                            delete typeToken;
211                                            ptr = NULL;
212                                  }                                  }
213                          }                          }
   
                         if(found){  
                                 wikiData = new WikiData(wikiDataDir, (const char*)fileName->mb_str(), fp);  
                                 list->Append(wikiData);  
                                 break;  
                         }  
             buf[0] = 0;  
214                  }                  }
215                  fclose(fp);                  fclose(fp);
216          cont = dir->GetNext(fileName);          cont = dir->GetNext(fileName);
217          }          }
218          delete dir;          delete dir;
219          delete fileName;          delete fileName;
220          for(i = 0; tokenList[i] != NULL; i++) free(tokenList[i]);          iconv_close(codeSet);
221    }
222    
223          list->Sort(compWikiData);  bool mnModel::normalSearch(char* tokenList[], FILE*fp, char* decodeFileNameBuf)
224          return list;  {
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  void mnModel::addSearchStr(wxString* searchStr)  bool mnModel::addSearchStr(wxString* searchStr)
282  {  {
283          wxString *string;          wxString *string;
284    
285          if(searchStrList->Index(searchStr->c_str()) == wxNOT_FOUND){          if(searchStrList->Index(searchStr->c_str()) == wxNOT_FOUND){
286                  string = new wxString(searchStr->c_str());                  string = new wxString(searchStr->c_str());
287                  searchStrList->Add(*string, 1);                  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)  void mnModel::removeSearchStr(wxString searchStr)
300  {  {
301          if(searchStrList->Index(searchStr.c_str()) != wxNOT_FOUND) {          if(searchStrList->Index(searchStr.c_str()) != wxNOT_FOUND) {
302                  searchStrList->Remove(searchStr.c_str());                  searchStrList->Remove(searchStr.c_str());
303                    wikiHash[*searchStr] = NULL;
304          }          }
305  }  }
306    
# Line 152  void mnModel::modSearchStr(wxString* old Line 313  void mnModel::modSearchStr(wxString* old
313                  itemStr.sprintf(wxT("%s"), newStr->c_str());                  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()  const wxString* mnModel::getWikiDataDir()
328  {  {
329          return wikiDataDir;          return wikiDataDir;
# Line 168  WikiData* mnModel::newWikiData() Line 340  WikiData* mnModel::newWikiData()
340    
341          return data;          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 ************************/  /******* WikiData ************************/
419  WikiData::WikiData(wxString* dataDir, const char* file, FILE* fp)  WikiData::WikiData(wxString* dataDir, wxString* inFileName)
420  {  {
421            FILE* fp;
422        wxString    fullPathName;
423          char* decodeStr;          char* decodeStr;
424          char  buf[MAX_BUF_SIZE];          char  buf[MAX_BUF_SIZE];
425          char* inbuf;          char* inbuf;
# Line 188  WikiData::WikiData(wxString* dataDir, co Line 436  WikiData::WikiData(wxString* dataDir, co
436    
437          text = NULL;          text = NULL;
438          memset(outbuf, 0, MAX_BUF_SIZE);          memset(outbuf, 0, MAX_BUF_SIZE);
439          fileName = new wxString((const char*)file, conv);          fileName = new wxString(inFileName->mb_str(), conv);
440          dataDirName = dataDir;          dataDirName = new wxString(dataDir->mb_str(), conv);
441          decodeStr = decode(file);          decodeStr = decode(fileName->mb_str());
442          inbufSize = strlen(decodeStr);          inbufSize = strlen(decodeStr);
443          outbufSize = sizeof(outbuf);          outbufSize = sizeof(outbuf);
444          iconv(codeSet, (ICONV_CONST char**)&decodeStr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);          iconv(codeSet, (ICONV_CONST char**)&decodeStr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
445          subject  = new wxString((const char*)outbuf, conv);          subject  = new wxString((const char*)outbuf, conv);
446            oldSubject  = new wxString((const char*)outbuf, conv);
447          iconv_close(codeSet);          iconv_close(codeSet);
448    
449          date     = NULL;          date     = NULL;
450    
451      rewind(fp);      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)) {          while(fgets(buf, MAX_BUF_SIZE, fp)) {
457                  if(strstr( (const char*)buf, (const char*)DATE_TAG)) {                  if(strstr( (const char*)buf, (const char*)DATE_TAG)) {
458                          strtok(buf, "\n");                          strtok(buf, "\n");
# Line 208  WikiData::WikiData(wxString* dataDir, co Line 461  WikiData::WikiData(wxString* dataDir, co
461                          break;                          break;
462                  }                  }
463          }          }
464            fclose(fp);
465    
466            isWriteToFile = true;
467  }  }
468    
469  WikiData::WikiData(wxString* dataDir) {  WikiData::WikiData(wxString* dataDir) {
470            FILE*      fp;
471          time_t     now;          time_t     now;
472          char       buf[MAX_BUF_SIZE];          char       buf[MAX_BUF_SIZE];
473          wxCSConv    conv(wxT(CODE_SET_SYSTEM));          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 = dataDir;          dataDirName = new wxString(dataDir->mb_str(), conv);
483    
484      time(&now);      time(&now);
485          memset(buf, 0, sizeof(buf));          memset(buf, 0, sizeof(buf));
486          strftime(buf, sizeof(buf), "%Y/%m/%d-%H%M%S",localtime(&now));          strftime(buf, sizeof(buf), "%Y/%m/%d-%H%M%S",localtime(&now));
487          subject  = new wxString(buf, conv);          subject  = new wxString(buf, conv);
488            oldSubject  = new wxString(buf, conv);
489    
490          fileName = new wxString(encode(buf), conv);          fileName = new wxString(encode(buf), conv);
491          fileName->Append(wxT(EXT_TAG));          fileName->Append(wxT(EXT_TAG));
# Line 228  WikiData::WikiData(wxString* dataDir) { Line 493  WikiData::WikiData(wxString* dataDir) {
493          memset(buf, 0, sizeof(buf));          memset(buf, 0, sizeof(buf));
494          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));
495          date    = new wxString(buf, conv);          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          memset(buf, 0, sizeof(buf));                  iconv_t codeSet = iconv_open(CODE_SET_SYSTEM, CODE_SET_EUC_JP);
511          strftime(buf, sizeof(buf), NEW_DATA,localtime(&now));                  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);          text    = new wxString(buf, conv);
524    
525            if(fp) fclose(fp);
526    
527            isWriteToFile = false;
528  }  }
529    
530  WikiData::~WikiData()  WikiData::~WikiData()
531  {  {
532          delete subject;          delete subject;
533            delete oldSubject;
534          delete fileName;          delete fileName;
535          delete date;          delete date;
536          delete text;          delete text;
537            delete dataDirName;
538  }  }
539    
540  const wxString* WikiData::getFileName()  const wxString* WikiData::getFileName()
# Line 253  const wxString* WikiData::getSubject() Line 547  const wxString* WikiData::getSubject()
547          return subject;          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)  void WikiData::modSubject(wxString* newSubject)
561  {  {
562          wxCSConv    conv(wxT(CODE_SET_SYSTEM));          wxCSConv    conv(wxT(CODE_SET_SYSTEM));
563          wxString*   oldFileName = fileName;          wxString*   oldFileName = fileName;
         wxString*   oldSubject  = subject;  
564          char        oldFullPath[MAX_BUF_SIZE];          char        oldFullPath[MAX_BUF_SIZE];
565          char        newFullPath[MAX_BUF_SIZE];          char        newFullPath[MAX_BUF_SIZE];
566          iconv_t     codeSet;          iconv_t     codeSet;
# Line 269  void WikiData::modSubject(wxString* newS Line 572  void WikiData::modSubject(wxString* newS
572          int         outbufSize = sizeof(outbuf);          int         outbufSize = sizeof(outbuf);
573          FILE*       fp;          FILE*       fp;
574    
575            oldSubject = subject;
576    
577          memset(outbuf, 0, outbufSize);          memset(outbuf, 0, outbufSize);
578          memset(inbuf,  0, sizeof(inbuf));          memset(inbuf,  0, sizeof(inbuf));
579          strcpy(inbuf, (const char*)newSubject->mb_str());          strcpy(inbuf, (const char*)newSubject->mb_str());
# Line 287  void WikiData::modSubject(wxString* newS Line 592  void WikiData::modSubject(wxString* newS
592          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());
593    
594          if((fp = fopen(newFullPath, "r")) == NULL) {          if((fp = fopen(newFullPath, "r")) == NULL) {
595                  rename(oldFullPath, newFullPath);                  if(rename(oldFullPath, newFullPath) < 0) wxLogMessage(wxT("rename error: errno=[%d]"), errno);
596          }          }
597          else if(strcmp(oldFullPath, newFullPath)){          else if(strcmp(oldFullPath, newFullPath)){
598                  wxLogMessage(wxT("File has already exist. [%s]"), fileName->c_str());                  wxLogMessage(wxT("File has already exist. [%s]"), fileName->c_str());
599                  fclose(fp);                  fclose(fp);
600          }          }
601            else {
602                    fclose(fp);
603            }
604    
         delete oldSubject;  
605          delete oldFileName;          delete oldFileName;
606  }  }
607    
# Line 323  const wxString* WikiData::getText() Line 630  const wxString* WikiData::getText()
630                  MN_FATAL_ERROR(wxT("failed iconv_open"));                  MN_FATAL_ERROR(wxT("failed iconv_open"));
631          }          }
632    
633          if(text) {          if(text && isWriteToFile == true) {
634                  iconv_close(codeSet);                  delete text;
635                    text = NULL;
636            }
637            else if(text && isWriteToFile == false) {
638                  return text;                  return text;
639          }          }
640    
# Line 357  void WikiData::modText(wxString* intext) Line 667  void WikiData::modText(wxString* intext)
667          wxCSConv conv(wxT(CODE_SET_SYSTEM));          wxCSConv conv(wxT(CODE_SET_SYSTEM));
668          delete text;          delete text;
669          //text = new wxString(intext->c_str());          //text = new wxString(intext->c_str());
670          text = new wxString(*intext);          //text = new wxString(*intext);
671          //text = new wxString(intext->mb_str(), conv);          text = new wxString(intext->mb_str(), conv);
672  }  }
673    
674  void WikiData::removeDataFile()  void WikiData::removeDataFile()
# Line 376  void WikiData::save() Line 686  void WikiData::save()
686          char fullPath[MAX_BUF_SIZE];          char fullPath[MAX_BUF_SIZE];
687          FILE* fp;          FILE* fp;
688          iconv_t     codeSet;          iconv_t     codeSet;
689          char        inbuf[MAX_WIKI_TEXT_SIZE];          char*        inbuf;
690          char        outbuf[MAX_WIKI_TEXT_SIZE];          char*        outbuf;
691          const char*       inbufPtr;          const char*  inbufPtr;
692          char*       outbufPtr;          char*       outbufPtr;
693          int         inbufSize;          int         inbufSize;
694          int         outbufSize;          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);          codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);
703          if(codeSet == (iconv_t)-1) {          if(codeSet == (iconv_t)-1) {
704                  MN_FATAL_ERROR(wxT("failed iconv_open"));                  MN_FATAL_ERROR(wxT("failed iconv_open"));
# Line 394  void WikiData::save() Line 710  void WikiData::save()
710                  MN_FATAL_ERROR(wxT("File open error."));                  MN_FATAL_ERROR(wxT("File open error."));
711          }          }
712    
713          memset(inbuf, 0, sizeof(inbuf));          memset(inbuf, 0, MAX_WIKI_TEXT_SIZE);
714          strcpy(inbuf,(const char*)text->mb_str());          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;          inbufPtr = inbuf;
721          inbufSize = strlen(inbufPtr);          inbufSize = strlen(inbufPtr);
722          outbufPtr = outbuf;          outbufPtr = outbuf;
723          outbufSize = sizeof(outbuf);          outbufSize = MAX_WIKI_TEXT_SIZE;
724          memset(outbuf, 0, outbufSize);          memset(outbuf, 0, outbufSize);
725          iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);          iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
726          fwrite(outbuf, sizeof(outbuf)-outbufSize, 1, fp);          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);          fclose(fp);
731          iconv_close(codeSet);          iconv_close(codeSet);
732            
733            free(inbuf);
734            free(outbuf);
735            
736            isWriteToFile = true;
737  }  }
738    
739  /******* Tools ************************/  /******* Tools ************************/

Legend:
Removed from v.1.11  
changed lines
  Added in v.1.31

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