| 4 |
#include <wx/regex.h> |
#include <wx/regex.h> |
| 5 |
#include <iconv.h> |
#include <iconv.h> |
| 6 |
|
|
| 7 |
/* プロトタイプ */ |
static void toLower(char* string); |
| 8 |
static char* decode(const char* string); |
static char* decode(const char* string); |
| 9 |
static char* encode(const char* string); |
static char* encode(const char* string); |
| 10 |
static int compWikiData(const void* wiki1, const void* wiki2); |
static int compWikiData(const void* wiki1, const void* wiki2); |
| 11 |
|
|
| 12 |
|
|
| 13 |
/******* WikiList ************************/ |
/******* WikiList ************************/ |
| 33 |
|
|
| 34 |
WikiList* mnModel::search(const char* searchStr) |
WikiList* mnModel::search(const char* searchStr) |
| 35 |
{ |
{ |
| 36 |
|
int i; |
| 37 |
wxDir* dir; |
wxDir* dir; |
| 38 |
FILE* fp; |
FILE* fp; |
| 39 |
wxString fullPathName; |
wxString fullPathName; |
|
char buf[MAX_BUF_SIZE]; |
|
| 40 |
WikiData* wikiData; |
WikiData* wikiData; |
| 41 |
WikiList* list = new WikiList(); |
WikiList* list = new WikiList(); |
| 42 |
wxString* fileName = new wxString(); |
wxString* fileName = new wxString(); |
| 43 |
wxRegEx* regStr; |
const char* decodeFileName; |
| 44 |
|
char decodeFileNameBuf[MAX_BUF_SIZE]; |
| 45 |
iconv_t codeSet; |
iconv_t codeSet; |
| 46 |
char outbuf[MAX_BUF_SIZE]; |
char outbuf[MAX_BUF_SIZE]; |
| 47 |
const char* inbufPtr = searchStr; |
const char* inbufPtr = searchStr; |
| 48 |
char* outbufPtr = outbuf; |
char* outbufPtr = outbuf; |
| 49 |
int inbufSize = strlen(searchStr); |
int inbufSize = strlen(searchStr); |
| 50 |
int outbufSize = sizeof(outbuf); |
int outbufSize = sizeof(outbuf); |
| 51 |
const char* decodeFileName; |
char* token; |
| 52 |
|
char* tokenList[32]; |
| 53 |
|
bool found; |
| 54 |
|
|
| 55 |
|
memset(tokenList, 0, sizeof(char*)*32); |
| 56 |
memset(outbuf, 0, outbufSize); |
memset(outbuf, 0, outbufSize); |
| 57 |
codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM); |
codeSet = iconv_open(CODE_SET_EUC_JP, CODE_SET_SYSTEM); |
| 58 |
if(codeSet == (iconv_t)-1) { |
if(codeSet == (iconv_t)-1) { |
| 59 |
MN_FATAL_ERROR(wxT("failed iconv_open")); |
MN_FATAL_ERROR(wxT("failed iconv_open")); |
| 60 |
} |
} |
| 61 |
iconv(codeSet, (const char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize); |
iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize); |
| 62 |
iconv_close(codeSet); |
iconv_close(codeSet); |
| 63 |
|
|
| 64 |
|
/* searchStr to Tokens */ |
| 65 |
|
token = strtok(outbuf, " "); |
| 66 |
|
if(token == NULL) return list; |
| 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); |
dir = new wxDir(*wikiDataDir); |
| 77 |
if ( !dir->IsOpened() ) |
if ( !dir->IsOpened() ) |
| 78 |
{ |
{ |
| 79 |
MN_FATAL_ERROR(wxT("wxDir has faild\n")); |
MN_FATAL_ERROR(wxT("wxDir has faild\n")); |
| 80 |
return NULL; |
return NULL; |
| 81 |
} |
} |
| 82 |
bool cont = dir->GetFirst(fileName, wxEmptyString, wxDIR_FILES); |
bool cont = dir->GetFirst(fileName, wxT("*.txt"), wxDIR_FILES); |
| 83 |
while(cont){ |
while(cont){ |
| 84 |
fullPathName = *wikiDataDir + wxT("/") + *fileName; |
fullPathName = *wikiDataDir + wxT("/") + *fileName; |
| 85 |
fp = fopen((const char*)fullPathName.mb_str(), "r"); |
fp = fopen((const char*)fullPathName.mb_str(), "r"); |
| 86 |
if(fp == NULL) { |
if(fp == NULL) { |
| 87 |
MN_FATAL_ERROR(wxT("fopen faild")); |
MN_FATAL_ERROR(wxT("fopen faild")); |
| 88 |
} |
} |
| 89 |
while(1){ |
|
| 90 |
memset(buf, 0, MAX_BUF_SIZE); |
/* TYPE search */ |
| 91 |
fread(buf, MAX_BUF_SIZE-1, 1, fp); |
if(strstr(tokenList[0], TYPESEARCH_TAG) == tokenList[0]) |
| 92 |
if(buf[0] == 0) break; |
{ |
| 93 |
|
found = typeSearch(tokenList[0], fp); |
| 94 |
|
if(found){ |
| 95 |
|
wikiData = new WikiData(wikiDataDir, (const char*)fileName->mb_str(), fp); |
| 96 |
|
list->Append(wikiData); |
| 97 |
|
} |
| 98 |
|
} |
| 99 |
|
/* Normal search */ |
| 100 |
|
else{ |
| 101 |
decodeFileName = decode(fileName->mb_str()); |
decodeFileName = decode(fileName->mb_str()); |
| 102 |
if(strstr((const char*)buf, (const char*)outbuf) || |
snprintf(decodeFileNameBuf, MAX_BUF_SIZE, "%s", decodeFileName); |
| 103 |
strstr((const char*)decodeFileName, (const char*)outbuf)) { |
toLower(decodeFileNameBuf); |
| 104 |
|
found = normalSearch(tokenList, fp, decodeFileNameBuf); |
| 105 |
|
if(found){ |
| 106 |
wikiData = new WikiData(wikiDataDir, (const char*)fileName->mb_str(), fp); |
wikiData = new WikiData(wikiDataDir, (const char*)fileName->mb_str(), fp); |
| 107 |
list->Append(wikiData); |
list->Append(wikiData); |
|
break; |
|
| 108 |
} |
} |
|
buf[0] = 0; |
|
| 109 |
} |
} |
| 110 |
fclose(fp); |
fclose(fp); |
| 111 |
cont = dir->GetNext(fileName); |
cont = dir->GetNext(fileName); |
| 112 |
} |
} |
| 113 |
delete dir; |
delete dir; |
| 114 |
|
delete fileName; |
| 115 |
|
for(i = 0; tokenList[i] != NULL; i++) free(tokenList[i]); |
| 116 |
|
|
| 117 |
list->Sort(compWikiData); |
list->Sort(compWikiData); |
| 118 |
return list; |
return list; |
| 119 |
} |
} |
| 120 |
|
|
| 121 |
|
bool mnModel::normalSearch(char* tokenList[], FILE*fp, char* decodeFileNameBuf) |
| 122 |
|
{ |
| 123 |
|
char buf[MAX_BUF_SIZE]; |
| 124 |
|
bool found; |
| 125 |
|
int i; |
| 126 |
|
while(1){ |
| 127 |
|
memset(buf, 0, MAX_BUF_SIZE); |
| 128 |
|
fread(buf, MAX_BUF_SIZE-1, 1, fp); |
| 129 |
|
if(buf[0] == 0) break; |
| 130 |
|
toLower(buf); |
| 131 |
|
found = TRUE; |
| 132 |
|
for(i = 0; tokenList[i] != NULL; i++){ |
| 133 |
|
toLower(tokenList[i]); |
| 134 |
|
if(strstr((const char*)buf, (const char*)tokenList[i]) || /* search in file context */ |
| 135 |
|
strstr((const char*)decodeFileNameBuf, (const char*)tokenList[i]) || /* search in file name */ |
| 136 |
|
strcmp((const char*)tokenList[i], (const char*)ALLMEMO_TAG) == 0) { /* SHOW ALL MEMO */ |
| 137 |
|
found = TRUE; |
| 138 |
|
} |
| 139 |
|
else { |
| 140 |
|
found = FALSE; |
| 141 |
|
break; |
| 142 |
|
} |
| 143 |
|
} |
| 144 |
|
|
| 145 |
|
if(found){ /* all tokens found */ |
| 146 |
|
break; |
| 147 |
|
} |
| 148 |
|
buf[0] = 0; |
| 149 |
|
} |
| 150 |
|
|
| 151 |
|
return found; |
| 152 |
|
} |
| 153 |
|
|
| 154 |
|
bool mnModel::typeSearch(char* typeStr, FILE*fp) |
| 155 |
|
{ |
| 156 |
|
char buf[MAX_BUF_SIZE]; |
| 157 |
|
bool found; |
| 158 |
|
int i; |
| 159 |
|
char* typeToken; |
| 160 |
|
char typeStrCopy[MAX_BUF_SIZE]; |
| 161 |
|
|
| 162 |
|
snprintf(typeStrCopy, MAX_BUF_SIZE, "%s", typeStr); |
| 163 |
|
while(1){ |
| 164 |
|
memset(buf, 0, MAX_BUF_SIZE); |
| 165 |
|
fgets(buf, MAX_BUF_SIZE, fp); |
| 166 |
|
if(buf[0] == 0) break; |
| 167 |
|
if(strstr((const char*)buf, TYPE_TAG)){ /* search TYPE line */ |
| 168 |
|
typeToken = strtok(typeStrCopy, ":"); |
| 169 |
|
typeToken = strtok(NULL, ":"); /* second field separated by colon(:) */ |
| 170 |
|
toLower(typeToken); |
| 171 |
|
toLower(buf); |
| 172 |
|
if(strstr(buf, typeToken)) return true; |
| 173 |
|
} |
| 174 |
|
} |
| 175 |
|
return false; |
| 176 |
|
} |
| 177 |
|
|
| 178 |
void mnModel::addSearchStr(wxString* searchStr) |
void mnModel::addSearchStr(wxString* searchStr) |
| 179 |
{ |
{ |
| 180 |
wxString *string; |
wxString *string; |
| 181 |
|
|
| 182 |
if(searchStrList->Index(searchStr->c_str()) == wxNOT_FOUND){ |
if(searchStrList->Index(searchStr->c_str()) == wxNOT_FOUND){ |
| 183 |
string = new wxString(searchStr->c_str()); |
string = new wxString(searchStr->c_str()); |
| 184 |
searchStrList->Add(*string, 1); |
//searchStrList->Add(*string, 1); |
| 185 |
|
searchStrList->Insert(*string, 0); |
| 186 |
} |
} |
| 187 |
} |
} |
| 188 |
|
|
| 202 |
itemStr.sprintf(wxT("%s"), newStr->c_str()); |
itemStr.sprintf(wxT("%s"), newStr->c_str()); |
| 203 |
} |
} |
| 204 |
} |
} |
| 205 |
|
const wxString* mnModel::getWikiDataDir() |
| 206 |
|
{ |
| 207 |
|
return wikiDataDir; |
| 208 |
|
} |
| 209 |
|
|
| 210 |
const wxArrayString* mnModel::getSearchStrList() |
const wxArrayString* mnModel::getSearchStrList() |
| 211 |
{ |
{ |
| 243 |
decodeStr = decode(file); |
decodeStr = decode(file); |
| 244 |
inbufSize = strlen(decodeStr); |
inbufSize = strlen(decodeStr); |
| 245 |
outbufSize = sizeof(outbuf); |
outbufSize = sizeof(outbuf); |
| 246 |
iconv(codeSet, (const char**)&decodeStr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize); |
iconv(codeSet, (ICONV_CONST char**)&decodeStr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize); |
| 247 |
subject = new wxString((const char*)outbuf, conv); |
subject = new wxString((const char*)outbuf, conv); |
| 248 |
iconv_close(codeSet); |
iconv_close(codeSet); |
| 249 |
|
|
| 327 |
if(codeSet == (iconv_t)-1) { |
if(codeSet == (iconv_t)-1) { |
| 328 |
MN_FATAL_ERROR(wxT("failed iconv_open")); |
MN_FATAL_ERROR(wxT("failed iconv_open")); |
| 329 |
} |
} |
| 330 |
iconv(codeSet, (const char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize); |
iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize); |
| 331 |
iconv_close(codeSet); |
iconv_close(codeSet); |
| 332 |
subject = new wxString(newSubject->c_str()); |
subject = new wxString(newSubject->c_str()); |
| 333 |
fileName = new wxString(encode(outbuf), conv); |
fileName = new wxString(encode(outbuf), conv); |
| 337 |
sprintf(newFullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str()); |
sprintf(newFullPath, "%s/%s", (const char*)dataDirName->mb_str(), (const char*)fileName->mb_str()); |
| 338 |
|
|
| 339 |
if((fp = fopen(newFullPath, "r")) == NULL) { |
if((fp = fopen(newFullPath, "r")) == NULL) { |
| 340 |
rename(oldFullPath, newFullPath); |
if(rename(oldFullPath, newFullPath) < 0) wxLogMessage(wxT("rename error: errno=[%d]"), errno); |
| 341 |
} |
} |
| 342 |
else { |
else if(strcmp(oldFullPath, newFullPath)){ |
| 343 |
wxLogMessage(wxT("File has already exist. [%s]"), fileName->c_str()); |
wxLogMessage(wxT("File has already exist. [%s]"), fileName->c_str()); |
| 344 |
fclose(fp); |
fclose(fp); |
| 345 |
} |
} |
| 346 |
|
else { |
| 347 |
|
fclose(fp); |
| 348 |
|
} |
| 349 |
|
|
| 350 |
delete oldSubject; |
delete oldSubject; |
| 351 |
delete oldFileName; |
delete oldFileName; |
| 389 |
} |
} |
| 390 |
|
|
| 391 |
while(fgets(buf, MAX_BUF_SIZE, fp)) { |
while(fgets(buf, MAX_BUF_SIZE, fp)) { |
| 392 |
|
#ifdef __WXMAC__ |
| 393 |
|
for(int i = 0; buf[i] != 0; i++) if(buf[i] == (char)MAC_BACKSLASH) buf[i] = '\\'; |
| 394 |
|
#endif |
| 395 |
inbufPtr = buf; |
inbufPtr = buf; |
| 396 |
inbufSize = sizeof(buf); |
inbufSize = sizeof(buf); |
| 397 |
outbufPtr = outbuf; |
outbufPtr = outbuf; |
| 398 |
outbufSize = sizeof(outbuf); |
outbufSize = sizeof(outbuf); |
| 399 |
memset(outbuf, 0, outbufSize); |
memset(outbuf, 0, outbufSize); |
| 400 |
iconv(codeSet, (const char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize); |
iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize); |
| 401 |
tmpStr = new wxString((char*)outbuf, conv); |
tmpStr = new wxString((char*)outbuf, conv); |
| 402 |
*text += *tmpStr; |
*text += *tmpStr; |
| 403 |
delete tmpStr; |
delete tmpStr; |
| 410 |
|
|
| 411 |
void WikiData::modText(wxString* intext) |
void WikiData::modText(wxString* intext) |
| 412 |
{ |
{ |
| 413 |
|
wxCSConv conv(wxT(CODE_SET_SYSTEM)); |
| 414 |
delete text; |
delete text; |
| 415 |
text = new wxString(intext->c_str()); |
//text = new wxString(intext->c_str()); |
| 416 |
|
text = new wxString(*intext); |
| 417 |
|
//text = new wxString(intext->mb_str(), conv); |
| 418 |
} |
} |
| 419 |
|
|
| 420 |
void WikiData::removeDataFile() |
void WikiData::removeDataFile() |
| 452 |
|
|
| 453 |
memset(inbuf, 0, sizeof(inbuf)); |
memset(inbuf, 0, sizeof(inbuf)); |
| 454 |
strcpy(inbuf,(const char*)text->mb_str()); |
strcpy(inbuf,(const char*)text->mb_str()); |
| 455 |
|
|
| 456 |
|
#ifdef __WXMAC__ |
| 457 |
|
for(int i = 0; inbuf[i] != 0; i++) if(inbuf[i] == (char)MAC_BACKSLASH) inbuf[i] = '\\'; |
| 458 |
|
#endif |
| 459 |
|
|
| 460 |
inbufPtr = inbuf; |
inbufPtr = inbuf; |
| 461 |
inbufSize = strlen(inbufPtr); |
inbufSize = strlen(inbufPtr); |
| 462 |
outbufPtr = outbuf; |
outbufPtr = outbuf; |
| 463 |
outbufSize = sizeof(outbuf); |
outbufSize = sizeof(outbuf); |
| 464 |
memset(outbuf, 0, outbufSize); |
memset(outbuf, 0, outbufSize); |
| 465 |
iconv(codeSet, (const char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize); |
iconv(codeSet, (ICONV_CONST char**)&inbufPtr, (size_t*)&inbufSize, &outbufPtr, (size_t*)&outbufSize); |
| 466 |
fwrite(outbuf, sizeof(outbuf)-outbufSize, 1, fp); |
fwrite(outbuf, sizeof(outbuf)-outbufSize, 1, fp); |
| 467 |
fclose(fp); |
fclose(fp); |
| 468 |
iconv_close(codeSet); |
iconv_close(codeSet); |
| 470 |
|
|
| 471 |
/******* Tools ************************/ |
/******* Tools ************************/ |
| 472 |
|
|
| 473 |
|
static void toLower(char* string) |
| 474 |
|
{ |
| 475 |
|
int i; |
| 476 |
|
|
| 477 |
|
for(i = 0; string[i] != 0; i++) { |
| 478 |
|
string[i] = tolower(string[i]); |
| 479 |
|
} |
| 480 |
|
} |
| 481 |
|
|
| 482 |
|
|
| 483 |
static char* decode(const char* string) |
static char* decode(const char* string) |
| 484 |
{ |
{ |
| 485 |
static char buf[MAX_BUF_SIZE]; |
static char buf[MAX_BUF_SIZE]; |