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