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.6 - (hide annotations) (download) (as text)
Wed Aug 10 05:35:03 2005 UTC (18 years, 7 months ago) by maloninc
Branch: MAIN
CVS Tags: dev-1_1-0001
Changes since 1.5: +8 -16 lines
File MIME type: text/x-c++src
implement regluar expression searching.

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

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