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.3 by maloninc, Wed Aug 3 23:19:01 2005 UTC revision 1.17 by maloninc, Tue Sep 13 01:37:43 2005 UTC
# Line 4  Line 4 
4  #include <wx/regex.h>  #include <wx/regex.h>
5  #include <iconv.h>  #include <iconv.h>
6    
7  /* プロトタイプ */  static void   toLower(char* string);
8  static char*  decode(const char* string);  static char*  decode(const char* string);
9  static char*  encode(const char* string);  static char*  encode(const char* string);
10  static int compWikiData(const void* wiki1, const void* wiki2);  static int    compWikiData(const void* wiki1, const void* wiki2);
11    
12    
13  /******* WikiList ************************/  /******* WikiList ************************/
# Line 33  mnModel::~mnModel() Line 33  mnModel::~mnModel()
33    
34  WikiList* mnModel::search(const char* searchStr)  WikiList* mnModel::search(const char* searchStr)
35  {  {
36            int         i;
37          wxDir*      dir;          wxDir*      dir;
38          FILE*       fp;          FILE*       fp;
39      wxString    fullPathName;      wxString    fullPathName;
# Line 40  WikiList* mnModel::search(const char* se Line 41  WikiList* mnModel::search(const char* se
41          WikiData*   wikiData;          WikiData*   wikiData;
42      WikiList*   list = new WikiList();      WikiList*   list = new WikiList();
43      wxString*   fileName = new wxString();      wxString*   fileName = new wxString();
         wxRegEx*        regStr;  
44          iconv_t     codeSet;          iconv_t     codeSet;
45          char        outbuf[MAX_BUF_SIZE];          char        outbuf[MAX_BUF_SIZE];
46          const char*       inbufPtr  = searchStr;          const char*       inbufPtr  = searchStr;
# Line 48  WikiList* mnModel::search(const char* se Line 48  WikiList* mnModel::search(const char* se
48          int         inbufSize = strlen(searchStr);          int         inbufSize = strlen(searchStr);
49          int         outbufSize = sizeof(outbuf);          int         outbufSize = sizeof(outbuf);
50          const char* decodeFileName;          const char* decodeFileName;
51            char decodeFileNameBuf[MAX_BUF_SIZE];
52            char*       token;
53            char*       tokenList[32];
54            bool        found;
55    
56            memset(tokenList, 0, sizeof(char*)*32);
57          memset(outbuf, 0, outbufSize);          memset(outbuf, 0, outbufSize);
58          codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);          codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);
59          if(codeSet == (iconv_t)-1) {          if(codeSet == (iconv_t)-1) {
60                  MN_FATAL_ERROR(wxT("failed iconv_open"));                  MN_FATAL_ERROR(wxT("failed iconv_open"));
61          }          }
62          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);
63          iconv_close(codeSet);          iconv_close(codeSet);
64    
65            /* searchStr to Tokens */
66            token = strtok(outbuf, " ");
67            if(token == NULL) return list;
68            tokenList[0] = (char*)malloc(strlen(token)+1);
69            snprintf(tokenList[0], strlen(token)+1, "%s", token);
70            i = 1;
71            while((token = strtok(NULL, " ")) != NULL) {
72                    tokenList[i] = (char*)malloc(strlen(token)+1);
73                    snprintf(tokenList[i], strlen(token)+1, "%s", token);
74                    i++;
75            }
76    
77          dir = new wxDir(*wikiDataDir);          dir = new wxDir(*wikiDataDir);
78      if ( !dir->IsOpened() )      if ( !dir->IsOpened() )
79      {      {
80                  MN_FATAL_ERROR(wxT("wxDir has faild\n"));                  MN_FATAL_ERROR(wxT("wxDir has faild\n"));
81          return NULL;          return NULL;
82      }      }
83      bool cont = dir->GetFirst(fileName, wxEmptyString, wxDIR_FILES);      bool cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES);
84          while(cont){          while(cont){
85          fullPathName = *wikiDataDir + wxT("/") + *fileName;          fullPathName = *wikiDataDir + wxT("/") + *fileName;
86                  fp = fopen((const char*)fullPathName.mb_str(), "r");                  fp = fopen((const char*)fullPathName.mb_str(), "r");
87                  if(fp == NULL) {                  if(fp == NULL) {
88                          MN_FATAL_ERROR(wxT("fopen faild"));                          MN_FATAL_ERROR(wxT("fopen faild"));
89                  }                  }
90    
91          while(1){          while(1){
92                  memset(buf, 0, MAX_BUF_SIZE);                  memset(buf, 0, MAX_BUF_SIZE);
93              fread(buf, MAX_BUF_SIZE-1, 1, fp);              fread(buf, MAX_BUF_SIZE-1, 1, fp);
94              if(buf[0] == 0) break;              if(buf[0] == 0) break;
95                          decodeFileName = decode(fileName->mb_str());                          decodeFileName = decode(fileName->mb_str());
96                  if(strstr((const char*)buf, (const char*)outbuf) ||                          snprintf(decodeFileNameBuf, MAX_BUF_SIZE, "%s", decodeFileName);
97                                  strstr((const char*)decodeFileName, (const char*)outbuf)) {                          toLower(buf);
98                            toLower(decodeFileNameBuf);
99                            found = TRUE;
100                            for(i = 0; tokenList[i] != NULL; i++){
101                                    toLower(tokenList[i]);
102                                    if(strstr((const char*)buf, (const char*)tokenList[i]) ||
103                                            strstr((const char*)decodeFileNameBuf, (const char*)tokenList[i])) {
104                                            found = TRUE;
105                                    }
106                                    else {
107                                            found = FALSE;
108                                            break;
109                                    }
110                            }
111    
112                            if(found){
113                                  wikiData = new WikiData(wikiDataDir, (const char*)fileName->mb_str(), fp);                                  wikiData = new WikiData(wikiDataDir, (const char*)fileName->mb_str(), fp);
114                                  list->Append(wikiData);                                  list->Append(wikiData);
115                                  break;                                  break;
# Line 87  WikiList* mnModel::search(const char* se Line 120  WikiList* mnModel::search(const char* se
120          cont = dir->GetNext(fileName);          cont = dir->GetNext(fileName);
121          }          }
122          delete dir;          delete dir;
123            delete fileName;
124            for(i = 0; tokenList[i] != NULL; i++) free(tokenList[i]);
125    
126          list->Sort(compWikiData);          list->Sort(compWikiData);
127          return list;          return list;
# Line 98  void mnModel::addSearchStr(wxString* sea Line 133  void mnModel::addSearchStr(wxString* sea
133    
134          if(searchStrList->Index(searchStr->c_str()) == wxNOT_FOUND){          if(searchStrList->Index(searchStr->c_str()) == wxNOT_FOUND){
135                  string = new wxString(searchStr->c_str());                  string = new wxString(searchStr->c_str());
136                  searchStrList->Add(*string, 1);                  //searchStrList->Add(*string, 1);
137                    searchStrList->Insert(*string, 0);
138          }          }
139  }  }
140    
# Line 118  void mnModel::modSearchStr(wxString* old Line 154  void mnModel::modSearchStr(wxString* old
154                  itemStr.sprintf(wxT("%s"), newStr->c_str());                  itemStr.sprintf(wxT("%s"), newStr->c_str());
155          }          }
156  }  }
157    const wxString* mnModel::getWikiDataDir()
158    {
159            return wikiDataDir;
160    }
161    
162  const wxArrayString* mnModel::getSearchStrList()  const wxArrayString* mnModel::getSearchStrList()
163  {  {
# Line 155  WikiData::WikiData(wxString* dataDir, co Line 195  WikiData::WikiData(wxString* dataDir, co
195          decodeStr = decode(file);          decodeStr = decode(file);
196          inbufSize = strlen(decodeStr);          inbufSize = strlen(decodeStr);
197          outbufSize = sizeof(outbuf);          outbufSize = sizeof(outbuf);
198          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);
199          subject  = new wxString((const char*)outbuf, conv);          subject  = new wxString((const char*)outbuf, conv);
200          iconv_close(codeSet);          iconv_close(codeSet);
201    
# Line 239  void WikiData::modSubject(wxString* newS Line 279  void WikiData::modSubject(wxString* newS
279          if(codeSet == (iconv_t)-1) {          if(codeSet == (iconv_t)-1) {
280                  MN_FATAL_ERROR(wxT("failed iconv_open"));                  MN_FATAL_ERROR(wxT("failed iconv_open"));
281          }          }
282          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);
283          iconv_close(codeSet);          iconv_close(codeSet);
284          subject  = new wxString(newSubject->c_str());          subject  = new wxString(newSubject->c_str());
285          fileName = new wxString(encode(outbuf), conv);          fileName = new wxString(encode(outbuf), conv);
# Line 249  void WikiData::modSubject(wxString* newS Line 289  void WikiData::modSubject(wxString* newS
289          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());
290    
291          if((fp = fopen(newFullPath, "r")) == NULL) {          if((fp = fopen(newFullPath, "r")) == NULL) {
292                  rename(oldFullPath, newFullPath);                  if(rename(oldFullPath, newFullPath) < 0) wxLogMessage(wxT("rename error: errno=[%d]"), errno);
293          }          }
294          else if(strcmp(oldFullPath, newFullPath)){          else if(strcmp(oldFullPath, newFullPath)){
295                  wxLogMessage(wxT("File has already exist. [%s]"), fileName->c_str());                  wxLogMessage(wxT("File has already exist. [%s]"), fileName->c_str());
296                  fclose(fp);                  fclose(fp);
297          }          }
298            else {
299                    fclose(fp);
300            }
301    
302          delete oldSubject;          delete oldSubject;
303          delete oldFileName;          delete oldFileName;
# Line 298  const wxString* WikiData::getText() Line 341  const wxString* WikiData::getText()
341          }          }
342    
343          while(fgets(buf, MAX_BUF_SIZE, fp)) {          while(fgets(buf, MAX_BUF_SIZE, fp)) {
344    #ifdef __WXMAC__
345                    for(int i = 0; buf[i] != 0; i++) if(buf[i] == (char)MAC_BACKSLASH) buf[i] = '\\';
346    #endif
347                  inbufPtr = buf;                  inbufPtr = buf;
348                  inbufSize = sizeof(buf);                  inbufSize = sizeof(buf);
349                  outbufPtr = outbuf;                  outbufPtr = outbuf;
350                  outbufSize = sizeof(outbuf);                  outbufSize = sizeof(outbuf);
351                  memset(outbuf, 0, outbufSize);                  memset(outbuf, 0, outbufSize);
352                  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);
353                  tmpStr = new wxString((char*)outbuf, conv);                  tmpStr = new wxString((char*)outbuf, conv);
354                  *text += *tmpStr;                  *text += *tmpStr;
355                  delete tmpStr;                  delete tmpStr;
# Line 358  void WikiData::save() Line 404  void WikiData::save()
404    
405          memset(inbuf, 0, sizeof(inbuf));          memset(inbuf, 0, sizeof(inbuf));
406          strcpy(inbuf,(const char*)text->mb_str());          strcpy(inbuf,(const char*)text->mb_str());
407    
408    #ifdef __WXMAC__
409            for(int i = 0; inbuf[i] != 0; i++) if(inbuf[i] == (char)MAC_BACKSLASH) inbuf[i] = '\\';
410    #endif
411    
412          inbufPtr = inbuf;          inbufPtr = inbuf;
413          inbufSize = strlen(inbufPtr);          inbufSize = strlen(inbufPtr);
414          outbufPtr = outbuf;          outbufPtr = outbuf;
415          outbufSize = sizeof(outbuf);          outbufSize = sizeof(outbuf);
416          memset(outbuf, 0, outbufSize);          memset(outbuf, 0, outbufSize);
417          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);
418          fwrite(outbuf, sizeof(outbuf)-outbufSize, 1, fp);          fwrite(outbuf, sizeof(outbuf)-outbufSize, 1, fp);
419          fclose(fp);          fclose(fp);
420          iconv_close(codeSet);          iconv_close(codeSet);
# Line 371  void WikiData::save() Line 422  void WikiData::save()
422    
423  /******* Tools ************************/  /******* Tools ************************/
424    
425    static void toLower(char* string)
426    {
427            int i;
428    
429            for(i = 0; string[i] != 0; i++) {
430                    string[i] = tolower(string[i]);
431            }
432    }
433    
434    
435  static char* decode(const char* string)  static char* decode(const char* string)
436  {  {
437          static char buf[MAX_BUF_SIZE];          static char buf[MAX_BUF_SIZE];

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.17

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