Develop and Download Open Source Software

Browse CVS Repository

Annotation of /malonnote/mnModel.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.17 - (hide annotations) (download) (as text)
Tue Sep 13 01:37:43 2005 UTC (18 years, 6 months ago) by maloninc
Branch: MAIN
CVS Tags: dev_1_3-0002
Changes since 1.16: +2 -1 lines
File MIME type: text/x-c++src
change search string list order(reverse)

1 maloninc 1.1 #include "mnDef.h"
2     #include "mnModel.h"
3     #include <wx/dir.h>
4     #include <wx/regex.h>
5     #include <iconv.h>
6    
7 maloninc 1.8 static void toLower(char* string);
8 maloninc 1.1 static char* decode(const char* string);
9     static char* encode(const char* string);
10 maloninc 1.8 static int compWikiData(const void* wiki1, const void* wiki2);
11 maloninc 1.1
12    
13     /******* WikiList ************************/
14     #include <wx/listimpl.cpp>
15     WX_DEFINE_LIST(WikiList);
16    
17    
18     /******* mnModel ************************/
19    
20     mnModel::mnModel(const char* dataDir)
21     {
22     wxCSConv conv(wxT(CODE_SET_SYSTEM));
23    
24     wikiDataDir = new wxString(dataDir, conv);
25     searchStrList = new wxArrayString();
26     }
27    
28     mnModel::~mnModel()
29     {
30     delete wikiDataDir;
31     }
32    
33    
34     WikiList* mnModel::search(const char* searchStr)
35     {
36 maloninc 1.9 int i;
37 maloninc 1.1 wxDir* dir;
38     FILE* fp;
39     wxString fullPathName;
40     char buf[MAX_BUF_SIZE];
41     WikiData* wikiData;
42     WikiList* list = new WikiList();
43     wxString* fileName = new wxString();
44 maloninc 1.7 iconv_t codeSet;
45     char outbuf[MAX_BUF_SIZE];
46     const char* inbufPtr = searchStr;
47     char* outbufPtr = outbuf;
48     int inbufSize = strlen(searchStr);
49     int outbufSize = sizeof(outbuf);
50 maloninc 1.1 const char* decodeFileName;
51 maloninc 1.8 char decodeFileNameBuf[MAX_BUF_SIZE];
52 maloninc 1.9 char* token;
53     char* tokenList[32];
54     bool found;
55 maloninc 1.1
56 maloninc 1.9 memset(tokenList, 0, sizeof(char*)*32);
57 maloninc 1.7 memset(outbuf, 0, outbufSize);
58     codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);
59     if(codeSet == (iconv_t)-1) {
60     MN_FATAL_ERROR(wxT("failed iconv_open"));
61     }
62     iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
63     iconv_close(codeSet);
64 maloninc 1.1
65 maloninc 1.9 /* searchStr to Tokens */
66     token = strtok(outbuf, " ");
67 maloninc 1.16 if(token == NULL) return list;
68 maloninc 1.9 tokenList[0] = (char*)malloc(strlen(token)+1);
69     snprintf(tokenList[0], strlen(token)+1, "%s", token);
70     i = 1;
71     while((token = strtok(NULL, " ")) != NULL) {
72     tokenList[i] = (char*)malloc(strlen(token)+1);
73     snprintf(tokenList[i], strlen(token)+1, "%s", token);
74     i++;
75     }
76    
77 maloninc 1.1 dir = new wxDir(*wikiDataDir);
78     if ( !dir->IsOpened() )
79     {
80     MN_FATAL_ERROR(wxT("wxDir has faild\n"));
81     return NULL;
82     }
83 maloninc 1.4 bool cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES);
84 maloninc 1.1 while(cont){
85     fullPathName = *wikiDataDir + wxT("/") + *fileName;
86     fp = fopen((const char*)fullPathName.mb_str(), "r");
87     if(fp == NULL) {
88     MN_FATAL_ERROR(wxT("fopen faild"));
89     }
90 maloninc 1.9
91 maloninc 1.1 while(1){
92     memset(buf, 0, MAX_BUF_SIZE);
93     fread(buf, MAX_BUF_SIZE-1, 1, fp);
94     if(buf[0] == 0) break;
95     decodeFileName = decode(fileName->mb_str());
96 maloninc 1.8 snprintf(decodeFileNameBuf, MAX_BUF_SIZE, "%s", decodeFileName);
97     toLower(buf);
98     toLower(decodeFileNameBuf);
99 maloninc 1.9 found = TRUE;
100     for(i = 0; tokenList[i] != NULL; i++){
101 maloninc 1.14 toLower(tokenList[i]);
102 maloninc 1.9 if(strstr((const char*)buf, (const char*)tokenList[i]) ||
103 maloninc 1.15 strstr((const char*)decodeFileNameBuf, (const char*)tokenList[i])) {
104 maloninc 1.9 found = TRUE;
105     }
106     else {
107     found = FALSE;
108     break;
109     }
110     }
111    
112     if(found){
113 maloninc 1.1 wikiData = new WikiData(wikiDataDir, (const char*)fileName->mb_str(), fp);
114     list->Append(wikiData);
115     break;
116     }
117     buf[0] = 0;
118     }
119     fclose(fp);
120     cont = dir->GetNext(fileName);
121     }
122     delete dir;
123 maloninc 1.9 delete fileName;
124     for(i = 0; tokenList[i] != NULL; i++) free(tokenList[i]);
125 maloninc 1.1
126     list->Sort(compWikiData);
127     return list;
128     }
129    
130     void mnModel::addSearchStr(wxString* searchStr)
131     {
132     wxString *string;
133    
134     if(searchStrList->Index(searchStr->c_str()) == wxNOT_FOUND){
135     string = new wxString(searchStr->c_str());
136 maloninc 1.17 //searchStrList->Add(*string, 1);
137     searchStrList->Insert(*string, 0);
138 maloninc 1.1 }
139     }
140    
141     void mnModel::removeSearchStr(wxString searchStr)
142     {
143     if(searchStrList->Index(searchStr.c_str()) != wxNOT_FOUND) {
144     searchStrList->Remove(searchStr.c_str());
145     }
146     }
147    
148     void mnModel::modSearchStr(wxString* oldStr, wxString* newStr)
149     {
150     int index;
151    
152     if((index = searchStrList->Index(oldStr->c_str())) != wxNOT_FOUND){
153     wxString& itemStr = searchStrList->Item(index);
154     itemStr.sprintf(wxT("%s"), newStr->c_str());
155     }
156     }
157 maloninc 1.4 const wxString* mnModel::getWikiDataDir()
158     {
159     return wikiDataDir;
160     }
161 maloninc 1.1
162     const wxArrayString* mnModel::getSearchStrList()
163     {
164     return searchStrList;
165     }
166    
167     WikiData* mnModel::newWikiData()
168     {
169     WikiData* data = new WikiData(wikiDataDir);
170    
171     return data;
172     }
173    
174     /******* WikiData ************************/
175     WikiData::WikiData(wxString* dataDir, const char* file, FILE* fp)
176     {
177     char* decodeStr;
178     char buf[MAX_BUF_SIZE];
179     char* inbuf;
180     int inbufSize;
181     char outbuf[MAX_BUF_SIZE];
182     char* outbufPtr = outbuf;
183     int outbufSize;
184     wxCSConv conv(wxT(CODE_SET_SYSTEM));
185    
186     iconv_t codeSet = iconv_open(CODE_SET_SYSTEM, CODE_SET_EUC_JP);
187     if(codeSet == (iconv_t)-1) {
188     MN_FATAL_ERROR(wxT("failed iconv_open"));
189     }
190    
191     text = NULL;
192     memset(outbuf, 0, MAX_BUF_SIZE);
193     fileName = new wxString((const char*)file, conv);
194     dataDirName = dataDir;
195     decodeStr = decode(file);
196     inbufSize = strlen(decodeStr);
197     outbufSize = sizeof(outbuf);
198 maloninc 1.5 iconv(codeSet, (ICONV_CONST char**)&decodeStr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
199 maloninc 1.1 subject = new wxString((const char*)outbuf, conv);
200     iconv_close(codeSet);
201    
202     date = NULL;
203    
204     rewind(fp);
205     while(fgets(buf, MAX_BUF_SIZE, fp)) {
206     if(strstr( (const char*)buf, (const char*)DATE_TAG)) {
207     strtok(buf, "\n");
208     strtok(buf, "\r");
209     date = new wxString((const char*)buf, conv);
210     break;
211     }
212     }
213     }
214    
215     WikiData::WikiData(wxString* dataDir) {
216     time_t now;
217     char buf[MAX_BUF_SIZE];
218     wxCSConv conv(wxT(CODE_SET_SYSTEM));
219    
220     dataDirName = dataDir;
221    
222     time(&now);
223     memset(buf, 0, sizeof(buf));
224     strftime(buf, sizeof(buf), "%Y/%m/%d-%H%M%S",localtime(&now));
225     subject = new wxString(buf, conv);
226    
227     fileName = new wxString(encode(buf), conv);
228     fileName->Append(wxT(EXT_TAG));
229    
230     memset(buf, 0, sizeof(buf));
231     strftime(buf, sizeof(buf), DATE_TAG "%Y/%m/%d %H:%M:%S",localtime(&now));
232     date = new wxString(buf, conv);
233    
234     memset(buf, 0, sizeof(buf));
235     strftime(buf, sizeof(buf), NEW_DATA,localtime(&now));
236     text = new wxString(buf, conv);
237    
238     }
239    
240     WikiData::~WikiData()
241     {
242     delete subject;
243     delete fileName;
244     delete date;
245     delete text;
246     }
247    
248     const wxString* WikiData::getFileName()
249     {
250     return fileName;
251     }
252    
253     const wxString* WikiData::getSubject()
254     {
255     return subject;
256     }
257    
258     void WikiData::modSubject(wxString* newSubject)
259     {
260     wxCSConv conv(wxT(CODE_SET_SYSTEM));
261     wxString* oldFileName = fileName;
262     wxString* oldSubject = subject;
263     char oldFullPath[MAX_BUF_SIZE];
264     char newFullPath[MAX_BUF_SIZE];
265     iconv_t codeSet;
266     char outbuf[MAX_BUF_SIZE];
267     char inbuf[MAX_BUF_SIZE];
268     const char* inbufPtr = inbuf;
269     char* outbufPtr = outbuf;
270     int inbufSize;
271     int outbufSize = sizeof(outbuf);
272     FILE* fp;
273    
274     memset(outbuf, 0, outbufSize);
275     memset(inbuf, 0, sizeof(inbuf));
276     strcpy(inbuf, (const char*)newSubject->mb_str());
277     inbufSize = strlen(inbuf);
278     codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);
279     if(codeSet == (iconv_t)-1) {
280     MN_FATAL_ERROR(wxT("failed iconv_open"));
281     }
282 maloninc 1.5 iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
283 maloninc 1.1 iconv_close(codeSet);
284     subject = new wxString(newSubject->c_str());
285     fileName = new wxString(encode(outbuf), conv);
286     fileName->Append(wxT(EXT_TAG));
287    
288     sprintf(oldFullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)oldFileName->mb_str());
289     sprintf(newFullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str());
290    
291     if((fp = fopen(newFullPath, "r")) == NULL) {
292 maloninc 1.13 if(rename(oldFullPath, newFullPath) < 0) wxLogMessage(wxT("rename error: errno=[%d]"), errno);
293 maloninc 1.1 }
294 maloninc 1.2 else if(strcmp(oldFullPath, newFullPath)){
295 maloninc 1.1 wxLogMessage(wxT("File has already exist. [%s]"), fileName->c_str());
296     fclose(fp);
297     }
298 maloninc 1.13 else {
299     fclose(fp);
300     }
301 maloninc 1.1
302     delete oldSubject;
303     delete oldFileName;
304     }
305    
306     const wxString* WikiData::getDate()
307     {
308     return date;
309     }
310    
311    
312     const wxString* WikiData::getText()
313     {
314     FILE* fp;
315     char buf[MAX_BUF_SIZE];
316     char fullPath[MAX_BUF_SIZE];
317 maloninc 1.11 iconv_t codeSet;
318     char outbuf[MAX_BUF_SIZE];
319     char* inbufPtr;
320     char* outbufPtr;
321     int inbufSize;
322     int outbufSize;
323 maloninc 1.1 wxCSConv conv(wxT(CODE_SET_SYSTEM));
324     wxString* tmpStr;
325    
326 maloninc 1.11 codeSet = iconv_open(CODE_SET_SYSTEM, CODE_SET_EUC_JP);
327     if(codeSet == (iconv_t)-1) {
328     MN_FATAL_ERROR(wxT("failed iconv_open"));
329     }
330    
331 maloninc 1.1 if(text) {
332 maloninc 1.11 iconv_close(codeSet);
333 maloninc 1.1 return text;
334     }
335    
336     text = new wxString();
337     sprintf(fullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str());
338     fp = fopen(fullPath, "r");
339     if(fp == NULL) {
340     MN_FATAL_ERROR(wxT("File open error."));
341     }
342    
343     while(fgets(buf, MAX_BUF_SIZE, fp)) {
344 maloninc 1.12 #ifdef __WXMAC__
345     for(int i = 0; buf[i] != 0; i++) if(buf[i] == (char)MAC_BACKSLASH) buf[i] = '\\';
346     #endif
347 maloninc 1.11 inbufPtr = buf;
348     inbufSize = sizeof(buf);
349     outbufPtr = outbuf;
350     outbufSize = sizeof(outbuf);
351     memset(outbuf, 0, outbufSize);
352     iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
353     tmpStr = new wxString((char*)outbuf, conv);
354 maloninc 1.1 *text += *tmpStr;
355     delete tmpStr;
356     }
357 maloninc 1.11 iconv_close(codeSet);
358 maloninc 1.1 fclose(fp);
359 maloninc 1.11
360 maloninc 1.1 return text;
361     }
362    
363     void WikiData::modText(wxString* intext)
364     {
365 maloninc 1.3 wxCSConv conv(wxT(CODE_SET_SYSTEM));
366 maloninc 1.1 delete text;
367 maloninc 1.3 //text = new wxString(intext->c_str());
368     text = new wxString(*intext);
369     //text = new wxString(intext->mb_str(), conv);
370 maloninc 1.1 }
371    
372     void WikiData::removeDataFile()
373     {
374     char fullPath[MAX_BUF_SIZE];
375    
376     sprintf(fullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str());
377     if(remove(fullPath)) {
378     MN_FATAL_ERROR(wxT("remove file error"));
379     }
380     }
381    
382     void WikiData::save()
383     {
384     char fullPath[MAX_BUF_SIZE];
385     FILE* fp;
386     iconv_t codeSet;
387     char inbuf[MAX_WIKI_TEXT_SIZE];
388     char outbuf[MAX_WIKI_TEXT_SIZE];
389     const char* inbufPtr;
390     char* outbufPtr;
391     int inbufSize;
392     int outbufSize;
393    
394     codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);
395     if(codeSet == (iconv_t)-1) {
396     MN_FATAL_ERROR(wxT("failed iconv_open"));
397     }
398    
399     sprintf(fullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str());
400     fp = fopen(fullPath, "wb");
401     if(fp == NULL) {
402     MN_FATAL_ERROR(wxT("File open error."));
403     }
404    
405     memset(inbuf, 0, sizeof(inbuf));
406     strcpy(inbuf,(const char*)text->mb_str());
407 maloninc 1.12
408     #ifdef __WXMAC__
409     for(int i = 0; inbuf[i] != 0; i++) if(inbuf[i] == (char)MAC_BACKSLASH) inbuf[i] = '\\';
410     #endif
411    
412 maloninc 1.1 inbufPtr = inbuf;
413     inbufSize = strlen(inbufPtr);
414     outbufPtr = outbuf;
415     outbufSize = sizeof(outbuf);
416     memset(outbuf, 0, outbufSize);
417 maloninc 1.5 iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
418 maloninc 1.1 fwrite(outbuf, sizeof(outbuf)-outbufSize, 1, fp);
419     fclose(fp);
420     iconv_close(codeSet);
421     }
422    
423     /******* Tools ************************/
424    
425 maloninc 1.8 static void toLower(char* string)
426     {
427     int i;
428    
429     for(i = 0; string[i] != 0; i++) {
430     string[i] = tolower(string[i]);
431     }
432     }
433    
434    
435 maloninc 1.1 static char* decode(const char* string)
436     {
437     static char buf[MAX_BUF_SIZE];
438     char c[5];
439     int i,j;
440     char* endPtr = NULL;
441    
442     j = 0;
443     memset(buf, 0, MAX_BUF_SIZE);
444     for(i = 0; string[i] != 0; i+=2) {
445     c[0] = '0'; c[1] = 'x';
446     c[2] = string[i]; c[3] = string[i+1]; c[4] = 0;
447     buf[j] = strtol(c, &endPtr, 0);
448     j++;
449     if(j >= MAX_BUF_SIZE) {
450     buf[MAX_BUF_SIZE] = 0;
451     break;
452     }
453     }
454    
455     return buf;
456     }
457    
458     static char* encode(const char* string)
459     {
460     static char buf[MAX_BUF_SIZE];
461     int i,j;
462    
463     j = 0;
464     memset(buf, 0, MAX_BUF_SIZE);
465     for(i = 0; string[i] != 0; i++) {
466     snprintf(&buf[j], 3, "%02X", (unsigned char)(string[i]));
467     j+=2;
468     if(j >= MAX_BUF_SIZE) {
469     buf[MAX_BUF_SIZE] = 0;
470     break;
471     }
472     }
473     return buf;
474     }
475    
476     /*
477     * wiki1 > wiki2 -> return < 0
478     * wiki1 = wiki2 -> return = 0
479     * wiki1 < wiki2 -> return > 0
480     */
481     static int compWikiData(const void* wiki1, const void* wiki2)
482     {
483     WikiData* data1 = *(WikiData**)wiki1;
484     WikiData* data2 = *(WikiData**)wiki2;
485     const wxString* str1 = NULL;
486     const wxString* str2 = NULL;
487    
488     str1 = data1->getDate();
489     str2 = data2->getDate();
490    
491     if(str1 != NULL && str2 != NULL) {
492     return (strcmp(str2->mb_str(), str1->mb_str()));
493     }
494     else{
495     return 0;
496     }
497     }
498    

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