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.19 by maloninc, Tue Sep 13 09:11:37 2005 UTC revision 1.24 by maloninc, Fri Sep 16 04:12:58 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;  
         WikiData*   wikiData;  
     WikiList*   list = new WikiList();  
     wxString*   fileName = new wxString();  
         const char* decodeFileName;  
         char decodeFileNameBuf[MAX_BUF_SIZE];  
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);
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 63  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 72  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                    return false;  /* because of removed */
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 79  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    /*
149     *  add malon-type:xxx to search list
150     */
151    void mnModel::group()
152    {
153            wxCSConv    conv(wxT(CODE_SET_SYSTEM));
154        char        buf[MAX_BUF_SIZE];
155            wxDir*      dir;
156            FILE*       fp;
157        wxString*   fileName = new wxString();
158        wxString    fullPathName;
159            int         typeTagLen = strlen(TYPE_TAG);
160            char*       token;
161            char*           inbufPtr;
162            int         inbufSize;
163            char        outbuf[MAX_BUF_SIZE];
164            char*       outbufPtr = outbuf;
165            int         outbufSize;
166            wxString*   typeToken;
167            char*       ptr;
168            int         i;
169    
170            iconv_t codeSet = iconv_open(CODE_SET_SYSTEM, CODE_SET_EUC_JP);
171            if(codeSet == (iconv_t)-1) {
172                    MN_FATAL_ERROR(wxT("failed iconv_open"));
173            }
174    
175            dir = new wxDir(*wikiDataDir);
176        if ( !dir->IsOpened() )
177        {
178                    MN_FATAL_ERROR(wxT("wxDir has faild\n"));
179            return ;
180        }
181      bool cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES);      bool cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES);
182          while(cont){          while(cont){
183          fullPathName = *wikiDataDir + wxT("/") + *fileName;          fullPathName = *wikiDataDir + wxT("/") + *fileName;
# Line 86  WikiList* mnModel::search(const char* se Line 185  WikiList* mnModel::search(const char* se
185                  if(fp == NULL) {                  if(fp == NULL) {
186                          MN_FATAL_ERROR(wxT("fopen faild"));                          MN_FATAL_ERROR(wxT("fopen faild"));
187                  }                  }
188                    while(1) {
189                  /* TYPE search */                          memset(buf, 0, MAX_BUF_SIZE);
190                  if(strstr(tokenList[0], TYPESEARCH_TAG) == tokenList[0])                          fgets(buf, MAX_BUF_SIZE, fp);
191                  {                          if(buf[0] == 0) break;
192                          found = typeSearch(tokenList[0], fp);                          if(strstr(buf, TYPE_TAG)){
193                          if(found){                                  ptr = &buf[typeTagLen];
194                                  wikiData = new WikiData(wikiDataDir, (const char*)fileName->mb_str(), fp);                                  while((token = strtok(ptr, " /\r\n:"))!= NULL) {
195                                  list->Append(wikiData);                                          memset(outbuf, 0, MAX_BUF_SIZE);
196                          }                                          snprintf(outbuf, MAX_BUF_SIZE, "%s", TYPESEARCH_TAG);
197                  }                                          inbufPtr = token;
198                  /* Normal search */                                          inbufSize = strlen(token);
199                  else{                                          outbufPtr = &outbuf[strlen(TYPESEARCH_TAG)];
200                          decodeFileName = decode(fileName->mb_str());                                          outbufSize = sizeof(outbuf) - strlen(TYPESEARCH_TAG);
201                          snprintf(decodeFileNameBuf, MAX_BUF_SIZE, "%s", decodeFileName);                                          iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
202                          toLower(decodeFileNameBuf);  
203                          found = normalSearch(tokenList, fp, decodeFileNameBuf);                                          typeToken = new wxString(outbuf, conv);
204                          if(found){                                          if( addSearchStr(typeToken) ) {
205                                  wikiData = new WikiData(wikiDataDir, (const char*)fileName->mb_str(), fp);                                                  WikiList* wikiList = search(typeToken->mb_str());
206                                  list->Append(wikiData);                                                  addSearchList(typeToken, wikiList);
207                                            }
208                                            delete typeToken;
209                                            ptr = NULL;
210                                    }
211                          }                          }
212                  }                  }
213                  fclose(fp);                  fclose(fp);
# Line 112  WikiList* mnModel::search(const char* se Line 215  WikiList* mnModel::search(const char* se
215          }          }
216          delete dir;          delete dir;
217          delete fileName;          delete fileName;
218          for(i = 0; tokenList[i] != NULL; i++) free(tokenList[i]);          iconv_close(codeSet);
   
         list->Sort(compWikiData);  
         return list;  
219  }  }
220    
221  bool mnModel::normalSearch(char* tokenList[], FILE*fp, char* decodeFileNameBuf)  bool mnModel::normalSearch(char* tokenList[], FILE*fp, char* decodeFileNameBuf)
# Line 167  bool mnModel::typeSearch(char* typeStr, Line 267  bool mnModel::typeSearch(char* typeStr,
267                  if(strstr((const char*)buf, TYPE_TAG)){  /* search TYPE line */                  if(strstr((const char*)buf, TYPE_TAG)){  /* search TYPE line */
268                          typeToken = strtok(typeStrCopy, ":");                          typeToken = strtok(typeStrCopy, ":");
269                          typeToken = strtok(NULL, ":");    /* second field separated by colon(:) */                          typeToken = strtok(NULL, ":");    /* second field separated by colon(:) */
270                            if(typeToken == NULL) return false;
271                          toLower(typeToken);                          toLower(typeToken);
272                          toLower(buf);                          toLower(buf);
273                          if(strstr(buf, typeToken)) return true;                          if(strstr(buf, typeToken)) return true;
# Line 175  bool mnModel::typeSearch(char* typeStr, Line 276  bool mnModel::typeSearch(char* typeStr,
276          return false;          return false;
277  }  }
278    
279  void mnModel::addSearchStr(wxString* searchStr)  bool mnModel::addSearchStr(wxString* searchStr)
280  {  {
281          wxString *string;          wxString *string;
282    
283          if(searchStrList->Index(searchStr->c_str()) == wxNOT_FOUND){          if(searchStrList->Index(searchStr->c_str()) == wxNOT_FOUND){
284                  string = new wxString(searchStr->c_str());                  string = new wxString(searchStr->c_str());
285                  //searchStrList->Add(*string, 1);                  searchStrList->Add(*string, 1);
286                  searchStrList->Insert(*string, 0);                  //searchStrList->Insert(*string, 0);
287                    return true;  /* Add */
288          }          }
289            return false; /* does'nt add because of duplicating */
290    }
291    
292    void mnModel::addSearchList(wxString* searchStr, WikiList* list)
293    {
294            wikiHash[*searchStr] = list;
295  }  }
296    
297  void mnModel::removeSearchStr(wxString searchStr)  void mnModel::removeSearchStr(wxString searchStr)
298  {  {
299          if(searchStrList->Index(searchStr.c_str()) != wxNOT_FOUND) {          if(searchStrList->Index(searchStr.c_str()) != wxNOT_FOUND) {
300                  searchStrList->Remove(searchStr.c_str());                  searchStrList->Remove(searchStr.c_str());
301                    wikiHash[*searchStr] = NULL;
302          }          }
303  }  }
304    
# Line 202  void mnModel::modSearchStr(wxString* old Line 311  void mnModel::modSearchStr(wxString* old
311                  itemStr.sprintf(wxT("%s"), newStr->c_str());                  itemStr.sprintf(wxT("%s"), newStr->c_str());
312          }          }
313  }  }
314    
315    void mnModel::clearSearchStrList()
316    {
317            searchStrList->Clear();
318    }
319    
320    void mnModel::clearSearchResultList()
321    {
322            wikiHash.clear();
323    }
324    
325  const wxString* mnModel::getWikiDataDir()  const wxString* mnModel::getWikiDataDir()
326  {  {
327          return wikiDataDir;          return wikiDataDir;
# Line 218  WikiData* mnModel::newWikiData() Line 338  WikiData* mnModel::newWikiData()
338    
339          return data;          return data;
340  }  }
341    const WikiList* mnModel::getSearchResultList(wxString* searchStr)
342    {
343            return wikiHash[*searchStr];
344    }
345    
346    /* add "addData's" clone. if already exist same data, overwrite it*/
347    void mnModel::addSearchResultList(wxString* searchStr, WikiData* addData)
348    {
349            WikiList* wikiList = wikiHash[*searchStr];
350            WikiList::Node* node;
351            WikiData* data;
352    
353            if(wikiList == NULL) {
354                    MN_FATAL_ERROR(wxT("wikiList is null"));
355                    return;
356            }
357    
358            node = wikiList->GetFirst();
359            while(node) {
360                    data = node->GetData();
361                    if(data == addData) return;
362                    if( *(data->getSubject()) == *(addData->getOldSubject()) ) {
363                            if(wikiList->DeleteObject(data)) {
364                                    //delete data; /* may be wrong.. */
365                                    break;
366                            }
367                            else {
368                                    MN_FATAL_ERROR(wxT("Can't find delete data"));
369                            }
370                    }
371                    node = node->GetNext();
372            }
373            WikiData* copy = new WikiData((wxString*)wikiDataDir, (wxString*)addData->getFileName());
374            wikiList->Append(copy);
375            wikiList->Sort(compWikiData);
376    }
377    
378    bool mnModel::delSearchResultList(wxString* searchStr, WikiData* delData)
379    {
380            WikiList* wikiList = wikiHash[*searchStr];
381            WikiList::Node* node;
382            WikiData* data;
383            bool      found = false;
384    
385            if(wikiList == NULL) {
386                    MN_FATAL_ERROR(wxT("wikiList is null"));
387                    return false;
388            }
389    
390            node = wikiList->GetFirst();
391            while(node) {
392                    data = node->GetData();
393                    if(data == delData) {
394                            if(!wikiList->DeleteObject(data)) {
395                                    MN_FATAL_ERROR(wxT("Can't find delete data"));
396                            }
397                            found = true;
398                            break;
399                    }
400                    else if( *(data->getSubject()) == *(delData->getOldSubject()) ) {
401                            if(wikiList->DeleteObject(data)) {
402                                    //delete data;  /* may be wrong.. */
403                                    found = true;
404                                    break;
405                            }
406                            else {
407                                    MN_FATAL_ERROR(wxT("Can't find delete data"));
408                            }
409                    }
410                    node = node->GetNext();
411            }
412            wikiList->Sort(compWikiData);
413            return found;
414    }
415    
416  /******* WikiData ************************/  /******* WikiData ************************/
417  WikiData::WikiData(wxString* dataDir, const char* file, FILE* fp)  WikiData::WikiData(wxString* dataDir, wxString* inFileName)
418  {  {
419            FILE* fp;
420        wxString    fullPathName;
421          char* decodeStr;          char* decodeStr;
422          char  buf[MAX_BUF_SIZE];          char  buf[MAX_BUF_SIZE];
423          char* inbuf;          char* inbuf;
# Line 238  WikiData::WikiData(wxString* dataDir, co Line 434  WikiData::WikiData(wxString* dataDir, co
434    
435          text = NULL;          text = NULL;
436          memset(outbuf, 0, MAX_BUF_SIZE);          memset(outbuf, 0, MAX_BUF_SIZE);
437          fileName = new wxString((const char*)file, conv);          fileName = new wxString(inFileName->mb_str(), conv);
438          dataDirName = dataDir;          dataDirName = new wxString(dataDir->mb_str(), conv);
439          decodeStr = decode(file);          decodeStr = decode(fileName->mb_str());
440          inbufSize = strlen(decodeStr);          inbufSize = strlen(decodeStr);
441          outbufSize = sizeof(outbuf);          outbufSize = sizeof(outbuf);
442          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);
443          subject  = new wxString((const char*)outbuf, conv);          subject  = new wxString((const char*)outbuf, conv);
444            oldSubject  = new wxString((const char*)outbuf, conv);
445          iconv_close(codeSet);          iconv_close(codeSet);
446    
447          date     = NULL;          date     = NULL;
448    
449      rewind(fp);      fullPathName = *dataDir + wxT("/") + *fileName;
450            fp = fopen((const char*)fullPathName.mb_str(), "r");
451            if(fp == NULL) {
452                    MN_FATAL_ERROR(wxT("fopen faild"));
453            }
454          while(fgets(buf, MAX_BUF_SIZE, fp)) {          while(fgets(buf, MAX_BUF_SIZE, fp)) {
455                  if(strstr( (const char*)buf, (const char*)DATE_TAG)) {                  if(strstr( (const char*)buf, (const char*)DATE_TAG)) {
456                          strtok(buf, "\n");                          strtok(buf, "\n");
# Line 258  WikiData::WikiData(wxString* dataDir, co Line 459  WikiData::WikiData(wxString* dataDir, co
459                          break;                          break;
460                  }                  }
461          }          }
462            fclose(fp);
463  }  }
464    
465  WikiData::WikiData(wxString* dataDir) {  WikiData::WikiData(wxString* dataDir) {
# Line 265  WikiData::WikiData(wxString* dataDir) { Line 467  WikiData::WikiData(wxString* dataDir) {
467          char       buf[MAX_BUF_SIZE];          char       buf[MAX_BUF_SIZE];
468          wxCSConv    conv(wxT(CODE_SET_SYSTEM));          wxCSConv    conv(wxT(CODE_SET_SYSTEM));
469    
470          dataDirName = dataDir;          dataDirName = new wxString(dataDir->mb_str(), conv);
471    
472      time(&now);      time(&now);
473          memset(buf, 0, sizeof(buf));          memset(buf, 0, sizeof(buf));
474          strftime(buf, sizeof(buf), "%Y/%m/%d-%H%M%S",localtime(&now));          strftime(buf, sizeof(buf), "%Y/%m/%d-%H%M%S",localtime(&now));
475          subject  = new wxString(buf, conv);          subject  = new wxString(buf, conv);
476            oldSubject  = new wxString(buf, conv);
477    
478          fileName = new wxString(encode(buf), conv);          fileName = new wxString(encode(buf), conv);
479          fileName->Append(wxT(EXT_TAG));          fileName->Append(wxT(EXT_TAG));
# Line 288  WikiData::WikiData(wxString* dataDir) { Line 491  WikiData::WikiData(wxString* dataDir) {
491  WikiData::~WikiData()  WikiData::~WikiData()
492  {  {
493          delete subject;          delete subject;
494            delete oldSubject;
495          delete fileName;          delete fileName;
496          delete date;          delete date;
497          delete text;          delete text;
498            delete dataDirName;
499  }  }
500    
501  const wxString* WikiData::getFileName()  const wxString* WikiData::getFileName()
# Line 303  const wxString* WikiData::getSubject() Line 508  const wxString* WikiData::getSubject()
508          return subject;          return subject;
509  }  }
510    
511    const wxString* WikiData::getOldSubject()
512    {
513            return oldSubject;
514    }
515    
516  void WikiData::modSubject(wxString* newSubject)  void WikiData::modSubject(wxString* newSubject)
517  {  {
518          wxCSConv    conv(wxT(CODE_SET_SYSTEM));          wxCSConv    conv(wxT(CODE_SET_SYSTEM));
519          wxString*   oldFileName = fileName;          wxString*   oldFileName = fileName;
         wxString*   oldSubject  = subject;  
520          char        oldFullPath[MAX_BUF_SIZE];          char        oldFullPath[MAX_BUF_SIZE];
521          char        newFullPath[MAX_BUF_SIZE];          char        newFullPath[MAX_BUF_SIZE];
522          iconv_t     codeSet;          iconv_t     codeSet;
# Line 319  void WikiData::modSubject(wxString* newS Line 528  void WikiData::modSubject(wxString* newS
528          int         outbufSize = sizeof(outbuf);          int         outbufSize = sizeof(outbuf);
529          FILE*       fp;          FILE*       fp;
530    
531            oldSubject = subject;
532    
533          memset(outbuf, 0, outbufSize);          memset(outbuf, 0, outbufSize);
534          memset(inbuf,  0, sizeof(inbuf));          memset(inbuf,  0, sizeof(inbuf));
535          strcpy(inbuf, (const char*)newSubject->mb_str());          strcpy(inbuf, (const char*)newSubject->mb_str());
# Line 347  void WikiData::modSubject(wxString* newS Line 558  void WikiData::modSubject(wxString* newS
558                  fclose(fp);                  fclose(fp);
559          }          }
560    
         delete oldSubject;  
561          delete oldFileName;          delete oldFileName;
562  }  }
563    
# Line 413  void WikiData::modText(wxString* intext) Line 623  void WikiData::modText(wxString* intext)
623          wxCSConv conv(wxT(CODE_SET_SYSTEM));          wxCSConv conv(wxT(CODE_SET_SYSTEM));
624          delete text;          delete text;
625          //text = new wxString(intext->c_str());          //text = new wxString(intext->c_str());
626          text = new wxString(*intext);          //text = new wxString(*intext);
627          //text = new wxString(intext->mb_str(), conv);          text = new wxString(intext->mb_str(), conv);
628  }  }
629    
630  void WikiData::removeDataFile()  void WikiData::removeDataFile()

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

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