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.17 by maloninc, Tue Sep 13 01:37:43 2005 UTC revision 1.35 by maloninc, Tue Oct 24 05:53:29 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);  static void   toLower(char* string);
10  static char*  decode(const char* string);  static char*  decode(const char* string);
# Line 30  mnModel::~mnModel() Line 32  mnModel::~mnModel()
32          delete wikiDataDir;          delete wikiDataDir;
33  }  }
34    
35    /*
36  WikiList* mnModel::search(const char* searchStr)   * 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  {  {
         int         i;  
         wxDir*      dir;  
         FILE*       fp;  
     wxString    fullPathName;  
     char        buf[MAX_BUF_SIZE];  
         WikiData*   wikiData;  
     WikiList*   list = new WikiList();  
     wxString*   fileName = new wxString();  
43          iconv_t     codeSet;          iconv_t     codeSet;
44          char        outbuf[MAX_BUF_SIZE];          char        outbuf[MAX_BUF_SIZE];
45          const char*       inbufPtr  = searchStr;          const char* inbufPtr   = searchStr;
46          char*       outbufPtr = outbuf;          char*       outbufPtr  = outbuf;
47          int         inbufSize = strlen(searchStr);          int         inbufSize  = strlen(searchStr);
48          int         outbufSize = sizeof(outbuf);          int         outbufSize = sizeof(outbuf);
         const char* decodeFileName;  
         char decodeFileNameBuf[MAX_BUF_SIZE];  
49          char*       token;          char*       token;
50          char*       tokenList[32];          int         i;
         bool        found;  
51    
         memset(tokenList, 0, sizeof(char*)*32);  
52          memset(outbuf, 0, outbufSize);          memset(outbuf, 0, outbufSize);
53          codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);          codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);
54          if(codeSet == (iconv_t)-1) {          if(codeSet == (iconv_t)-1) {
# Line 64  WikiList* mnModel::search(const char* se Line 59  WikiList* mnModel::search(const char* se
59    
60          /* searchStr to Tokens */          /* searchStr to Tokens */
61          token = strtok(outbuf, " ");          token = strtok(outbuf, " ");
62          if(token == NULL) return list;          if(token == NULL) return false;
63          tokenList[0] = (char*)malloc(strlen(token)+1);          tokenList[0] = (char*)malloc(strlen(token)+1);
64          snprintf(tokenList[0], strlen(token)+1, "%s", token);          snprintf(tokenList[0], strlen(token)+1, "%s", token);
65          i = 1;          i = 1;
# Line 72  WikiList* mnModel::search(const char* se Line 67  WikiList* mnModel::search(const char* se
67                  tokenList[i] = (char*)malloc(strlen(token)+1);                  tokenList[i] = (char*)malloc(strlen(token)+1);
68                  snprintf(tokenList[i], strlen(token)+1, "%s", token);                  snprintf(tokenList[i], strlen(token)+1, "%s", token);
69                  i++;                  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("Loading Data"), (wxT("Now loading... ") + *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)
160    {
161            int         i;
162            wxDir*      dir;
163            WikiData*   wikiData;
164        WikiList*   list = new WikiList();
165        wxString*   fileName = new wxString();
166            char*       tokenList[MAX_TOKEN];
167    
168            memset(tokenList, 0, sizeof(char*)*MAX_TOKEN);
169            if( makeSearchToken(searchStr, tokenList) == false) return list;
170    
171          dir = new wxDir(*wikiDataDir);          dir = new wxDir(*wikiDataDir);
172      if ( !dir->IsOpened() )      if ( !dir->IsOpened() )
# Line 80  WikiList* mnModel::search(const char* se Line 174  WikiList* mnModel::search(const char* se
174                  MN_FATAL_ERROR(wxT("wxDir has faild\n"));                  MN_FATAL_ERROR(wxT("wxDir has faild\n"));
175          return NULL;          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];
213            char*       outbufPtr = outbuf;
214            int         outbufSize;
215            wxString*   typeToken;
216            char*       ptr;
217            int         i;
218    
219            iconv_t codeSet = iconv_open(CODE_SET_SYSTEM, CODE_SET_EUC_JP);
220            if(codeSet == (iconv_t)-1) {
221                    MN_FATAL_ERROR(wxT("failed iconv_open"));
222            }
223    
224            dir = new wxDir(*wikiDataDir);
225        if ( !dir->IsOpened() )
226        {
227                    MN_FATAL_ERROR(wxT("wxDir has faild\n"));
228            return ;
229        }
230      bool cont = dir->GetFirst(fileName, wxT("*.txt"), 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;
# Line 87  WikiList* mnModel::search(const char* se Line 234  WikiList* mnModel::search(const char* se
234                  if(fp == NULL) {                  if(fp == NULL) {
235                          MN_FATAL_ERROR(wxT("fopen faild"));                          MN_FATAL_ERROR(wxT("fopen faild"));
236                  }                  }
237                    while(1) {
238          while(1){                          memset(buf, 0, MAX_BUF_SIZE);
239                  memset(buf, 0, MAX_BUF_SIZE);                          fgets(buf, MAX_BUF_SIZE, fp);
240              fread(buf, MAX_BUF_SIZE-1, 1, fp);                          if(buf[0] == 0) break;
241              if(buf[0] == 0) break;                          if(strstr(buf, TYPE_TAG)){
242                          decodeFileName = decode(fileName->mb_str());                                  ptr = &buf[typeTagLen];
243                          snprintf(decodeFileNameBuf, MAX_BUF_SIZE, "%s", decodeFileName);                                  while((token = strtok(ptr, " /\r\n:"))!= NULL) {
244                          toLower(buf);                                          memset(outbuf, 0, MAX_BUF_SIZE);
245                          toLower(decodeFileNameBuf);                                          snprintf(outbuf, MAX_BUF_SIZE, "%s", TYPESEARCH_TAG);
246                          found = TRUE;                                          inbufPtr = token;
247                          for(i = 0; tokenList[i] != NULL; i++){                                          inbufSize = strlen(token);
248                                  toLower(tokenList[i]);                                          outbufPtr = &outbuf[strlen(TYPESEARCH_TAG)];
249                                  if(strstr((const char*)buf, (const char*)tokenList[i]) ||                                          outbufSize = sizeof(outbuf) - strlen(TYPESEARCH_TAG);
250                                          strstr((const char*)decodeFileNameBuf, (const char*)tokenList[i])) {                                          iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
251                                          found = TRUE;  
252                                  }                                          typeToken = new wxString(outbuf, conv);
253                                  else {                                          if( addSearchStr(typeToken) ) {
254                                          found = FALSE;                                                  WikiList* wikiList = search(typeToken->mb_str());
255                                          break;                                                  addSearchList(typeToken, wikiList);
256                                            }
257                                            delete typeToken;
258                                            ptr = NULL;
259                                  }                                  }
260                          }                          }
   
                         if(found){  
                                 wikiData = new WikiData(wikiDataDir, (const char*)fileName->mb_str(), fp);  
                                 list->Append(wikiData);  
                                 break;  
                         }  
             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;          delete fileName;
267          for(i = 0; tokenList[i] != NULL; i++) free(tokenList[i]);          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  void mnModel::addSearchStr(wxString* searchStr)  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    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);                  //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 154  void mnModel::modSearchStr(wxString* old Line 360  void mnModel::modSearchStr(wxString* old
360                  itemStr.sprintf(wxT("%s"), newStr->c_str());                  itemStr.sprintf(wxT("%s"), newStr->c_str());
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()  const wxString* mnModel::getWikiDataDir()
375  {  {
376          return wikiDataDir;          return wikiDataDir;
# Line 170  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 190  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, (ICONV_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 210  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 230  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 255  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 271  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 299  void WikiData::modSubject(wxString* newS Line 649  void WikiData::modSubject(wxString* newS
649                  fclose(fp);                  fclose(fp);
650          }          }
651    
         delete oldSubject;  
652          delete oldFileName;          delete oldFileName;
653  }  }
654    
# Line 328  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 341  const wxString* WikiData::getText() Line 693  const wxString* WikiData::getText()
693          }          }
694    
695          while(fgets(buf, MAX_BUF_SIZE, fp)) {          while(fgets(buf, MAX_BUF_SIZE, fp)) {
 #ifdef __WXMAC__  
                 for(int i = 0; buf[i] != 0; i++) if(buf[i] == (char)MAC_BACKSLASH) buf[i] = '\\';  
 #endif  
696                  inbufPtr = buf;                  inbufPtr = buf;
697                  inbufSize = sizeof(buf);                  inbufSize = sizeof(buf);
698                  outbufPtr = outbuf;                  outbufPtr = outbuf;
# Line 365  void WikiData::modText(wxString* intext) Line 714  void WikiData::modText(wxString* intext)
714          wxCSConv conv(wxT(CODE_SET_SYSTEM));          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);          //text = new wxString(*intext);
718          //text = new wxString(intext->mb_str(), conv);          text = new wxString(intext->mb_str(), conv);
719  }  }
720    
721  void WikiData::removeDataFile()  void WikiData::removeDataFile()
# Line 384  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 402  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__  //#ifdef __WXMAC__
764          for(int i = 0; inbuf[i] != 0; i++) if(inbuf[i] == (char)MAC_BACKSLASH) inbuf[i] = '\\';  //      for(int i = 0; inbuf[i] != 0; i++) if(inbuf[i] == (char)MAC_BACKSLASH) inbuf[i] = '\\';
765  #endif  //#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, (ICONV_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 ************************/

Legend:
Removed from v.1.17  
changed lines
  Added in v.1.35

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