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.10 - (show annotations) (download) (as text)
Thu Aug 11 01:51:21 2005 UTC (18 years, 7 months ago) by maloninc
Branch: MAIN
CVS Tags: dev-1_1-0005, dev-1_1-0006, dev-1_1-0007
Changes since 1.9: +2 -21 lines
File MIME type: text/x-c++src
fixed ~, \ BUGS

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 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 dir = new wxDir(*wikiDataDir);
77 if ( !dir->IsOpened() )
78 {
79 MN_FATAL_ERROR(wxT("wxDir has faild\n"));
80 return NULL;
81 }
82 bool cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES);
83 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
90 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 snprintf(decodeFileNameBuf, MAX_BUF_SIZE, "%s", decodeFileName);
96 toLower(buf);
97 toLower(outbuf);
98 toLower(decodeFileNameBuf);
99 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 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 delete fileName;
123 for(i = 0; tokenList[i] != NULL; i++) free(tokenList[i]);
124
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 const wxString* mnModel::getWikiDataDir()
156 {
157 return wikiDataDir;
158 }
159
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 iconv(codeSet, (ICONV_CONST char**)&decodeStr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
197 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 iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
281 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 else if(strcmp(oldFullPath, newFullPath)){
293 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 wxCSConv conv(wxT(CODE_SET_SYSTEM));
313 wxCSConv eucConv(wxT(CODE_SET_EUC_JP));
314 wxString* tmpStr;
315
316 if(text) {
317 return text;
318 }
319
320 text = new wxString();
321 sprintf(fullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str());
322 fp = fopen(fullPath, "r");
323 if(fp == NULL) {
324 MN_FATAL_ERROR(wxT("File open error."));
325 }
326
327 while(fgets(buf, MAX_BUF_SIZE, fp)) {
328 tmpStr = new wxString(buf, eucConv);
329 *text += *tmpStr;
330 delete tmpStr;
331 }
332 fclose(fp);
333 return text;
334 }
335
336 void WikiData::modText(wxString* intext)
337 {
338 wxCSConv conv(wxT(CODE_SET_SYSTEM));
339 delete text;
340 //text = new wxString(intext->c_str());
341 text = new wxString(*intext);
342 //text = new wxString(intext->mb_str(), conv);
343 }
344
345 void WikiData::removeDataFile()
346 {
347 char fullPath[MAX_BUF_SIZE];
348
349 sprintf(fullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str());
350 if(remove(fullPath)) {
351 MN_FATAL_ERROR(wxT("remove file error"));
352 }
353 }
354
355 void WikiData::save()
356 {
357 char fullPath[MAX_BUF_SIZE];
358 FILE* fp;
359 iconv_t codeSet;
360 char inbuf[MAX_WIKI_TEXT_SIZE];
361 char outbuf[MAX_WIKI_TEXT_SIZE];
362 const char* inbufPtr;
363 char* outbufPtr;
364 int inbufSize;
365 int outbufSize;
366
367 codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);
368 if(codeSet == (iconv_t)-1) {
369 MN_FATAL_ERROR(wxT("failed iconv_open"));
370 }
371
372 sprintf(fullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str());
373 fp = fopen(fullPath, "wb");
374 if(fp == NULL) {
375 MN_FATAL_ERROR(wxT("File open error."));
376 }
377
378 memset(inbuf, 0, sizeof(inbuf));
379 strcpy(inbuf,(const char*)text->mb_str());
380 inbufPtr = inbuf;
381 inbufSize = strlen(inbufPtr);
382 outbufPtr = outbuf;
383 outbufSize = sizeof(outbuf);
384 memset(outbuf, 0, outbufSize);
385 iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
386 fwrite(outbuf, sizeof(outbuf)-outbufSize, 1, fp);
387 fclose(fp);
388 iconv_close(codeSet);
389 }
390
391 /******* Tools ************************/
392
393 static void toLower(char* string)
394 {
395 int i;
396
397 for(i = 0; string[i] != 0; i++) {
398 string[i] = tolower(string[i]);
399 }
400 }
401
402
403 static char* decode(const char* string)
404 {
405 static char buf[MAX_BUF_SIZE];
406 char c[5];
407 int i,j;
408 char* endPtr = NULL;
409
410 j = 0;
411 memset(buf, 0, MAX_BUF_SIZE);
412 for(i = 0; string[i] != 0; i+=2) {
413 c[0] = '0'; c[1] = 'x';
414 c[2] = string[i]; c[3] = string[i+1]; c[4] = 0;
415 buf[j] = strtol(c, &endPtr, 0);
416 j++;
417 if(j >= MAX_BUF_SIZE) {
418 buf[MAX_BUF_SIZE] = 0;
419 break;
420 }
421 }
422
423 return buf;
424 }
425
426 static char* encode(const char* string)
427 {
428 static char buf[MAX_BUF_SIZE];
429 int i,j;
430
431 j = 0;
432 memset(buf, 0, MAX_BUF_SIZE);
433 for(i = 0; string[i] != 0; i++) {
434 snprintf(&buf[j], 3, "%02X", (unsigned char)(string[i]));
435 j+=2;
436 if(j >= MAX_BUF_SIZE) {
437 buf[MAX_BUF_SIZE] = 0;
438 break;
439 }
440 }
441 return buf;
442 }
443
444 /*
445 * wiki1 > wiki2 -> return < 0
446 * wiki1 = wiki2 -> return = 0
447 * wiki1 < wiki2 -> return > 0
448 */
449 static int compWikiData(const void* wiki1, const void* wiki2)
450 {
451 WikiData* data1 = *(WikiData**)wiki1;
452 WikiData* data2 = *(WikiData**)wiki2;
453 const wxString* str1 = NULL;
454 const wxString* str2 = NULL;
455
456 str1 = data1->getDate();
457 str2 = data2->getDate();
458
459 if(str1 != NULL && str2 != NULL) {
460 return (strcmp(str2->mb_str(), str1->mb_str()));
461 }
462 else{
463 return 0;
464 }
465 }
466

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