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.13 by maloninc, Sat Aug 13 06:12:05 2005 UTC revision 1.32 by maloninc, Fri Oct 20 09:36:08 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    void mnModel::readAll( void )
113    {
114            int         i;
115            wxDir*      dir;
116            WikiData*   wikiData;
117        wxString*   fileName = new wxString();
118            char*       tokenList[MAX_TOKEN];
119    
120            memset(tokenList, 0, sizeof(char*)*MAX_TOKEN);
121            tokenList[0] = "";
122    
123            dir = new wxDir(*wikiDataDir);
124        if ( !dir->IsOpened() )
125        {
126                    MN_FATAL_ERROR(wxT("wxDir has faild\n"));
127            return ;
128        }
129    
130        bool cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES);
131            while(cont){
132    
133                    matchWithToken(fileName, tokenList); /* match with token list, but never match, just read. */
134    
135            cont = dir->GetNext(fileName);
136          }          }
137    
138            delete dir;
139            delete fileName;
140    }
141    
142    WikiList* mnModel::search(const char* searchStr)
143    {
144            int         i;
145            wxDir*      dir;
146            WikiData*   wikiData;
147        WikiList*   list = new WikiList();
148        wxString*   fileName = new wxString();
149            char*       tokenList[MAX_TOKEN];
150    
151            memset(tokenList, 0, sizeof(char*)*MAX_TOKEN);
152            if( makeSearchToken(searchStr, tokenList) == false) return list;
153    
154          dir = new wxDir(*wikiDataDir);          dir = new wxDir(*wikiDataDir);
155      if ( !dir->IsOpened() )      if ( !dir->IsOpened() )
156      {      {
157                  MN_FATAL_ERROR(wxT("wxDir has faild\n"));                  MN_FATAL_ERROR(wxT("wxDir has faild\n"));
158          return NULL;          return NULL;
159      }      }
160    
161        bool cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES);
162            while(cont){
163    
164                    if( matchWithToken(fileName, tokenList) ) { /* match with token list */
165                            wikiData = new WikiData(wikiDataDir, fileName);
166                            list->Append(wikiData);
167                    }
168    
169            cont = dir->GetNext(fileName);
170            }
171    
172            delete dir;
173            delete fileName;
174            for(i = 0; tokenList[i] != NULL; i++) free(tokenList[i]);
175    
176            list->Sort(compWikiData);
177            return list;
178    }
179    
180    /*
181     *  add malon-type:xxx to search list
182     */
183    void mnModel::group()
184    {
185            wxCSConv    conv(wxT(CODE_SET_SYSTEM));
186        char        buf[MAX_BUF_SIZE];
187            wxDir*      dir;
188            FILE*       fp;
189        wxString*   fileName = new wxString();
190        wxString    fullPathName;
191            int         typeTagLen = strlen(TYPE_TAG);
192            char*       token;
193            char*           inbufPtr;
194            int         inbufSize;
195            char        outbuf[MAX_BUF_SIZE];
196            char*       outbufPtr = outbuf;
197            int         outbufSize;
198            wxString*   typeToken;
199            char*       ptr;
200            int         i;
201    
202            iconv_t codeSet = iconv_open(CODE_SET_SYSTEM, CODE_SET_EUC_JP);
203            if(codeSet == (iconv_t)-1) {
204                    MN_FATAL_ERROR(wxT("failed iconv_open"));
205            }
206    
207            dir = new wxDir(*wikiDataDir);
208        if ( !dir->IsOpened() )
209        {
210                    MN_FATAL_ERROR(wxT("wxDir has faild\n"));
211            return ;
212        }
213      bool cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES);      bool cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES);
214          while(cont){          while(cont){
215          fullPathName = *wikiDataDir + wxT("/") + *fileName;          fullPathName = *wikiDataDir + wxT("/") + *fileName;
# Line 86  WikiList* mnModel::search(const char* se Line 217  WikiList* mnModel::search(const char* se
217                  if(fp == NULL) {                  if(fp == NULL) {
218                          MN_FATAL_ERROR(wxT("fopen faild"));                          MN_FATAL_ERROR(wxT("fopen faild"));
219                  }                  }
220                    while(1) {
221          while(1){                          memset(buf, 0, MAX_BUF_SIZE);
222                  memset(buf, 0, MAX_BUF_SIZE);                          fgets(buf, MAX_BUF_SIZE, fp);
223              fread(buf, MAX_BUF_SIZE-1, 1, fp);                          if(buf[0] == 0) break;
224              if(buf[0] == 0) break;                          if(strstr(buf, TYPE_TAG)){
225                          decodeFileName = decode(fileName->mb_str());                                  ptr = &buf[typeTagLen];
226                          snprintf(decodeFileNameBuf, MAX_BUF_SIZE, "%s", decodeFileName);                                  while((token = strtok(ptr, " /\r\n:"))!= NULL) {
227                          toLower(buf);                                          memset(outbuf, 0, MAX_BUF_SIZE);
228                          toLower(outbuf);                                          snprintf(outbuf, MAX_BUF_SIZE, "%s", TYPESEARCH_TAG);
229                          toLower(decodeFileNameBuf);                                          inbufPtr = token;
230                          found = TRUE;                                          inbufSize = strlen(token);
231                          for(i = 0; tokenList[i] != NULL; i++){                                          outbufPtr = &outbuf[strlen(TYPESEARCH_TAG)];
232                                  if(strstr((const char*)buf, (const char*)tokenList[i]) ||                                          outbufSize = sizeof(outbuf) - strlen(TYPESEARCH_TAG);
233                                          strstr((const char*)decodeFileName, (const char*)tokenList[i])) {                                          iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
234                                          found = TRUE;  
235                                            typeToken = new wxString(outbuf, conv);
236                                            if( addSearchStr(typeToken) ) {
237                                                    WikiList* wikiList = search(typeToken->mb_str());
238                                                    addSearchList(typeToken, wikiList);
239                                            }
240                                            delete typeToken;
241                                            ptr = NULL;
242                                  }                                  }
                                 else {  
                                         found = FALSE;  
                                         break;  
                                 }  
                         }  
   
                         if(found){  
                                 wikiData = new WikiData(wikiDataDir, (const char*)fileName->mb_str(), fp);  
                                 list->Append(wikiData);  
                                 break;  
243                          }                          }
             buf[0] = 0;  
244                  }                  }
245                  fclose(fp);                  fclose(fp);
246          cont = dir->GetNext(fileName);          cont = dir->GetNext(fileName);
247          }          }
248          delete dir;          delete dir;
249          delete fileName;          delete fileName;
250          for(i = 0; tokenList[i] != NULL; i++) free(tokenList[i]);          iconv_close(codeSet);
251    }
252    
253          list->Sort(compWikiData);  bool mnModel::normalSearch(char* tokenList[], FILE*fp, char* decodeFileNameBuf)
254          return list;  {
255        char        buf[MAX_BUF_SIZE];
256            bool        found;
257            int         i;
258            while(1){
259            memset(buf, 0, MAX_BUF_SIZE);
260            fread(buf, MAX_BUF_SIZE-1, 1, fp);
261            if(buf[0] == 0) break;
262                    toLower(buf);
263                    found = TRUE;
264                    for(i = 0; tokenList[i] != NULL; i++){
265                            toLower(tokenList[i]);
266                            if(strstr((const char*)buf, (const char*)tokenList[i]) ||                 /* search in file context */
267                                    strstr((const char*)decodeFileNameBuf, (const char*)tokenList[i]) ||  /* search in file name    */
268                                    strcmp((const char*)tokenList[i], (const char*)ALLMEMO_TAG) == 0) {   /* SHOW  ALL MEMO         */
269                                    found = TRUE;
270                            }
271                            else {
272                                    found = FALSE;
273                                    break;
274                            }
275                    }
276    
277                    if(found){ /* all tokens found */
278                            break;
279                    }
280            buf[0] = 0;
281            }
282    
283            return found;
284  }  }
285    
286  void mnModel::addSearchStr(wxString* searchStr)  bool mnModel::typeSearch(char* typeStr, FILE*fp)
287    {
288        char        buf[MAX_BUF_SIZE];
289            bool        found;
290            int         i;
291            char*       typeToken;
292            char        typeStrCopy[MAX_BUF_SIZE];
293    
294            snprintf(typeStrCopy, MAX_BUF_SIZE, "%s", typeStr);
295            while(1){
296            memset(buf, 0, MAX_BUF_SIZE);
297            fgets(buf, MAX_BUF_SIZE, fp);
298            if(buf[0] == 0) break;
299                    if(strstr((const char*)buf, TYPE_TAG)){  /* search TYPE line */
300                            typeToken = strtok(typeStrCopy, ":");
301                            typeToken = strtok(NULL, ":");    /* second field separated by colon(:) */
302                            if(typeToken == NULL) return false;
303                            toLower(typeToken);
304                            toLower(buf);
305                            if(strstr(buf, typeToken)) return true;
306                    }
307            }
308            return false;
309    }
310    
311    bool mnModel::addSearchStr(wxString* searchStr)
312  {  {
313          wxString *string;          wxString *string;
314    
315          if(searchStrList->Index(searchStr->c_str()) == wxNOT_FOUND){          if(searchStrList->Index(searchStr->c_str()) == wxNOT_FOUND){
316                  string = new wxString(searchStr->c_str());                  string = new wxString(searchStr->c_str());
317                  searchStrList->Add(*string, 1);                  searchStrList->Add(*string, 1);
318                    //searchStrList->Insert(*string, 0);
319                    return true;  /* Add */
320          }          }
321            return false; /* does'nt add because of duplicating */
322    }
323    
324    void mnModel::addSearchList(wxString* searchStr, WikiList* list)
325    {
326            wikiHash[*searchStr] = list;
327  }  }
328    
329  void mnModel::removeSearchStr(wxString searchStr)  void mnModel::removeSearchStr(wxString searchStr)
330  {  {
331          if(searchStrList->Index(searchStr.c_str()) != wxNOT_FOUND) {          if(searchStrList->Index(searchStr.c_str()) != wxNOT_FOUND) {
332                  searchStrList->Remove(searchStr.c_str());                  searchStrList->Remove(searchStr.c_str());
333                    wikiHash[*searchStr] = NULL;
334          }          }
335  }  }
336    
# Line 152  void mnModel::modSearchStr(wxString* old Line 343  void mnModel::modSearchStr(wxString* old
343                  itemStr.sprintf(wxT("%s"), newStr->c_str());                  itemStr.sprintf(wxT("%s"), newStr->c_str());
344          }          }
345  }  }
346    
347    void mnModel::clearSearchStrList()
348    {
349            searchStrList->Clear();
350    }
351    
352    void mnModel::clearSearchResultList()
353    {
354            wikiHash.clear();
355    }
356    
357  const wxString* mnModel::getWikiDataDir()  const wxString* mnModel::getWikiDataDir()
358  {  {
359          return wikiDataDir;          return wikiDataDir;
# Line 168  WikiData* mnModel::newWikiData() Line 370  WikiData* mnModel::newWikiData()
370    
371          return data;          return data;
372  }  }
373    const WikiList* mnModel::getSearchResultList(wxString* searchStr)
374    {
375            return wikiHash[*searchStr];
376    }
377    
378    /* add "addData's" clone. if already exist same data, overwrite it*/
379    void mnModel::addSearchResultList(wxString* searchStr, WikiData* addData)
380    {
381            WikiList* wikiList = wikiHash[*searchStr];
382            WikiList::Node* node;
383            WikiData* data;
384    
385            if(wikiList == NULL) {
386                    MN_FATAL_ERROR(wxT("wikiList is null"));
387                    return;
388            }
389    
390            node = wikiList->GetFirst();
391            while(node) {
392                    data = node->GetData();
393                    if(data == addData) return;
394                    if( *(data->getSubject()) == *(addData->getOldSubject()) ) {
395                            if(wikiList->DeleteObject(data)) {
396                                    //delete data; /* may be wrong.. */
397                                    break;
398                            }
399                            else {
400                                    MN_FATAL_ERROR(wxT("Can't find delete data"));
401                            }
402                    }
403                    node = node->GetNext();
404            }
405            WikiData* copy = new WikiData((wxString*)wikiDataDir, (wxString*)addData->getFileName());
406            wikiList->Append(copy);
407            wikiList->Sort(compWikiData);
408    }
409    
410    bool mnModel::delSearchResultList(wxString* searchStr, WikiData* delData)
411    {
412            WikiList* wikiList = wikiHash[*searchStr];
413            WikiList::Node* node;
414            WikiData* data;
415            bool      found = false;
416    
417            if(wikiList == NULL) {
418                    MN_FATAL_ERROR(wxT("wikiList is null"));
419                    return false;
420            }
421    
422            node = wikiList->GetFirst();
423            while(node) {
424                    data = node->GetData();
425                    if(data == delData) {
426                            if(!wikiList->DeleteObject(data)) {
427                                    MN_FATAL_ERROR(wxT("Can't find delete data"));
428                            }
429                            found = true;
430                            break;
431                    }
432                    else if( *(data->getSubject()) == *(delData->getOldSubject()) ) {
433                            if(wikiList->DeleteObject(data)) {
434                                    //delete data;  /* may be wrong.. */
435                                    found = true;
436                                    break;
437                            }
438                            else {
439                                    MN_FATAL_ERROR(wxT("Can't find delete data"));
440                            }
441                    }
442                    node = node->GetNext();
443            }
444            wikiList->Sort(compWikiData);
445            return found;
446    }
447    
448  /******* WikiData ************************/  /******* WikiData ************************/
449  WikiData::WikiData(wxString* dataDir, const char* file, FILE* fp)  WikiData::WikiData(wxString* dataDir, wxString* inFileName)
450  {  {
451            FILE* fp;
452        wxString    fullPathName;
453          char* decodeStr;          char* decodeStr;
454          char  buf[MAX_BUF_SIZE];          char  buf[MAX_BUF_SIZE];
455          char* inbuf;          char* inbuf;
# Line 188  WikiData::WikiData(wxString* dataDir, co Line 466  WikiData::WikiData(wxString* dataDir, co
466    
467          text = NULL;          text = NULL;
468          memset(outbuf, 0, MAX_BUF_SIZE);          memset(outbuf, 0, MAX_BUF_SIZE);
469          fileName = new wxString((const char*)file, conv);          fileName = new wxString(inFileName->mb_str(), conv);
470          dataDirName = dataDir;          dataDirName = new wxString(dataDir->mb_str(), conv);
471          decodeStr = decode(file);          decodeStr = decode(fileName->mb_str());
472          inbufSize = strlen(decodeStr);          inbufSize = strlen(decodeStr);
473          outbufSize = sizeof(outbuf);          outbufSize = sizeof(outbuf);
474          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);
475          subject  = new wxString((const char*)outbuf, conv);          subject  = new wxString((const char*)outbuf, conv);
476            oldSubject  = new wxString((const char*)outbuf, conv);
477          iconv_close(codeSet);          iconv_close(codeSet);
478    
479          date     = NULL;          date     = NULL;
480    
481      rewind(fp);      fullPathName = *dataDir + wxT("/") + *fileName;
482            fp = fopen((const char*)fullPathName.mb_str(), "r");
483            if(fp == NULL) {
484                    MN_FATAL_ERROR(wxT("fopen faild"));
485            }
486          while(fgets(buf, MAX_BUF_SIZE, fp)) {          while(fgets(buf, MAX_BUF_SIZE, fp)) {
487                  if(strstr( (const char*)buf, (const char*)DATE_TAG)) {                  if(strstr( (const char*)buf, (const char*)DATE_TAG)) {
488                          strtok(buf, "\n");                          strtok(buf, "\n");
# Line 208  WikiData::WikiData(wxString* dataDir, co Line 491  WikiData::WikiData(wxString* dataDir, co
491                          break;                          break;
492                  }                  }
493          }          }
494            fclose(fp);
495    
496            isWriteToFile = true;
497  }  }
498    
499  WikiData::WikiData(wxString* dataDir) {  WikiData::WikiData(wxString* dataDir) {
500            FILE*      fp;
501          time_t     now;          time_t     now;
502          char       buf[MAX_BUF_SIZE];          char       buf[MAX_BUF_SIZE];
503          wxCSConv    conv(wxT(CODE_SET_SYSTEM));          char       fname[MAX_BUF_SIZE];
504            char       templateBuf[MAX_BUF_SIZE];
505            char*      inbufPtr;
506            int        inbufSize;
507            char       outbuf[MAX_BUF_SIZE];
508            char*      outbufPtr;
509            int        outbufSize;
510            wxCSConv   conv(wxT(CODE_SET_SYSTEM));
511    
512          dataDirName = dataDir;          dataDirName = new wxString(dataDir->mb_str(), conv);
513    
514      time(&now);      time(&now);
515          memset(buf, 0, sizeof(buf));          memset(buf, 0, sizeof(buf));
516          strftime(buf, sizeof(buf), "%Y/%m/%d-%H%M%S",localtime(&now));          strftime(buf, sizeof(buf), "%Y/%m/%d-%H%M%S",localtime(&now));
517          subject  = new wxString(buf, conv);          subject  = new wxString(buf, conv);
518            oldSubject  = new wxString(buf, conv);
519    
520          fileName = new wxString(encode(buf), conv);          fileName = new wxString(encode(buf), conv);
521          fileName->Append(wxT(EXT_TAG));          fileName->Append(wxT(EXT_TAG));
# Line 228  WikiData::WikiData(wxString* dataDir) { Line 523  WikiData::WikiData(wxString* dataDir) {
523          memset(buf, 0, sizeof(buf));          memset(buf, 0, sizeof(buf));
524          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));
525          date    = new wxString(buf, conv);          date    = new wxString(buf, conv);
526    
527            /* try to open template file */
528            snprintf(fname, sizeof(fname), "%s/%s", (const char*)(dataDir->mb_str()), NEW_DATA_TEMPLATE);
529            fp = fopen(fname, "r");
530            if(fp == NULL){
531                    memset(buf, 0, sizeof(buf));
532                    strftime(buf, sizeof(buf), NEW_DATA,localtime(&now));
533            }
534            else {
535                    memset(buf, 0, sizeof(buf));
536                    memset(templateBuf, 0, sizeof(templateBuf));
537                    memset(outbuf, 0, sizeof(outbuf));
538                    fread(templateBuf, sizeof(templateBuf), 1, fp);
539                    
540          memset(buf, 0, sizeof(buf));                  iconv_t codeSet = iconv_open(CODE_SET_SYSTEM, CODE_SET_EUC_JP);
541          strftime(buf, sizeof(buf), NEW_DATA,localtime(&now));                  if(codeSet == (iconv_t)-1) {
542                            MN_FATAL_ERROR(wxT("failed iconv_open"));
543                    }
544                    inbufPtr = templateBuf;
545                outbufPtr = outbuf;
546                    inbufSize = strlen(templateBuf);
547                    outbufSize = sizeof(outbuf);
548                    iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
549                    
550                    strftime(buf, sizeof(buf), outbuf,localtime(&now));
551    
552            }
553          text    = new wxString(buf, conv);          text    = new wxString(buf, conv);
554    
555            if(fp) fclose(fp);
556    
557            isWriteToFile = false;
558  }  }
559    
560  WikiData::~WikiData()  WikiData::~WikiData()
561  {  {
562          delete subject;          delete subject;
563            delete oldSubject;
564          delete fileName;          delete fileName;
565          delete date;          delete date;
566          delete text;          delete text;
567            delete dataDirName;
568  }  }
569    
570  const wxString* WikiData::getFileName()  const wxString* WikiData::getFileName()
# Line 253  const wxString* WikiData::getSubject() Line 577  const wxString* WikiData::getSubject()
577          return subject;          return subject;
578  }  }
579    
580    const wxString* WikiData::getOldSubject()
581    {
582            return oldSubject;
583    }
584    
585    void WikiData::setOldSubjectFromCurrent()
586    {
587            oldSubject = new wxString(*subject);
588    }
589    
590  void WikiData::modSubject(wxString* newSubject)  void WikiData::modSubject(wxString* newSubject)
591  {  {
592          wxCSConv    conv(wxT(CODE_SET_SYSTEM));          wxCSConv    conv(wxT(CODE_SET_SYSTEM));
593          wxString*   oldFileName = fileName;          wxString*   oldFileName = fileName;
         wxString*   oldSubject  = subject;  
594          char        oldFullPath[MAX_BUF_SIZE];          char        oldFullPath[MAX_BUF_SIZE];
595          char        newFullPath[MAX_BUF_SIZE];          char        newFullPath[MAX_BUF_SIZE];
596          iconv_t     codeSet;          iconv_t     codeSet;
# Line 269  void WikiData::modSubject(wxString* newS Line 602  void WikiData::modSubject(wxString* newS
602          int         outbufSize = sizeof(outbuf);          int         outbufSize = sizeof(outbuf);
603          FILE*       fp;          FILE*       fp;
604    
605            oldSubject = subject;
606    
607          memset(outbuf, 0, outbufSize);          memset(outbuf, 0, outbufSize);
608          memset(inbuf,  0, sizeof(inbuf));          memset(inbuf,  0, sizeof(inbuf));
609          strcpy(inbuf, (const char*)newSubject->mb_str());          strcpy(inbuf, (const char*)newSubject->mb_str());
# Line 297  void WikiData::modSubject(wxString* newS Line 632  void WikiData::modSubject(wxString* newS
632                  fclose(fp);                  fclose(fp);
633          }          }
634    
         delete oldSubject;  
635          delete oldFileName;          delete oldFileName;
636  }  }
637    
# Line 326  const wxString* WikiData::getText() Line 660  const wxString* WikiData::getText()
660                  MN_FATAL_ERROR(wxT("failed iconv_open"));                  MN_FATAL_ERROR(wxT("failed iconv_open"));
661          }          }
662    
663          if(text) {          if(text && isWriteToFile == true) {
664                  iconv_close(codeSet);                  delete text;
665                    text = NULL;
666            }
667            else if(text && isWriteToFile == false) {
668                  return text;                  return text;
669          }          }
670    
# Line 339  const wxString* WikiData::getText() Line 676  const wxString* WikiData::getText()
676          }          }
677    
678          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  
679                  inbufPtr = buf;                  inbufPtr = buf;
680                  inbufSize = sizeof(buf);                  inbufSize = sizeof(buf);
681                  outbufPtr = outbuf;                  outbufPtr = outbuf;
# Line 363  void WikiData::modText(wxString* intext) Line 697  void WikiData::modText(wxString* intext)
697          wxCSConv conv(wxT(CODE_SET_SYSTEM));          wxCSConv conv(wxT(CODE_SET_SYSTEM));
698          delete text;          delete text;
699          //text = new wxString(intext->c_str());          //text = new wxString(intext->c_str());
700          text = new wxString(*intext);          //text = new wxString(*intext);
701          //text = new wxString(intext->mb_str(), conv);          text = new wxString(intext->mb_str(), conv);
702  }  }
703    
704  void WikiData::removeDataFile()  void WikiData::removeDataFile()
# Line 382  void WikiData::save() Line 716  void WikiData::save()
716          char fullPath[MAX_BUF_SIZE];          char fullPath[MAX_BUF_SIZE];
717          FILE* fp;          FILE* fp;
718          iconv_t     codeSet;          iconv_t     codeSet;
719          char        inbuf[MAX_WIKI_TEXT_SIZE];          char*        inbuf;
720          char        outbuf[MAX_WIKI_TEXT_SIZE];          char*        outbuf;
721          const char*       inbufPtr;          const char*  inbufPtr;
722          char*       outbufPtr;          char*       outbufPtr;
723          int         inbufSize;          int         inbufSize;
724          int         outbufSize;          int         outbufSize;
725    
726            inbuf  = (char*)malloc(MAX_WIKI_TEXT_SIZE);
727            outbuf = (char*)malloc(MAX_WIKI_TEXT_SIZE);
728            if(inbuf == NULL || outbuf == NULL) {
729                    MN_FATAL_ERROR(wxT("It's too big data. Max size is 5MB."));
730            }
731    
732          codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);          codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);
733          if(codeSet == (iconv_t)-1) {          if(codeSet == (iconv_t)-1) {
734                  MN_FATAL_ERROR(wxT("failed iconv_open"));                  MN_FATAL_ERROR(wxT("failed iconv_open"));
# Line 400  void WikiData::save() Line 740  void WikiData::save()
740                  MN_FATAL_ERROR(wxT("File open error."));                  MN_FATAL_ERROR(wxT("File open error."));
741          }          }
742    
743          memset(inbuf, 0, sizeof(inbuf));          memset(inbuf, 0, MAX_WIKI_TEXT_SIZE);
744          strcpy(inbuf,(const char*)text->mb_str());          strcpy(inbuf,(const char*)text->mb_str());
745    
746  #ifdef __WXMAC__  //#ifdef __WXMAC__
747          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] = '\\';
748  #endif  //#endif
749    
750          inbufPtr = inbuf;          inbufPtr = inbuf;
751          inbufSize = strlen(inbufPtr);          inbufSize = strlen(inbufPtr);
752          outbufPtr = outbuf;          outbufPtr = outbuf;
753          outbufSize = sizeof(outbuf);          outbufSize = MAX_WIKI_TEXT_SIZE;
754          memset(outbuf, 0, outbufSize);          memset(outbuf, 0, outbufSize);
755          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);
756          fwrite(outbuf, sizeof(outbuf)-outbufSize, 1, fp);          if(inbufSize != 0) { // iconv error
757                    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);
758            }
759            fwrite(outbuf, MAX_WIKI_TEXT_SIZE-outbufSize, 1, fp);
760          fclose(fp);          fclose(fp);
761          iconv_close(codeSet);          iconv_close(codeSet);
762            
763            free(inbuf);
764            free(outbuf);
765            
766            isWriteToFile = true;
767  }  }
768    
769  /******* Tools ************************/  /******* Tools ************************/

Legend:
Removed from v.1.13  
changed lines
  Added in v.1.32

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