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.23 by maloninc, Thu Sep 15 09:20:48 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 65  bool mnModel::makeSearchToken(const char Line 67  bool mnModel::makeSearchToken(const char
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;          return true;
73  }  }
# Line 81  bool mnModel::matchWithToken(wxString* f Line 84  bool mnModel::matchWithToken(wxString* f
84      fullPathName = *wikiDataDir + wxT("/") + *fileName;      fullPathName = *wikiDataDir + wxT("/") + *fileName;
85          fp = fopen((const char*)fullPathName.mb_str(), "r");          fp = fopen((const char*)fullPathName.mb_str(), "r");
86          if(fp == NULL) {          if(fp == NULL) {
87                  MN_FATAL_ERROR(wxT("fopen faild"));                  return false;  /* because of removed */
88          }          }
89    
90          /* TYPE search */          /* TYPE search */
# Line 107  bool mnModel::matchWithToken(wxString* f Line 110  bool mnModel::matchWithToken(wxString* f
110          return ans;          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)  WikiList* mnModel::search(const char* searchStr)
160  {  {
161          int         i;          int         i;
# Line 114  WikiList* mnModel::search(const char* se Line 163  WikiList* mnModel::search(const char* se
163          WikiData*   wikiData;          WikiData*   wikiData;
164      WikiList*   list = new WikiList();      WikiList*   list = new WikiList();
165      wxString*   fileName = new wxString();      wxString*   fileName = new wxString();
166          char*       tokenList[32];          char*       tokenList[MAX_TOKEN];
167    
168          memset(tokenList, 0, sizeof(char*)*32);          memset(tokenList, 0, sizeof(char*)*MAX_TOKEN);
169          if( makeSearchToken(searchStr, tokenList) == false) return list;          if( makeSearchToken(searchStr, tokenList) == false) return list;
170    
171          dir = new wxDir(*wikiDataDir);          dir = new wxDir(*wikiDataDir);
# Line 145  WikiList* mnModel::search(const char* se Line 194  WikiList* mnModel::search(const char* se
194          return list;          return list;
195  }  }
196    
197    /*
198     *  add malon-type:xxx to search list
199     */
200  void mnModel::group()  void mnModel::group()
201  {  {
202          wxCSConv    conv(wxT(CODE_SET_SYSTEM));          wxCSConv    conv(wxT(CODE_SET_SYSTEM));
# Line 198  void mnModel::group() Line 250  void mnModel::group()
250                                          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);
251    
252                                          typeToken = new wxString(outbuf, conv);                                          typeToken = new wxString(outbuf, conv);
253                                          addSearchStr(typeToken);                                          if( addSearchStr(typeToken) ) {
254                                                    WikiList* wikiList = search(typeToken->mb_str());
255                                                    addSearchList(typeToken, wikiList);
256                                            }
257                                          delete typeToken;                                          delete typeToken;
258                                          ptr = NULL;                                          ptr = NULL;
259                                  }                                  }
# Line 270  bool mnModel::typeSearch(char* typeStr, Line 325  bool mnModel::typeSearch(char* typeStr,
325          return false;          return false;
326  }  }
327    
328  void mnModel::addSearchStr(wxString* searchStr)  bool mnModel::addSearchStr(wxString* searchStr)
329  {  {
330          wxString *string;          wxString *string;
331    
# Line 278  void mnModel::addSearchStr(wxString* sea Line 333  void mnModel::addSearchStr(wxString* sea
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)  void mnModel::addSearchList(wxString* searchStr, WikiList* list)
# Line 290  void mnModel::removeSearchStr(wxString s Line 347  void mnModel::removeSearchStr(wxString s
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 302  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 330  void mnModel::addSearchResultList(wxStri Line 399  void mnModel::addSearchResultList(wxStri
399          WikiList::Node* node;          WikiList::Node* node;
400          WikiData* data;          WikiData* data;
401    
402          node = wikiList->GetFirst();          if(wikiList == NULL) {
403          if(!node) {                  MN_FATAL_ERROR(wxT("wikiList is null"));
404                  MN_FATAL_ERROR(wxT("Search Result List is empty"));                  return;
405          }          }
406    
407            node = wikiList->GetFirst();
408          while(node) {          while(node) {
409                  data = node->GetData();                  data = node->GetData();
410                  if(data == addData) return;                  if(data == addData) return;
411                  if( *(data->getSubject()) == *(addData->getSubject()) ) {                  if( *(data->getSubject()) == *(addData->getOldSubject()) ) {
412                          if(wikiList->DeleteObject(data)) {                          if(wikiList->DeleteObject(data)) {
413                                  delete data;                                  //delete data; /* may be wrong.. */
414                                  break;                                  break;
415                          }                          }
416                          else {                          else {
# Line 354  void mnModel::addSearchResultList(wxStri Line 424  void mnModel::addSearchResultList(wxStri
424          wikiList->Sort(compWikiData);          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, wxString* inFileName)  WikiData::WikiData(wxString* dataDir, wxString* inFileName)
467  {  {
# Line 375  WikiData::WikiData(wxString* dataDir, wx Line 483  WikiData::WikiData(wxString* dataDir, wx
483    
484          text = NULL;          text = NULL;
485          memset(outbuf, 0, MAX_BUF_SIZE);          memset(outbuf, 0, MAX_BUF_SIZE);
486          fileName = new wxString(*inFileName);          fileName = new wxString(inFileName->mb_str(), conv);
487          dataDirName = dataDir;          dataDirName = new wxString(dataDir->mb_str(), conv);
488          decodeStr = decode(fileName->mb_str());          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;
# Line 400  WikiData::WikiData(wxString* dataDir, wx Line 509  WikiData::WikiData(wxString* dataDir, wx
509                  }                  }
510          }          }
511          fclose(fp);          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 420  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 445  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 461  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 489  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 518  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 531  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 555  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 574  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 592  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.23  
changed lines
  Added in v.1.35

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