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.12 - (hide annotations) (download) (as text)
Sat Aug 13 02:00:29 2005 UTC (18 years, 7 months ago) by maloninc
Branch: MAIN
CVS Tags: dev-1_1-0008
Changes since 1.11: +8 -0 lines
File MIME type: text/x-c++src
for Mac backslash(not yen mark) and emacs like keybind for mac(incomplete)

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

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