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.4 by maloninc, Thu Aug 4 05:10:02 2005 UTC revision 1.20 by maloninc, Tue Sep 13 11:17:03 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          wxRegEx*        regStr;          const char* decodeFileName;
44            char decodeFileNameBuf[MAX_BUF_SIZE];
45          iconv_t     codeSet;          iconv_t     codeSet;
46          char        outbuf[MAX_BUF_SIZE];          char        outbuf[MAX_BUF_SIZE];
47          const char*       inbufPtr  = searchStr;          const char*       inbufPtr  = searchStr;
48          char*       outbufPtr = outbuf;          char*       outbufPtr = outbuf;
49          int         inbufSize = strlen(searchStr);          int         inbufSize = strlen(searchStr);
50          int         outbufSize = sizeof(outbuf);          int         outbufSize = sizeof(outbuf);
51          const char* decodeFileName;          char*       token;
52            char*       tokenList[32];
53            bool        found;
54    
55            memset(tokenList, 0, sizeof(char*)*32);
56          memset(outbuf, 0, outbufSize);          memset(outbuf, 0, outbufSize);
57          codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);          codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);
58          if(codeSet == (iconv_t)-1) {          if(codeSet == (iconv_t)-1) {
59                  MN_FATAL_ERROR(wxT("failed iconv_open"));                  MN_FATAL_ERROR(wxT("failed iconv_open"));
60          }          }
61          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);
62          iconv_close(codeSet);          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() )
78      {      {
# Line 70  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                  if(strstr((const char*)buf, (const char*)outbuf) ||                          snprintf(decodeFileNameBuf, MAX_BUF_SIZE, "%s", decodeFileName);
103                                  strstr((const char*)decodeFileName, (const char*)outbuf)) {                          toLower(decodeFileNameBuf);
104                            found = normalSearch(tokenList, fp, decodeFileNameBuf);
105                            if(found){
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;
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    void mnModel::group()
122    {
123            wxCSConv    conv(wxT(CODE_SET_SYSTEM));
124        char        buf[MAX_BUF_SIZE];
125            wxDir*      dir;
126            FILE*       fp;
127        wxString*   fileName = new wxString();
128        wxString    fullPathName;
129            int         typeTagLen = strlen(TYPE_TAG);
130            char*       token;
131            char*           inbufPtr;
132            int         inbufSize;
133            char        outbuf[MAX_BUF_SIZE];
134            char*       outbufPtr = outbuf;
135            int         outbufSize;
136            wxString*   typeToken;
137            char*       ptr;
138            int         i;
139    
140            iconv_t codeSet = iconv_open(CODE_SET_SYSTEM, CODE_SET_EUC_JP);
141            if(codeSet == (iconv_t)-1) {
142                    MN_FATAL_ERROR(wxT("failed iconv_open"));
143            }
144    
145            dir = new wxDir(*wikiDataDir);
146        if ( !dir->IsOpened() )
147        {
148                    MN_FATAL_ERROR(wxT("wxDir has faild\n"));
149            return ;
150        }
151        bool cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES);
152            while(cont){
153            fullPathName = *wikiDataDir + wxT("/") + *fileName;
154                    fp = fopen((const char*)fullPathName.mb_str(), "r");
155                    if(fp == NULL) {
156                            MN_FATAL_ERROR(wxT("fopen faild"));
157                    }
158                    while(1) {
159                            memset(buf, 0, MAX_BUF_SIZE);
160                            fgets(buf, MAX_BUF_SIZE, fp);
161                            if(buf[0] == 0) break;
162                            if(strstr(buf, TYPE_TAG)){
163                                    ptr = &buf[typeTagLen];
164                                    while((token = strtok(ptr, " /\r\n:"))!= NULL) {
165                                            memset(outbuf, 0, MAX_BUF_SIZE);
166                                            snprintf(outbuf, MAX_BUF_SIZE, "%s", TYPESEARCH_TAG);
167                                            inbufPtr = token;
168                                            inbufSize = strlen(token);
169                                            outbufPtr = &outbuf[strlen(TYPESEARCH_TAG)];
170                                            outbufSize = sizeof(outbuf) - strlen(TYPESEARCH_TAG);
171                                            iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
172    
173                                            typeToken = new wxString(outbuf, conv);
174                                            //wxLogMessage(wxT("[%s]"), typeToken->c_str());
175                                            addSearchStr(typeToken);
176                                            delete typeToken;
177                                            ptr = NULL;
178                                    }
179                            }
180                    }
181                    fclose(fp);
182            cont = dir->GetNext(fileName);
183            }
184            delete dir;
185            delete fileName;
186            iconv_close(codeSet);
187    }
188    
189    bool mnModel::normalSearch(char* tokenList[], FILE*fp, char* decodeFileNameBuf)
190    {
191        char        buf[MAX_BUF_SIZE];
192            bool        found;
193            int         i;
194            while(1){
195            memset(buf, 0, MAX_BUF_SIZE);
196            fread(buf, MAX_BUF_SIZE-1, 1, fp);
197            if(buf[0] == 0) break;
198                    toLower(buf);
199                    found = TRUE;
200                    for(i = 0; tokenList[i] != NULL; i++){
201                            toLower(tokenList[i]);
202                            if(strstr((const char*)buf, (const char*)tokenList[i]) ||                 /* search in file context */
203                                    strstr((const char*)decodeFileNameBuf, (const char*)tokenList[i]) ||  /* search in file name    */
204                                    strcmp((const char*)tokenList[i], (const char*)ALLMEMO_TAG) == 0) {   /* SHOW  ALL MEMO         */
205                                    found = TRUE;
206                            }
207                            else {
208                                    found = FALSE;
209                                    break;
210                            }
211                    }
212    
213                    if(found){ /* all tokens found */
214                            break;
215                    }
216            buf[0] = 0;
217            }
218    
219            return found;
220    }
221    
222    bool mnModel::typeSearch(char* typeStr, FILE*fp)
223    {
224        char        buf[MAX_BUF_SIZE];
225            bool        found;
226            int         i;
227            char*       typeToken;
228            char        typeStrCopy[MAX_BUF_SIZE];
229    
230            snprintf(typeStrCopy, MAX_BUF_SIZE, "%s", typeStr);
231            while(1){
232            memset(buf, 0, MAX_BUF_SIZE);
233            fgets(buf, MAX_BUF_SIZE, fp);
234            if(buf[0] == 0) break;
235                    if(strstr((const char*)buf, TYPE_TAG)){  /* search TYPE line */
236                            typeToken = strtok(typeStrCopy, ":");
237                            typeToken = strtok(NULL, ":");    /* second field separated by colon(:) */
238                            toLower(typeToken);
239                            toLower(buf);
240                            if(strstr(buf, typeToken)) return true;
241                    }
242            }
243            return false;
244    }
245    
246  void mnModel::addSearchStr(wxString* searchStr)  void mnModel::addSearchStr(wxString* searchStr)
247  {  {
248          wxString *string;          wxString *string;
249    
250          if(searchStrList->Index(searchStr->c_str()) == wxNOT_FOUND){          if(searchStrList->Index(searchStr->c_str()) == wxNOT_FOUND){
251                  string = new wxString(searchStr->c_str());                  string = new wxString(searchStr->c_str());
252                  searchStrList->Add(*string, 1);                  //searchStrList->Add(*string, 1);
253                    searchStrList->Insert(*string, 0);
254          }          }
255  }  }
256    
# Line 159  WikiData::WikiData(wxString* dataDir, co Line 311  WikiData::WikiData(wxString* dataDir, co
311          decodeStr = decode(file);          decodeStr = decode(file);
312          inbufSize = strlen(decodeStr);          inbufSize = strlen(decodeStr);
313          outbufSize = sizeof(outbuf);          outbufSize = sizeof(outbuf);
314          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);
315          subject  = new wxString((const char*)outbuf, conv);          subject  = new wxString((const char*)outbuf, conv);
316          iconv_close(codeSet);          iconv_close(codeSet);
317    
# Line 243  void WikiData::modSubject(wxString* newS Line 395  void WikiData::modSubject(wxString* newS
395          if(codeSet == (iconv_t)-1) {          if(codeSet == (iconv_t)-1) {
396                  MN_FATAL_ERROR(wxT("failed iconv_open"));                  MN_FATAL_ERROR(wxT("failed iconv_open"));
397          }          }
398          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);
399          iconv_close(codeSet);          iconv_close(codeSet);
400          subject  = new wxString(newSubject->c_str());          subject  = new wxString(newSubject->c_str());
401          fileName = new wxString(encode(outbuf), conv);          fileName = new wxString(encode(outbuf), conv);
# Line 253  void WikiData::modSubject(wxString* newS Line 405  void WikiData::modSubject(wxString* newS
405          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());
406    
407          if((fp = fopen(newFullPath, "r")) == NULL) {          if((fp = fopen(newFullPath, "r")) == NULL) {
408                  rename(oldFullPath, newFullPath);                  if(rename(oldFullPath, newFullPath) < 0) wxLogMessage(wxT("rename error: errno=[%d]"), errno);
409          }          }
410          else if(strcmp(oldFullPath, newFullPath)){          else if(strcmp(oldFullPath, newFullPath)){
411                  wxLogMessage(wxT("File has already exist. [%s]"), fileName->c_str());                  wxLogMessage(wxT("File has already exist. [%s]"), fileName->c_str());
412                  fclose(fp);                  fclose(fp);
413          }          }
414            else {
415                    fclose(fp);
416            }
417    
418          delete oldSubject;          delete oldSubject;
419          delete oldFileName;          delete oldFileName;
# Line 302  const wxString* WikiData::getText() Line 457  const wxString* WikiData::getText()
457          }          }
458    
459          while(fgets(buf, MAX_BUF_SIZE, fp)) {          while(fgets(buf, MAX_BUF_SIZE, fp)) {
460    #ifdef __WXMAC__
461                    for(int i = 0; buf[i] != 0; i++) if(buf[i] == (char)MAC_BACKSLASH) buf[i] = '\\';
462    #endif
463                  inbufPtr = buf;                  inbufPtr = buf;
464                  inbufSize = sizeof(buf);                  inbufSize = sizeof(buf);
465                  outbufPtr = outbuf;                  outbufPtr = outbuf;
466                  outbufSize = sizeof(outbuf);                  outbufSize = sizeof(outbuf);
467                  memset(outbuf, 0, outbufSize);                  memset(outbuf, 0, outbufSize);
468                  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);
469                  tmpStr = new wxString((char*)outbuf, conv);                  tmpStr = new wxString((char*)outbuf, conv);
470                  *text += *tmpStr;                  *text += *tmpStr;
471                  delete tmpStr;                  delete tmpStr;
# Line 362  void WikiData::save() Line 520  void WikiData::save()
520    
521          memset(inbuf, 0, sizeof(inbuf));          memset(inbuf, 0, sizeof(inbuf));
522          strcpy(inbuf,(const char*)text->mb_str());          strcpy(inbuf,(const char*)text->mb_str());
523    
524    #ifdef __WXMAC__
525            for(int i = 0; inbuf[i] != 0; i++) if(inbuf[i] == (char)MAC_BACKSLASH) inbuf[i] = '\\';
526    #endif
527    
528          inbufPtr = inbuf;          inbufPtr = inbuf;
529          inbufSize = strlen(inbufPtr);          inbufSize = strlen(inbufPtr);
530          outbufPtr = outbuf;          outbufPtr = outbuf;
531          outbufSize = sizeof(outbuf);          outbufSize = sizeof(outbuf);
532          memset(outbuf, 0, outbufSize);          memset(outbuf, 0, outbufSize);
533          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);
534          fwrite(outbuf, sizeof(outbuf)-outbufSize, 1, fp);          fwrite(outbuf, sizeof(outbuf)-outbufSize, 1, fp);
535          fclose(fp);          fclose(fp);
536          iconv_close(codeSet);          iconv_close(codeSet);
# Line 375  void WikiData::save() Line 538  void WikiData::save()
538    
539  /******* Tools ************************/  /******* Tools ************************/
540    
541    static void toLower(char* string)
542    {
543            int i;
544    
545            for(i = 0; string[i] != 0; i++) {
546                    string[i] = tolower(string[i]);
547            }
548    }
549    
550    
551  static char* decode(const char* string)  static char* decode(const char* string)
552  {  {
553          static char buf[MAX_BUF_SIZE];          static char buf[MAX_BUF_SIZE];

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.20

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