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.26 by maloninc, Fri Oct 7 10:19:46 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 108  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 461  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 = new wxString(dataDir->mb_str(), conv);          dataDirName = new wxString(dataDir->mb_str(), conv);
530    
# Line 482  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()
# Line 592  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 605  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 648  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 666  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          if(inbufSize != 0) { // iconv error          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);                  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, sizeof(outbuf)-outbufSize, 1, fp);          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.26  
changed lines
  Added in v.1.35

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