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.2 - (show annotations) (download) (as text)
Sun Jul 31 06:54:12 2005 UTC (18 years, 7 months ago) by maloninc
Branch: MAIN
CVS Tags: dev-1_0-0010, dev-1_0-0009, dev-1_0-0008, dev-1_0-0007
Changes since 1.1: +1 -1 lines
File MIME type: text/x-c++src
implement data edit dialog, but not data entry dialog.

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

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