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.6 by maloninc, Wed Aug 10 05:35:03 2005 UTC revision 1.19 by maloninc, Tue Sep 13 09:11:37 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;
     char        buf[MAX_BUF_SIZE];  
40          WikiData*   wikiData;          WikiData*   wikiData;
41      WikiList*   list = new WikiList();      WikiList*   list = new WikiList();
42      wxString*   fileName = new wxString();      wxString*   fileName = new wxString();
43          const char* decodeFileName;          const char* decodeFileName;
44          wxCSConv eucConv(wxT(CODE_SET_EUC_JP));          char decodeFileNameBuf[MAX_BUF_SIZE];
45          wxCSConv sysConv(wxT(CODE_SET_SYSTEM));          iconv_t     codeSet;
46            char        outbuf[MAX_BUF_SIZE];
47            const char*       inbufPtr  = searchStr;
48            char*       outbufPtr = outbuf;
49            int         inbufSize = strlen(searchStr);
50            int         outbufSize = sizeof(outbuf);
51            char*       token;
52            char*       tokenList[32];
53            bool        found;
54    
55            memset(tokenList, 0, sizeof(char*)*32);
56            memset(outbuf, 0, outbufSize);
57            codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);
58            if(codeSet == (iconv_t)-1) {
59                    MN_FATAL_ERROR(wxT("failed iconv_open"));
60            }
61            iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
62            iconv_close(codeSet);
63    
64            /* searchStr to Tokens */
65            token = strtok(outbuf, " ");
66            if(token == NULL) return list;
67            tokenList[0] = (char*)malloc(strlen(token)+1);
68            snprintf(tokenList[0], strlen(token)+1, "%s", token);
69            i = 1;
70            while((token = strtok(NULL, " ")) != NULL) {
71                    tokenList[i] = (char*)malloc(strlen(token)+1);
72                    snprintf(tokenList[i], strlen(token)+1, "%s", token);
73                    i++;
74            }
75    
76          dir = new wxDir(*wikiDataDir);          dir = new wxDir(*wikiDataDir);
77      if ( !dir->IsOpened() )      if ( !dir->IsOpened() )
# Line 58  WikiList* mnModel::search(const char* se Line 86  WikiList* mnModel::search(const char* se
86                  if(fp == NULL) {                  if(fp == NULL) {
87                          MN_FATAL_ERROR(wxT("fopen faild"));                          MN_FATAL_ERROR(wxT("fopen faild"));
88                  }                  }
89          while(1){  
90                  memset(buf, 0, MAX_BUF_SIZE);                  /* TYPE search */
91              fread(buf, MAX_BUF_SIZE-1, 1, fp);                  if(strstr(tokenList[0], TYPESEARCH_TAG) == tokenList[0])
92              if(buf[0] == 0) break;                  {
93                            found = typeSearch(tokenList[0], fp);
94                            if(found){
95                                    wikiData = new WikiData(wikiDataDir, (const char*)fileName->mb_str(), fp);
96                                    list->Append(wikiData);
97                            }
98                    }
99                    /* Normal search */
100                    else{
101                          decodeFileName = decode(fileName->mb_str());                          decodeFileName = decode(fileName->mb_str());
102                          wxString strbuf(buf, eucConv);                          snprintf(decodeFileNameBuf, MAX_BUF_SIZE, "%s", decodeFileName);
103                          wxString strSearch(searchStr, sysConv);                          toLower(decodeFileNameBuf);
104                          wxString strDecodeFileName(decodeFileName, eucConv);                          found = normalSearch(tokenList, fp, decodeFileNameBuf);
105                          wxRegEx regStr(strSearch, wxRE_DEFAULT|wxRE_NEWLINE|wxRE_ICASE);                          if(found){
                         if(regStr.Matches(strbuf) || regStr.Matches(strDecodeFileName)){  
106                                  wikiData = new WikiData(wikiDataDir, (const char*)fileName->mb_str(), fp);                                  wikiData = new WikiData(wikiDataDir, (const char*)fileName->mb_str(), fp);
107                                  list->Append(wikiData);                                  list->Append(wikiData);
                                 break;  
108                          }                          }
             buf[0] = 0;  
109                  }                  }
110                  fclose(fp);                  fclose(fp);
111          cont = dir->GetNext(fileName);          cont = dir->GetNext(fileName);
112          }          }
113          delete dir;          delete dir;
114          delete fileName;          delete fileName;
115            for(i = 0; tokenList[i] != NULL; i++) free(tokenList[i]);
116    
117          list->Sort(compWikiData);          list->Sort(compWikiData);
118          return list;          return list;
119  }  }
120    
121    bool mnModel::normalSearch(char* tokenList[], FILE*fp, char* decodeFileNameBuf)
122    {
123        char        buf[MAX_BUF_SIZE];
124            bool        found;
125            int         i;
126            while(1){
127            memset(buf, 0, MAX_BUF_SIZE);
128            fread(buf, MAX_BUF_SIZE-1, 1, fp);
129            if(buf[0] == 0) break;
130                    toLower(buf);
131                    found = TRUE;
132                    for(i = 0; tokenList[i] != NULL; i++){
133                            toLower(tokenList[i]);
134                            if(strstr((const char*)buf, (const char*)tokenList[i]) ||                 /* search in file context */
135                                    strstr((const char*)decodeFileNameBuf, (const char*)tokenList[i]) ||  /* search in file name    */
136                                    strcmp((const char*)tokenList[i], (const char*)ALLMEMO_TAG) == 0) {   /* SHOW  ALL MEMO         */
137                                    found = TRUE;
138                            }
139                            else {
140                                    found = FALSE;
141                                    break;
142                            }
143                    }
144    
145                    if(found){ /* all tokens found */
146                            break;
147                    }
148            buf[0] = 0;
149            }
150    
151            return found;
152    }
153    
154    bool mnModel::typeSearch(char* typeStr, FILE*fp)
155    {
156        char        buf[MAX_BUF_SIZE];
157            bool        found;
158            int         i;
159            char*       typeToken;
160            char        typeStrCopy[MAX_BUF_SIZE];
161    
162            snprintf(typeStrCopy, MAX_BUF_SIZE, "%s", typeStr);
163            while(1){
164            memset(buf, 0, MAX_BUF_SIZE);
165            fgets(buf, MAX_BUF_SIZE, fp);
166            if(buf[0] == 0) break;
167                    if(strstr((const char*)buf, TYPE_TAG)){  /* search TYPE line */
168                            typeToken = strtok(typeStrCopy, ":");
169                            typeToken = strtok(NULL, ":");    /* second field separated by colon(:) */
170                            toLower(typeToken);
171                            toLower(buf);
172                            if(strstr(buf, typeToken)) return true;
173                    }
174            }
175            return false;
176    }
177    
178  void mnModel::addSearchStr(wxString* searchStr)  void mnModel::addSearchStr(wxString* searchStr)
179  {  {
180          wxString *string;          wxString *string;
181    
182          if(searchStrList->Index(searchStr->c_str()) == wxNOT_FOUND){          if(searchStrList->Index(searchStr->c_str()) == wxNOT_FOUND){
183                  string = new wxString(searchStr->c_str());                  string = new wxString(searchStr->c_str());
184                  searchStrList->Add(*string, 1);                  //searchStrList->Add(*string, 1);
185                    searchStrList->Insert(*string, 0);
186          }          }
187  }  }
188    
# Line 245  void WikiData::modSubject(wxString* newS Line 337  void WikiData::modSubject(wxString* newS
337          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());
338    
339          if((fp = fopen(newFullPath, "r")) == NULL) {          if((fp = fopen(newFullPath, "r")) == NULL) {
340                  rename(oldFullPath, newFullPath);                  if(rename(oldFullPath, newFullPath) < 0) wxLogMessage(wxT("rename error: errno=[%d]"), errno);
341          }          }
342          else if(strcmp(oldFullPath, newFullPath)){          else if(strcmp(oldFullPath, newFullPath)){
343                  wxLogMessage(wxT("File has already exist. [%s]"), fileName->c_str());                  wxLogMessage(wxT("File has already exist. [%s]"), fileName->c_str());
344                  fclose(fp);                  fclose(fp);
345          }          }
346            else {
347                    fclose(fp);
348            }
349    
350          delete oldSubject;          delete oldSubject;
351          delete oldFileName;          delete oldFileName;
# Line 294  const wxString* WikiData::getText() Line 389  const wxString* WikiData::getText()
389          }          }
390    
391          while(fgets(buf, MAX_BUF_SIZE, fp)) {          while(fgets(buf, MAX_BUF_SIZE, fp)) {
392    #ifdef __WXMAC__
393                    for(int i = 0; buf[i] != 0; i++) if(buf[i] == (char)MAC_BACKSLASH) buf[i] = '\\';
394    #endif
395                  inbufPtr = buf;                  inbufPtr = buf;
396                  inbufSize = sizeof(buf);                  inbufSize = sizeof(buf);
397                  outbufPtr = outbuf;                  outbufPtr = outbuf;
# Line 354  void WikiData::save() Line 452  void WikiData::save()
452    
453          memset(inbuf, 0, sizeof(inbuf));          memset(inbuf, 0, sizeof(inbuf));
454          strcpy(inbuf,(const char*)text->mb_str());          strcpy(inbuf,(const char*)text->mb_str());
455    
456    #ifdef __WXMAC__
457            for(int i = 0; inbuf[i] != 0; i++) if(inbuf[i] == (char)MAC_BACKSLASH) inbuf[i] = '\\';
458    #endif
459    
460          inbufPtr = inbuf;          inbufPtr = inbuf;
461          inbufSize = strlen(inbufPtr);          inbufSize = strlen(inbufPtr);
462          outbufPtr = outbuf;          outbufPtr = outbuf;
# Line 367  void WikiData::save() Line 470  void WikiData::save()
470    
471  /******* Tools ************************/  /******* Tools ************************/
472    
473    static void toLower(char* string)
474    {
475            int i;
476    
477            for(i = 0; string[i] != 0; i++) {
478                    string[i] = tolower(string[i]);
479            }
480    }
481    
482    
483  static char* decode(const char* string)  static char* decode(const char* string)
484  {  {
485          static char buf[MAX_BUF_SIZE];          static char buf[MAX_BUF_SIZE];

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.19

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