| 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(decodeFileNameBuf); |
| 98 |
found = TRUE; |
| 99 |
for(i = 0; tokenList[i] != NULL; i++){ |
| 100 |
toLower(tokenList[i]); |
| 101 |
if(strstr((const char*)buf, (const char*)tokenList[i]) || |
| 102 |
strstr((const char*)decodeFileNameBuf, (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 |
if(rename(oldFullPath, newFullPath) < 0) wxLogMessage(wxT("rename error: errno=[%d]"), errno); |
| 291 |
} |
| 292 |
else if(strcmp(oldFullPath, newFullPath)){ |
| 293 |
wxLogMessage(wxT("File has already exist. [%s]"), fileName->c_str()); |
| 294 |
fclose(fp); |
| 295 |
} |
| 296 |
else { |
| 297 |
fclose(fp); |
| 298 |
} |
| 299 |
|
| 300 |
delete oldSubject; |
| 301 |
delete oldFileName; |
| 302 |
} |
| 303 |
|
| 304 |
const wxString* WikiData::getDate() |
| 305 |
{ |
| 306 |
return date; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
const wxString* WikiData::getText() |
| 311 |
{ |
| 312 |
FILE* fp; |
| 313 |
char buf[MAX_BUF_SIZE]; |
| 314 |
char fullPath[MAX_BUF_SIZE]; |
| 315 |
iconv_t codeSet; |
| 316 |
char outbuf[MAX_BUF_SIZE]; |
| 317 |
char* inbufPtr; |
| 318 |
char* outbufPtr; |
| 319 |
int inbufSize; |
| 320 |
int outbufSize; |
| 321 |
wxCSConv conv(wxT(CODE_SET_SYSTEM)); |
| 322 |
wxString* tmpStr; |
| 323 |
|
| 324 |
codeSet = iconv_open(CODE_SET_SYSTEM, CODE_SET_EUC_JP); |
| 325 |
if(codeSet == (iconv_t)-1) { |
| 326 |
MN_FATAL_ERROR(wxT("failed iconv_open")); |
| 327 |
} |
| 328 |
|
| 329 |
if(text) { |
| 330 |
iconv_close(codeSet); |
| 331 |
return text; |
| 332 |
} |
| 333 |
|
| 334 |
text = new wxString(); |
| 335 |
sprintf(fullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str()); |
| 336 |
fp = fopen(fullPath, "r"); |
| 337 |
if(fp == NULL) { |
| 338 |
MN_FATAL_ERROR(wxT("File open error.")); |
| 339 |
} |
| 340 |
|
| 341 |
while(fgets(buf, MAX_BUF_SIZE, fp)) { |
| 342 |
#ifdef __WXMAC__ |
| 343 |
for(int i = 0; buf[i] != 0; i++) if(buf[i] == (char)MAC_BACKSLASH) buf[i] = '\\'; |
| 344 |
#endif |
| 345 |
inbufPtr = buf; |
| 346 |
inbufSize = sizeof(buf); |
| 347 |
outbufPtr = outbuf; |
| 348 |
outbufSize = sizeof(outbuf); |
| 349 |
memset(outbuf, 0, outbufSize); |
| 350 |
iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize); |
| 351 |
tmpStr = new wxString((char*)outbuf, conv); |
| 352 |
*text += *tmpStr; |
| 353 |
delete tmpStr; |
| 354 |
} |
| 355 |
iconv_close(codeSet); |
| 356 |
fclose(fp); |
| 357 |
|
| 358 |
return text; |
| 359 |
} |
| 360 |
|
| 361 |
void WikiData::modText(wxString* intext) |
| 362 |
{ |
| 363 |
wxCSConv conv(wxT(CODE_SET_SYSTEM)); |
| 364 |
delete text; |
| 365 |
//text = new wxString(intext->c_str()); |
| 366 |
text = new wxString(*intext); |
| 367 |
//text = new wxString(intext->mb_str(), conv); |
| 368 |
} |
| 369 |
|
| 370 |
void WikiData::removeDataFile() |
| 371 |
{ |
| 372 |
char fullPath[MAX_BUF_SIZE]; |
| 373 |
|
| 374 |
sprintf(fullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str()); |
| 375 |
if(remove(fullPath)) { |
| 376 |
MN_FATAL_ERROR(wxT("remove file error")); |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
void WikiData::save() |
| 381 |
{ |
| 382 |
char fullPath[MAX_BUF_SIZE]; |
| 383 |
FILE* fp; |
| 384 |
iconv_t codeSet; |
| 385 |
char inbuf[MAX_WIKI_TEXT_SIZE]; |
| 386 |
char outbuf[MAX_WIKI_TEXT_SIZE]; |
| 387 |
const char* inbufPtr; |
| 388 |
char* outbufPtr; |
| 389 |
int inbufSize; |
| 390 |
int outbufSize; |
| 391 |
|
| 392 |
codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM); |
| 393 |
if(codeSet == (iconv_t)-1) { |
| 394 |
MN_FATAL_ERROR(wxT("failed iconv_open")); |
| 395 |
} |
| 396 |
|
| 397 |
sprintf(fullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str()); |
| 398 |
fp = fopen(fullPath, "wb"); |
| 399 |
if(fp == NULL) { |
| 400 |
MN_FATAL_ERROR(wxT("File open error.")); |
| 401 |
} |
| 402 |
|
| 403 |
memset(inbuf, 0, sizeof(inbuf)); |
| 404 |
strcpy(inbuf,(const char*)text->mb_str()); |
| 405 |
|
| 406 |
#ifdef __WXMAC__ |
| 407 |
for(int i = 0; inbuf[i] != 0; i++) if(inbuf[i] == (char)MAC_BACKSLASH) inbuf[i] = '\\'; |
| 408 |
#endif |
| 409 |
|
| 410 |
inbufPtr = inbuf; |
| 411 |
inbufSize = strlen(inbufPtr); |
| 412 |
outbufPtr = outbuf; |
| 413 |
outbufSize = sizeof(outbuf); |
| 414 |
memset(outbuf, 0, outbufSize); |
| 415 |
iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize); |
| 416 |
fwrite(outbuf, sizeof(outbuf)-outbufSize, 1, fp); |
| 417 |
fclose(fp); |
| 418 |
iconv_close(codeSet); |
| 419 |
} |
| 420 |
|
| 421 |
/******* Tools ************************/ |
| 422 |
|
| 423 |
static void toLower(char* string) |
| 424 |
{ |
| 425 |
int i; |
| 426 |
|
| 427 |
for(i = 0; string[i] != 0; i++) { |
| 428 |
string[i] = tolower(string[i]); |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
static char* decode(const char* string) |
| 434 |
{ |
| 435 |
static char buf[MAX_BUF_SIZE]; |
| 436 |
char c[5]; |
| 437 |
int i,j; |
| 438 |
char* endPtr = NULL; |
| 439 |
|
| 440 |
j = 0; |
| 441 |
memset(buf, 0, MAX_BUF_SIZE); |
| 442 |
for(i = 0; string[i] != 0; i+=2) { |
| 443 |
c[0] = '0'; c[1] = 'x'; |
| 444 |
c[2] = string[i]; c[3] = string[i+1]; c[4] = 0; |
| 445 |
buf[j] = strtol(c, &endPtr, 0); |
| 446 |
j++; |
| 447 |
if(j >= MAX_BUF_SIZE) { |
| 448 |
buf[MAX_BUF_SIZE] = 0; |
| 449 |
break; |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
return buf; |
| 454 |
} |
| 455 |
|
| 456 |
static char* encode(const char* string) |
| 457 |
{ |
| 458 |
static char buf[MAX_BUF_SIZE]; |
| 459 |
int i,j; |
| 460 |
|
| 461 |
j = 0; |
| 462 |
memset(buf, 0, MAX_BUF_SIZE); |
| 463 |
for(i = 0; string[i] != 0; i++) { |
| 464 |
snprintf(&buf[j], 3, "%02X", (unsigned char)(string[i])); |
| 465 |
j+=2; |
| 466 |
if(j >= MAX_BUF_SIZE) { |
| 467 |
buf[MAX_BUF_SIZE] = 0; |
| 468 |
break; |
| 469 |
} |
| 470 |
} |
| 471 |
return buf; |
| 472 |
} |
| 473 |
|
| 474 |
/* |
| 475 |
* wiki1 > wiki2 -> return < 0 |
| 476 |
* wiki1 = wiki2 -> return = 0 |
| 477 |
* wiki1 < wiki2 -> return > 0 |
| 478 |
*/ |
| 479 |
static int compWikiData(const void* wiki1, const void* wiki2) |
| 480 |
{ |
| 481 |
WikiData* data1 = *(WikiData**)wiki1; |
| 482 |
WikiData* data2 = *(WikiData**)wiki2; |
| 483 |
const wxString* str1 = NULL; |
| 484 |
const wxString* str2 = NULL; |
| 485 |
|
| 486 |
str1 = data1->getDate(); |
| 487 |
str2 = data2->getDate(); |
| 488 |
|
| 489 |
if(str1 != NULL && str2 != NULL) { |
| 490 |
return (strcmp(str2->mb_str(), str1->mb_str())); |
| 491 |
} |
| 492 |
else{ |
| 493 |
return 0; |
| 494 |
} |
| 495 |
} |
| 496 |
|