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.18 - (show annotations) (download) (as text)
Tue Sep 13 04:22:15 2005 UTC (18 years, 6 months ago) by maloninc
Branch: MAIN
CVS Tags: dev_1_3-0003
Changes since 1.17: +2 -1 lines
File MIME type: text/x-c++src
implement searching all memo

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

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