Develop and Download Open Source Software

Browse CVS Repository

Contents of /malonnote/mnModel.cpp

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


Revision 1.16 - (show annotations) (download) (as text)
Mon Sep 12 21:44:33 2005 UTC (18 years, 6 months ago) by maloninc
Branch: MAIN
CVS Tags: dev_1_3-0001
Changes since 1.15: +1 -0 lines
File MIME type: text/x-c++src
fixed bug which crashed when search word is NULL.

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

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