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.24 by maloninc, Fri Sep 16 04:12:58 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);  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 107  bool mnModel::matchWithToken(wxString* f Line 110  bool mnModel::matchWithToken(wxString* f
110          return ans;          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;          int         i;
# Line 114  WikiList* mnModel::search(const char* se Line 161  WikiList* mnModel::search(const char* se
161          WikiData*   wikiData;          WikiData*   wikiData;
162      WikiList*   list = new WikiList();      WikiList*   list = new WikiList();
163      wxString*   fileName = new wxString();      wxString*   fileName = new wxString();
164          char*       tokenList[32];          char*       tokenList[MAX_TOKEN];
165    
166          memset(tokenList, 0, sizeof(char*)*32);          memset(tokenList, 0, sizeof(char*)*MAX_TOKEN);
167          if( makeSearchToken(searchStr, tokenList) == false) return list;          if( makeSearchToken(searchStr, tokenList) == false) return list;
168    
169          dir = new wxDir(*wikiDataDir);          dir = new wxDir(*wikiDataDir);
# Line 460  WikiData::WikiData(wxString* dataDir, wx Line 507  WikiData::WikiData(wxString* dataDir, wx
507                  }                  }
508          }          }
509          fclose(fp);          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 = new wxString(dataDir->mb_str(), conv);          dataDirName = new wxString(dataDir->mb_str(), conv);
528    
# Line 481  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()
# Line 513  const wxString* WikiData::getOldSubject( Line 597  const wxString* WikiData::getOldSubject(
597          return oldSubject;          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));
# Line 586  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 599  const wxString* WikiData::getText() Line 691  const wxString* WikiData::getText()
691          }          }
692    
693          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  
694                  inbufPtr = buf;                  inbufPtr = buf;
695                  inbufSize = sizeof(buf);                  inbufSize = sizeof(buf);
696                  outbufPtr = outbuf;                  outbufPtr = outbuf;
# Line 642  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 660  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__  //#ifdef __WXMAC__
762          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] = '\\';
763  #endif  //#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, (ICONV_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 ************************/

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

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