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.11 - (show annotations) (download) (as text)
Fri Aug 12 07:59:51 2005 UTC (18 years, 7 months ago) by maloninc
Branch: MAIN
Changes since 1.10: +21 -2 lines
File MIME type: text/x-c++src
SJIS -> CP932

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 iconv_t codeSet;
313 char outbuf[MAX_BUF_SIZE];
314 char* inbufPtr;
315 char* outbufPtr;
316 int inbufSize;
317 int outbufSize;
318 wxCSConv conv(wxT(CODE_SET_SYSTEM));
319 wxString* tmpStr;
320
321 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 if(text) {
327 iconv_close(codeSet);
328 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 inbufPtr = buf;
340 inbufSize = sizeof(buf);
341 outbufPtr = outbuf;
342 outbufSize = sizeof(outbuf);
343 memset(outbuf, 0, outbufSize);
344 iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
345 tmpStr = new wxString((char*)outbuf, conv);
346 *text += *tmpStr;
347 delete tmpStr;
348 }
349 iconv_close(codeSet);
350 fclose(fp);
351
352 return text;
353 }
354
355 void WikiData::modText(wxString* intext)
356 {
357 wxCSConv conv(wxT(CODE_SET_SYSTEM));
358 delete text;
359 //text = new wxString(intext->c_str());
360 text = new wxString(*intext);
361 //text = new wxString(intext->mb_str(), conv);
362 }
363
364 void WikiData::removeDataFile()
365 {
366 char fullPath[MAX_BUF_SIZE];
367
368 sprintf(fullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str());
369 if(remove(fullPath)) {
370 MN_FATAL_ERROR(wxT("remove file error"));
371 }
372 }
373
374 void WikiData::save()
375 {
376 char fullPath[MAX_BUF_SIZE];
377 FILE* fp;
378 iconv_t codeSet;
379 char inbuf[MAX_WIKI_TEXT_SIZE];
380 char outbuf[MAX_WIKI_TEXT_SIZE];
381 const char* inbufPtr;
382 char* outbufPtr;
383 int inbufSize;
384 int outbufSize;
385
386 codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM);
387 if(codeSet == (iconv_t)-1) {
388 MN_FATAL_ERROR(wxT("failed iconv_open"));
389 }
390
391 sprintf(fullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str());
392 fp = fopen(fullPath, "wb");
393 if(fp == NULL) {
394 MN_FATAL_ERROR(wxT("File open error."));
395 }
396
397 memset(inbuf, 0, sizeof(inbuf));
398 strcpy(inbuf,(const char*)text->mb_str());
399 inbufPtr = inbuf;
400 inbufSize = strlen(inbufPtr);
401 outbufPtr = outbuf;
402 outbufSize = sizeof(outbuf);
403 memset(outbuf, 0, outbufSize);
404 iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize);
405 fwrite(outbuf, sizeof(outbuf)-outbufSize, 1, fp);
406 fclose(fp);
407 iconv_close(codeSet);
408 }
409
410 /******* Tools ************************/
411
412 static void toLower(char* string)
413 {
414 int i;
415
416 for(i = 0; string[i] != 0; i++) {
417 string[i] = tolower(string[i]);
418 }
419 }
420
421
422 static char* decode(const char* string)
423 {
424 static char buf[MAX_BUF_SIZE];
425 char c[5];
426 int i,j;
427 char* endPtr = NULL;
428
429 j = 0;
430 memset(buf, 0, MAX_BUF_SIZE);
431 for(i = 0; string[i] != 0; i+=2) {
432 c[0] = '0'; c[1] = 'x';
433 c[2] = string[i]; c[3] = string[i+1]; c[4] = 0;
434 buf[j] = strtol(c, &endPtr, 0);
435 j++;
436 if(j >= MAX_BUF_SIZE) {
437 buf[MAX_BUF_SIZE] = 0;
438 break;
439 }
440 }
441
442 return buf;
443 }
444
445 static char* encode(const char* string)
446 {
447 static char buf[MAX_BUF_SIZE];
448 int i,j;
449
450 j = 0;
451 memset(buf, 0, MAX_BUF_SIZE);
452 for(i = 0; string[i] != 0; i++) {
453 snprintf(&buf[j], 3, "%02X", (unsigned char)(string[i]));
454 j+=2;
455 if(j >= MAX_BUF_SIZE) {
456 buf[MAX_BUF_SIZE] = 0;
457 break;
458 }
459 }
460 return buf;
461 }
462
463 /*
464 * wiki1 > wiki2 -> return < 0
465 * wiki1 = wiki2 -> return = 0
466 * wiki1 < wiki2 -> return > 0
467 */
468 static int compWikiData(const void* wiki1, const void* wiki2)
469 {
470 WikiData* data1 = *(WikiData**)wiki1;
471 WikiData* data2 = *(WikiData**)wiki2;
472 const wxString* str1 = NULL;
473 const wxString* str2 = NULL;
474
475 str1 = data1->getDate();
476 str2 = data2->getDate();
477
478 if(str1 != NULL && str2 != NULL) {
479 return (strcmp(str2->mb_str(), str1->mb_str()));
480 }
481 else{
482 return 0;
483 }
484 }
485

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