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.8 - (hide annotations) (download) (as text)
Wed Aug 10 06:06:16 2005 UTC (18 years, 7 months ago) by maloninc
Branch: MAIN
CVS Tags: dev-1_1-0002
Changes since 1.7: +17 -2 lines
File MIME type: text/x-c++src
search ignore case

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

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