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.1.1.1 by maloninc, Thu Jul 28 03:25:06 2005 UTC revision 1.23 by maloninc, Thu Sep 15 09:20:48 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 30  mnModel::~mnModel() Line 30  mnModel::~mnModel()
30          delete wikiDataDir;          delete wikiDataDir;
31  }  }
32    
33    /*
34     * 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    {
41            iconv_t     codeSet;
42            char        outbuf[MAX_BUF_SIZE];
43            const char* inbufPtr   = searchStr;
44            char*       outbufPtr  = outbuf;
45            int         inbufSize  = strlen(searchStr);
46            int         outbufSize = sizeof(outbuf);
47            char*       token;
48            int         i;
49    
50            memset(outbuf, 0, outbufSize);
51            codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);
52            if(codeSet == (iconv_t)-1) {
53                    MN_FATAL_ERROR(wxT("failed iconv_open"));
54            }
55            iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
56            iconv_close(codeSet);
57    
58            /* searchStr to Tokens */
59            token = strtok(outbuf, " ");
60            if(token == NULL) return false;
61            tokenList[0] = (char*)malloc(strlen(token)+1);
62            snprintf(tokenList[0], strlen(token)+1, "%s", token);
63            i = 1;
64            while((token = strtok(NULL, " ")) != NULL) {
65                    tokenList[i] = (char*)malloc(strlen(token)+1);
66                    snprintf(tokenList[i], strlen(token)+1, "%s", token);
67                    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)  WikiList* mnModel::search(const char* searchStr)
111  {  {
112            int         i;
113          wxDir*      dir;          wxDir*      dir;
         FILE*       fp;  
     wxString    fullPathName;  
     char        buf[MAX_BUF_SIZE];  
114          WikiData*   wikiData;          WikiData*   wikiData;
115      WikiList*   list = new WikiList();      WikiList*   list = new WikiList();
116      wxString*   fileName = new wxString();      wxString*   fileName = new wxString();
117          wxRegEx*        regStr;          char*       tokenList[32];
118          iconv_t     codeSet;  
119            memset(tokenList, 0, sizeof(char*)*32);
120            if( makeSearchToken(searchStr, tokenList) == false) return list;
121    
122            dir = new wxDir(*wikiDataDir);
123        if ( !dir->IsOpened() )
124        {
125                    MN_FATAL_ERROR(wxT("wxDir has faild\n"));
126            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];          char        outbuf[MAX_BUF_SIZE];
         const char*       inbufPtr  = searchStr;  
161          char*       outbufPtr = outbuf;          char*       outbufPtr = outbuf;
162          int         inbufSize = strlen(searchStr);          int         outbufSize;
163          int         outbufSize = sizeof(outbuf);          wxString*   typeToken;
164          const char* decodeFileName;          char*       ptr;
165            int         i;
166    
167          memset(outbuf, 0, outbufSize);          iconv_t codeSet = iconv_open(CODE_SET_SYSTEM, CODE_SET_EUC_JP);
         codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);  
168          if(codeSet == (iconv_t)-1) {          if(codeSet == (iconv_t)-1) {
169                  MN_FATAL_ERROR(wxT("failed iconv_open"));                  MN_FATAL_ERROR(wxT("failed iconv_open"));
170          }          }
         iconv(codeSet, (const char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);  
         iconv_close(codeSet);  
171    
172          dir = new wxDir(*wikiDataDir);          dir = new wxDir(*wikiDataDir);
173      if ( !dir->IsOpened() )      if ( !dir->IsOpened() )
174      {      {
175                  MN_FATAL_ERROR(wxT("wxDir has faild\n"));                  MN_FATAL_ERROR(wxT("wxDir has faild\n"));
176          return NULL;          return ;
177      }      }
178      bool cont = dir->GetFirst(fileName, wxEmptyString, 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;
181                  fp = fopen((const char*)fullPathName.mb_str(), "r");                  fp = fopen((const char*)fullPathName.mb_str(), "r");
182                  if(fp == NULL) {                  if(fp == NULL) {
183                          MN_FATAL_ERROR(wxT("fopen faild"));                          MN_FATAL_ERROR(wxT("fopen faild"));
184                  }                  }
185          while(1){                  while(1) {
186                  memset(buf, 0, MAX_BUF_SIZE);                          memset(buf, 0, MAX_BUF_SIZE);
187              fread(buf, MAX_BUF_SIZE-1, 1, fp);                          fgets(buf, MAX_BUF_SIZE, fp);
188              if(buf[0] == 0) break;                          if(buf[0] == 0) break;
189                          decodeFileName = decode(fileName->mb_str());                          if(strstr(buf, TYPE_TAG)){
190                  if(strstr((const char*)buf, (const char*)outbuf) ||                                  ptr = &buf[typeTagLen];
191                                  strstr((const char*)decodeFileName, (const char*)outbuf)) {                                  while((token = strtok(ptr, " /\r\n:"))!= NULL) {
192                                  wikiData = new WikiData(wikiDataDir, (const char*)fileName->mb_str(), fp);                                          memset(outbuf, 0, MAX_BUF_SIZE);
193                                  list->Append(wikiData);                                          snprintf(outbuf, MAX_BUF_SIZE, "%s", TYPESEARCH_TAG);
194                                  break;                                          inbufPtr = token;
195                                            inbufSize = strlen(token);
196                                            outbufPtr = &outbuf[strlen(TYPESEARCH_TAG)];
197                                            outbufSize = sizeof(outbuf) - strlen(TYPESEARCH_TAG);
198                                            iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
199    
200                                            typeToken = new wxString(outbuf, conv);
201                                            addSearchStr(typeToken);
202                                            delete typeToken;
203                                            ptr = NULL;
204                                    }
205                          }                          }
             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;
212            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 99  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 118  void mnModel::modSearchStr(wxString* old Line 302  void mnModel::modSearchStr(wxString* old
302                  itemStr.sprintf(wxT("%s"), newStr->c_str());                  itemStr.sprintf(wxT("%s"), newStr->c_str());
303          }          }
304  }  }
305    const wxString* mnModel::getWikiDataDir()
306    {
307            return wikiDataDir;
308    }
309    
310  const wxArrayString* mnModel::getSearchStrList()  const wxArrayString* mnModel::getSearchStrList()
311  {  {
# Line 130  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 150  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, (const char**)&decodeStr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);          iconv(codeSet, (ICONV_CONST char**)&decodeStr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
384          subject  = new wxString((const char*)outbuf, conv);          subject  = new wxString((const char*)outbuf, conv);
385          iconv_close(codeSet);          iconv_close(codeSet);
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 170  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) {
# Line 239  void WikiData::modSubject(wxString* newS Line 469  void WikiData::modSubject(wxString* newS
469          if(codeSet == (iconv_t)-1) {          if(codeSet == (iconv_t)-1) {
470                  MN_FATAL_ERROR(wxT("failed iconv_open"));                  MN_FATAL_ERROR(wxT("failed iconv_open"));
471          }          }
472          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);
473          iconv_close(codeSet);          iconv_close(codeSet);
474          subject  = new wxString(newSubject->c_str());          subject  = new wxString(newSubject->c_str());
475          fileName = new wxString(encode(outbuf), conv);          fileName = new wxString(encode(outbuf), conv);
# Line 249  void WikiData::modSubject(wxString* newS Line 479  void WikiData::modSubject(wxString* newS
479          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());
480    
481          if((fp = fopen(newFullPath, "r")) == NULL) {          if((fp = fopen(newFullPath, "r")) == NULL) {
482                  rename(oldFullPath, newFullPath);                  if(rename(oldFullPath, newFullPath) < 0) wxLogMessage(wxT("rename error: errno=[%d]"), errno);
483          }          }
484          else {          else if(strcmp(oldFullPath, newFullPath)){
485                  wxLogMessage(wxT("File has already exist. [%s]"), fileName->c_str());                  wxLogMessage(wxT("File has already exist. [%s]"), fileName->c_str());
486                  fclose(fp);                  fclose(fp);
487          }          }
488            else {
489                    fclose(fp);
490            }
491    
492          delete oldSubject;          delete oldSubject;
493          delete oldFileName;          delete oldFileName;
# Line 298  const wxString* WikiData::getText() Line 531  const wxString* WikiData::getText()
531          }          }
532    
533          while(fgets(buf, MAX_BUF_SIZE, fp)) {          while(fgets(buf, MAX_BUF_SIZE, fp)) {
534    #ifdef __WXMAC__
535                    for(int i = 0; buf[i] != 0; i++) if(buf[i] == (char)MAC_BACKSLASH) buf[i] = '\\';
536    #endif
537                  inbufPtr = buf;                  inbufPtr = buf;
538                  inbufSize = sizeof(buf);                  inbufSize = sizeof(buf);
539                  outbufPtr = outbuf;                  outbufPtr = outbuf;
540                  outbufSize = sizeof(outbuf);                  outbufSize = sizeof(outbuf);
541                  memset(outbuf, 0, outbufSize);                  memset(outbuf, 0, outbufSize);
542                  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);
543                  tmpStr = new wxString((char*)outbuf, conv);                  tmpStr = new wxString((char*)outbuf, conv);
544                  *text += *tmpStr;                  *text += *tmpStr;
545                  delete tmpStr;                  delete tmpStr;
# Line 316  const wxString* WikiData::getText() Line 552  const wxString* WikiData::getText()
552    
553  void WikiData::modText(wxString* intext)  void WikiData::modText(wxString* intext)
554  {  {
555            wxCSConv conv(wxT(CODE_SET_SYSTEM));
556          delete text;          delete text;
557          text = new wxString(intext->c_str());          //text = new wxString(intext->c_str());
558            text = new wxString(*intext);
559            //text = new wxString(intext->mb_str(), conv);
560  }  }
561    
562  void WikiData::removeDataFile()  void WikiData::removeDataFile()
# Line 355  void WikiData::save() Line 594  void WikiData::save()
594    
595          memset(inbuf, 0, sizeof(inbuf));          memset(inbuf, 0, sizeof(inbuf));
596          strcpy(inbuf,(const char*)text->mb_str());          strcpy(inbuf,(const char*)text->mb_str());
597    
598    #ifdef __WXMAC__
599            for(int i = 0; inbuf[i] != 0; i++) if(inbuf[i] == (char)MAC_BACKSLASH) inbuf[i] = '\\';
600    #endif
601    
602          inbufPtr = inbuf;          inbufPtr = inbuf;
603          inbufSize = strlen(inbufPtr);          inbufSize = strlen(inbufPtr);
604          outbufPtr = outbuf;          outbufPtr = outbuf;
605          outbufSize = sizeof(outbuf);          outbufSize = sizeof(outbuf);
606          memset(outbuf, 0, outbufSize);          memset(outbuf, 0, outbufSize);
607          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);
608          fwrite(outbuf, sizeof(outbuf)-outbufSize, 1, fp);          fwrite(outbuf, sizeof(outbuf)-outbufSize, 1, fp);
609          fclose(fp);          fclose(fp);
610          iconv_close(codeSet);          iconv_close(codeSet);
# Line 368  void WikiData::save() Line 612  void WikiData::save()
612    
613  /******* Tools ************************/  /******* Tools ************************/
614    
615    static void toLower(char* string)
616    {
617            int i;
618    
619            for(i = 0; string[i] != 0; i++) {
620                    string[i] = tolower(string[i]);
621            }
622    }
623    
624    
625  static char* decode(const char* string)  static char* decode(const char* string)
626  {  {
627          static char buf[MAX_BUF_SIZE];          static char buf[MAX_BUF_SIZE];

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

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