Develop and Download Open Source Software

Browse CVS Repository

Diff of /malonnote/mnModel.cpp

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

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

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

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