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.3 - (hide annotations) (download) (as text)
Wed Aug 3 23:19:01 2005 UTC (18 years, 7 months ago) by maloninc
Branch: MAIN
CVS Tags: dev-1_0-0011
Changes since 1.2: +4 -1 lines
File MIME type: text/x-c++src
fixed meta tag charset to system dependent value

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

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