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.16 by maloninc, Mon Sep 12 21:44:33 2005 UTC revision 1.23 by maloninc, Thu Sep 15 09:20:48 2005 UTC
# Line 30  mnModel::~mnModel() Line 30  mnModel::~mnModel()
30          delete wikiDataDir;          delete wikiDataDir;
31  }  }
32    
33    /*
34  WikiList* mnModel::search(const char* searchStr)   * iconv encode and create token list,
35     * so you must free tokenLis after you used
36     *
37     * if failed to make token list, return FALSE.
38     */
39    bool mnModel::makeSearchToken(const char* searchStr, char* tokenList[])
40  {  {
         int         i;  
         wxDir*      dir;  
         FILE*       fp;  
     wxString    fullPathName;  
     char        buf[MAX_BUF_SIZE];  
         WikiData*   wikiData;  
     WikiList*   list = new WikiList();  
     wxString*   fileName = new wxString();  
41          iconv_t     codeSet;          iconv_t     codeSet;
42          char        outbuf[MAX_BUF_SIZE];          char        outbuf[MAX_BUF_SIZE];
43          const char*       inbufPtr  = searchStr;          const char* inbufPtr   = searchStr;
44          char*       outbufPtr = outbuf;          char*       outbufPtr  = outbuf;
45          int         inbufSize = strlen(searchStr);          int         inbufSize  = strlen(searchStr);
46          int         outbufSize = sizeof(outbuf);          int         outbufSize = sizeof(outbuf);
         const char* decodeFileName;  
         char decodeFileNameBuf[MAX_BUF_SIZE];  
47          char*       token;          char*       token;
48          char*       tokenList[32];          int         i;
         bool        found;  
49    
         memset(tokenList, 0, sizeof(char*)*32);  
50          memset(outbuf, 0, outbufSize);          memset(outbuf, 0, outbufSize);
51          codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);          codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);
52          if(codeSet == (iconv_t)-1) {          if(codeSet == (iconv_t)-1) {
# Line 64  WikiList* mnModel::search(const char* se Line 57  WikiList* mnModel::search(const char* se
57    
58          /* searchStr to Tokens */          /* searchStr to Tokens */
59          token = strtok(outbuf, " ");          token = strtok(outbuf, " ");
60          if(token == NULL) return list;          if(token == NULL) return false;
61          tokenList[0] = (char*)malloc(strlen(token)+1);          tokenList[0] = (char*)malloc(strlen(token)+1);
62          snprintf(tokenList[0], strlen(token)+1, "%s", token);          snprintf(tokenList[0], strlen(token)+1, "%s", token);
63          i = 1;          i = 1;
# Line 73  WikiList* mnModel::search(const char* se Line 66  WikiList* mnModel::search(const char* se
66                  snprintf(tokenList[i], strlen(token)+1, "%s", token);                  snprintf(tokenList[i], strlen(token)+1, "%s", token);
67                  i++;                  i++;
68          }          }
69            return true;
70    }
71    
72    bool mnModel::matchWithToken(wxString* fileName, char* tokenList[])
73    {
74            const char* decodeFileName;
75            char decodeFileNameBuf[MAX_BUF_SIZE];
76        wxString    fullPathName;
77            FILE*       fp;
78            bool        ans = false;
79            bool        found;
80    
81        fullPathName = *wikiDataDir + wxT("/") + *fileName;
82            fp = fopen((const char*)fullPathName.mb_str(), "r");
83            if(fp == NULL) {
84                    MN_FATAL_ERROR(wxT("fopen faild"));
85            }
86    
87            /* TYPE search */
88            if(strstr(tokenList[0], TYPESEARCH_TAG) == tokenList[0])
89            {
90                    found = typeSearch(tokenList[0], fp);
91                    if(found){
92                            ans = true;
93                    }
94            }
95            /* Normal search */
96            else{
97                    decodeFileName = decode(fileName->mb_str());
98                    snprintf(decodeFileNameBuf, MAX_BUF_SIZE, "%s", decodeFileName);
99                    toLower(decodeFileNameBuf);
100                    found = normalSearch(tokenList, fp, decodeFileNameBuf);
101                    if(found){
102                            ans = true;
103                    }
104            }
105            fclose(fp);
106    
107            return ans;
108    }
109    
110    WikiList* mnModel::search(const char* searchStr)
111    {
112            int         i;
113            wxDir*      dir;
114            WikiData*   wikiData;
115        WikiList*   list = new WikiList();
116        wxString*   fileName = new wxString();
117            char*       tokenList[32];
118    
119            memset(tokenList, 0, sizeof(char*)*32);
120            if( makeSearchToken(searchStr, tokenList) == false) return list;
121    
122          dir = new wxDir(*wikiDataDir);          dir = new wxDir(*wikiDataDir);
123      if ( !dir->IsOpened() )      if ( !dir->IsOpened() )
# Line 80  WikiList* mnModel::search(const char* se Line 125  WikiList* mnModel::search(const char* se
125                  MN_FATAL_ERROR(wxT("wxDir has faild\n"));                  MN_FATAL_ERROR(wxT("wxDir has faild\n"));
126          return NULL;          return NULL;
127      }      }
128    
129        bool cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES);
130            while(cont){
131    
132                    if( matchWithToken(fileName, tokenList) ) { /* match with token list */
133                            wikiData = new WikiData(wikiDataDir, fileName);
134                            list->Append(wikiData);
135                    }
136    
137            cont = dir->GetNext(fileName);
138            }
139    
140            delete dir;
141            delete fileName;
142            for(i = 0; tokenList[i] != NULL; i++) free(tokenList[i]);
143    
144            list->Sort(compWikiData);
145            return list;
146    }
147    
148    void mnModel::group()
149    {
150            wxCSConv    conv(wxT(CODE_SET_SYSTEM));
151        char        buf[MAX_BUF_SIZE];
152            wxDir*      dir;
153            FILE*       fp;
154        wxString*   fileName = new wxString();
155        wxString    fullPathName;
156            int         typeTagLen = strlen(TYPE_TAG);
157            char*       token;
158            char*           inbufPtr;
159            int         inbufSize;
160            char        outbuf[MAX_BUF_SIZE];
161            char*       outbufPtr = outbuf;
162            int         outbufSize;
163            wxString*   typeToken;
164            char*       ptr;
165            int         i;
166    
167            iconv_t codeSet = iconv_open(CODE_SET_SYSTEM, CODE_SET_EUC_JP);
168            if(codeSet == (iconv_t)-1) {
169                    MN_FATAL_ERROR(wxT("failed iconv_open"));
170            }
171    
172            dir = new wxDir(*wikiDataDir);
173        if ( !dir->IsOpened() )
174        {
175                    MN_FATAL_ERROR(wxT("wxDir has faild\n"));
176            return ;
177        }
178      bool cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES);      bool cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES);
179          while(cont){          while(cont){
180          fullPathName = *wikiDataDir + wxT("/") + *fileName;          fullPathName = *wikiDataDir + wxT("/") + *fileName;
# Line 87  WikiList* mnModel::search(const char* se Line 182  WikiList* mnModel::search(const char* se
182                  if(fp == NULL) {                  if(fp == NULL) {
183                          MN_FATAL_ERROR(wxT("fopen faild"));                          MN_FATAL_ERROR(wxT("fopen faild"));
184                  }                  }
185                    while(1) {
186          while(1){                          memset(buf, 0, MAX_BUF_SIZE);
187                  memset(buf, 0, MAX_BUF_SIZE);                          fgets(buf, MAX_BUF_SIZE, fp);
188              fread(buf, MAX_BUF_SIZE-1, 1, fp);                          if(buf[0] == 0) break;
189              if(buf[0] == 0) break;                          if(strstr(buf, TYPE_TAG)){
190                          decodeFileName = decode(fileName->mb_str());                                  ptr = &buf[typeTagLen];
191                          snprintf(decodeFileNameBuf, MAX_BUF_SIZE, "%s", decodeFileName);                                  while((token = strtok(ptr, " /\r\n:"))!= NULL) {
192                          toLower(buf);                                          memset(outbuf, 0, MAX_BUF_SIZE);
193                          toLower(decodeFileNameBuf);                                          snprintf(outbuf, MAX_BUF_SIZE, "%s", TYPESEARCH_TAG);
194                          found = TRUE;                                          inbufPtr = token;
195                          for(i = 0; tokenList[i] != NULL; i++){                                          inbufSize = strlen(token);
196                                  toLower(tokenList[i]);                                          outbufPtr = &outbuf[strlen(TYPESEARCH_TAG)];
197                                  if(strstr((const char*)buf, (const char*)tokenList[i]) ||                                          outbufSize = sizeof(outbuf) - strlen(TYPESEARCH_TAG);
198                                          strstr((const char*)decodeFileNameBuf, (const char*)tokenList[i])) {                                          iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
199                                          found = TRUE;  
200                                  }                                          typeToken = new wxString(outbuf, conv);
201                                  else {                                          addSearchStr(typeToken);
202                                          found = FALSE;                                          delete typeToken;
203                                          break;                                          ptr = NULL;
204                                  }                                  }
205                          }                          }
   
                         if(found){  
                                 wikiData = new WikiData(wikiDataDir, (const char*)fileName->mb_str(), fp);  
                                 list->Append(wikiData);  
                                 break;  
                         }  
             buf[0] = 0;  
206                  }                  }
207                  fclose(fp);                  fclose(fp);
208          cont = dir->GetNext(fileName);          cont = dir->GetNext(fileName);
209          }          }
210          delete dir;          delete dir;
211          delete fileName;          delete fileName;
212          for(i = 0; tokenList[i] != NULL; i++) free(tokenList[i]);          iconv_close(codeSet);
213    }
214    
215          list->Sort(compWikiData);  bool mnModel::normalSearch(char* tokenList[], FILE*fp, char* decodeFileNameBuf)
216          return list;  {
217        char        buf[MAX_BUF_SIZE];
218            bool        found;
219            int         i;
220            while(1){
221            memset(buf, 0, MAX_BUF_SIZE);
222            fread(buf, MAX_BUF_SIZE-1, 1, fp);
223            if(buf[0] == 0) break;
224                    toLower(buf);
225                    found = TRUE;
226                    for(i = 0; tokenList[i] != NULL; i++){
227                            toLower(tokenList[i]);
228                            if(strstr((const char*)buf, (const char*)tokenList[i]) ||                 /* search in file context */
229                                    strstr((const char*)decodeFileNameBuf, (const char*)tokenList[i]) ||  /* search in file name    */
230                                    strcmp((const char*)tokenList[i], (const char*)ALLMEMO_TAG) == 0) {   /* SHOW  ALL MEMO         */
231                                    found = TRUE;
232                            }
233                            else {
234                                    found = FALSE;
235                                    break;
236                            }
237                    }
238    
239                    if(found){ /* all tokens found */
240                            break;
241                    }
242            buf[0] = 0;
243            }
244    
245            return found;
246    }
247    
248    bool mnModel::typeSearch(char* typeStr, FILE*fp)
249    {
250        char        buf[MAX_BUF_SIZE];
251            bool        found;
252            int         i;
253            char*       typeToken;
254            char        typeStrCopy[MAX_BUF_SIZE];
255    
256            snprintf(typeStrCopy, MAX_BUF_SIZE, "%s", typeStr);
257            while(1){
258            memset(buf, 0, MAX_BUF_SIZE);
259            fgets(buf, MAX_BUF_SIZE, fp);
260            if(buf[0] == 0) break;
261                    if(strstr((const char*)buf, TYPE_TAG)){  /* search TYPE line */
262                            typeToken = strtok(typeStrCopy, ":");
263                            typeToken = strtok(NULL, ":");    /* second field separated by colon(:) */
264                            if(typeToken == NULL) return false;
265                            toLower(typeToken);
266                            toLower(buf);
267                            if(strstr(buf, typeToken)) return true;
268                    }
269            }
270            return false;
271  }  }
272    
273  void mnModel::addSearchStr(wxString* searchStr)  void mnModel::addSearchStr(wxString* searchStr)
# Line 134  void mnModel::addSearchStr(wxString* sea Line 277  void mnModel::addSearchStr(wxString* sea
277          if(searchStrList->Index(searchStr->c_str()) == wxNOT_FOUND){          if(searchStrList->Index(searchStr->c_str()) == wxNOT_FOUND){
278                  string = new wxString(searchStr->c_str());                  string = new wxString(searchStr->c_str());
279                  searchStrList->Add(*string, 1);                  searchStrList->Add(*string, 1);
280                    //searchStrList->Insert(*string, 0);
281          }          }
282  }  }
283    
284    void mnModel::addSearchList(wxString* searchStr, WikiList* list)
285    {
286            wikiHash[*searchStr] = list;
287    }
288    
289  void mnModel::removeSearchStr(wxString searchStr)  void mnModel::removeSearchStr(wxString searchStr)
290  {  {
291          if(searchStrList->Index(searchStr.c_str()) != wxNOT_FOUND) {          if(searchStrList->Index(searchStr.c_str()) != wxNOT_FOUND) {
# Line 169  WikiData* mnModel::newWikiData() Line 318  WikiData* mnModel::newWikiData()
318    
319          return data;          return data;
320  }  }
321    const WikiList* mnModel::getSearchResultList(wxString* searchStr)
322    {
323            return wikiHash[*searchStr];
324    }
325    
326    /* add "addData's" clone. if already exist same data, overwrite it*/
327    void mnModel::addSearchResultList(wxString* searchStr, WikiData* addData)
328    {
329            WikiList* wikiList = wikiHash[*searchStr];
330            WikiList::Node* node;
331            WikiData* data;
332    
333            node = wikiList->GetFirst();
334            if(!node) {
335                    MN_FATAL_ERROR(wxT("Search Result List is empty"));
336            }
337    
338            while(node) {
339                    data = node->GetData();
340                    if(data == addData) return;
341                    if( *(data->getSubject()) == *(addData->getSubject()) ) {
342                            if(wikiList->DeleteObject(data)) {
343                                    delete data;
344                                    break;
345                            }
346                            else {
347                                    MN_FATAL_ERROR(wxT("Can't find delete data"));
348                            }
349                    }
350                    node = node->GetNext();
351            }
352            WikiData* copy = new WikiData((wxString*)wikiDataDir, (wxString*)addData->getFileName());
353            wikiList->Append(copy);
354            wikiList->Sort(compWikiData);
355    }
356    
357  /******* WikiData ************************/  /******* WikiData ************************/
358  WikiData::WikiData(wxString* dataDir, const char* file, FILE* fp)  WikiData::WikiData(wxString* dataDir, wxString* inFileName)
359  {  {
360            FILE* fp;
361        wxString    fullPathName;
362          char* decodeStr;          char* decodeStr;
363          char  buf[MAX_BUF_SIZE];          char  buf[MAX_BUF_SIZE];
364          char* inbuf;          char* inbuf;
# Line 189  WikiData::WikiData(wxString* dataDir, co Line 375  WikiData::WikiData(wxString* dataDir, co
375    
376          text = NULL;          text = NULL;
377          memset(outbuf, 0, MAX_BUF_SIZE);          memset(outbuf, 0, MAX_BUF_SIZE);
378          fileName = new wxString((const char*)file, conv);          fileName = new wxString(*inFileName);
379          dataDirName = dataDir;          dataDirName = dataDir;
380          decodeStr = decode(file);          decodeStr = decode(fileName->mb_str());
381          inbufSize = strlen(decodeStr);          inbufSize = strlen(decodeStr);
382          outbufSize = sizeof(outbuf);          outbufSize = sizeof(outbuf);
383          iconv(codeSet, (ICONV_CONST char**)&decodeStr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);          iconv(codeSet, (ICONV_CONST char**)&decodeStr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
# Line 200  WikiData::WikiData(wxString* dataDir, co Line 386  WikiData::WikiData(wxString* dataDir, co
386    
387          date     = NULL;          date     = NULL;
388    
389      rewind(fp);      fullPathName = *dataDir + wxT("/") + *fileName;
390            fp = fopen((const char*)fullPathName.mb_str(), "r");
391            if(fp == NULL) {
392                    MN_FATAL_ERROR(wxT("fopen faild"));
393            }
394          while(fgets(buf, MAX_BUF_SIZE, fp)) {          while(fgets(buf, MAX_BUF_SIZE, fp)) {
395                  if(strstr( (const char*)buf, (const char*)DATE_TAG)) {                  if(strstr( (const char*)buf, (const char*)DATE_TAG)) {
396                          strtok(buf, "\n");                          strtok(buf, "\n");
# Line 209  WikiData::WikiData(wxString* dataDir, co Line 399  WikiData::WikiData(wxString* dataDir, co
399                          break;                          break;
400                  }                  }
401          }          }
402            fclose(fp);
403  }  }
404    
405  WikiData::WikiData(wxString* dataDir) {  WikiData::WikiData(wxString* dataDir) {

Legend:
Removed from v.1.16  
changed lines
  Added in v.1.23

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