bool create_bob_txt() { // current directory 를 구한다. wchar_t *buf=NULL; uint32_t buflen = 0; buflen = GetCurrentDirectoryW(buflen, buf); if (0 == buflen) { print("err, GetCurrentDirectoryW() failed. gle = 0x%08x", GetLastError()); return false; } buf = (PWSTR) malloc(sizeof(WCHAR) * buflen); if (0 == GetCurrentDirectoryW(buflen, buf)) { print("err, GetCurrentDirectoryW() failed. gle = 0x%08x", GetLastError()); free(buf); return false; } // current dir \\ bob.txt 파일명 생성 wchar_t file_name[260]; if (!SUCCEEDED(StringCbPrintfW( file_name, sizeof(file_name), L"%ws\\bob.txt", buf))) { print("err, can not create file name"); free(buf); return false; } if (true == is_file_existsW(file_name)) { ::DeleteFileW(file_name); } // 파일 생성 HANDLE file_handle = CreateFileW( file_name, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); if(file_handle == INVALID_HANDLE_VALUE) { print("err, CreateFile(path=%ws), gle=0x%08x", file_name, GetLastError()); return false; } // 파일에 데이터 쓰기 DWORD bytes_written = 0; wchar_t string_buf[1024]; if (!SUCCEEDED(StringCbPrintfW( string_buf, sizeof(string_buf), L"동해물과 백두산이 마르고 닳도록 하느님이 보우하사 우리나라만세"))) { print("err, can not create data to write."); CloseHandle(file_handle); return false; } char *pUtf8 =NULL; int len = UnicodeToUtf8(string_buf,&pUtf8); if (!WriteFile(file_handle, pUtf8, len, &bytes_written, NULL)) { print("err, WriteFile() failed. gle = 0x%08x", GetLastError()); CloseHandle(file_handle); return false; } // 영어로 쓰기 if (!SUCCEEDED(StringCbPrintfW( string_buf, sizeof(string_buf), L"All work and no play makes jack a dull boy."))) { print("err, can not create data to write."); CloseHandle(file_handle); return false; } free(pUtf8); *pUtf8 =NULL; len = UnicodeToUtf8(string_buf,&pUtf8); if (!WriteFile(file_handle, string_buf, wcslen(string_buf), &bytes_written, NULL)) { print("err, WriteFile() failed. gle = 0x%08x", GetLastError()); CloseHandle(file_handle); return false; } char string_bufa[1024]; if (!SUCCEEDED(StringCbPrintfA( string_bufa, sizeof(string_bufa), "동해물과 백두산이 마르고 닳도록 하느님이 보우하사 우리나라만세"))) { print("err, can not create data to write."); CloseHandle(file_handle); return false; } free(pUtf8); pUtf8 =NULL; pUtf8 =ANSIToUTF8(string_bufa); len = strlen(pUtf8); if (!WriteFile(file_handle, pUtf8, len, &bytes_written, NULL)) { print("err, WriteFile() failed. gle = 0x%08x", GetLastError()); CloseHandle(file_handle); return false; } // 영어로 쓰기 if (!SUCCEEDED(StringCbPrintfA( string_bufa, sizeof(string_bufa), "All work and no play makes jack a dull boy."))) { print("err, can not create data to write."); CloseHandle(file_handle); return false; } free(pUtf8); pUtf8 =NULL; pUtf8 =ANSIToUTF8(string_bufa); len = strlen(pUtf8); if (!WriteFile(file_handle, string_bufa, strlen(string_bufa), &bytes_written, NULL)) { print("err, WriteFile() failed. gle = 0x%08x", GetLastError()); CloseHandle(file_handle); return false; } free(pUtf8); // 파일 닫기 CloseHandle(file_handle); wchar_t copy_file_name[260]; if (!SUCCEEDED(StringCbPrintfW( copy_file_name, sizeof(copy_file_name), L"%ws\\bob2.txt", buf))) { print("err, can not copy file name"); free(buf); return false; } free(buf); buf = NULL; /* BOB.txt -> BOB2.txt 파일 복사 */ if(!CopyFileW(file_name,copy_file_name,false)){ print("CopyFileW fail"); return false; } /* ReadFile()을 통한 읽기 후 출력*/ HANDLE read_file_handle = CreateFileW( copy_file_name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); if(read_file_handle == INVALID_HANDLE_VALUE) { print("err, CreateFile(path=%ws), gle=0x%08x", read_file_handle, GetLastError()); return false; } char read_buf[512]; char buf_noNull[512]; char* ansi_buf; DWORD result=0; ReadFile(read_file_handle,read_buf,sizeof(char)*512,&result,NULL); // read_buf의 글자 사이사이에 있는 NULL값 제거 ( UTF8ToANSI 에서 strlen을 사용하기 때문) int j=0; for(int i=0;i<256;i++){ if(read_buf[i] != '\0'){ buf_noNull[j++] = read_buf[i]; } } buf_noNull[j]='\0'; // 출력 ansi_buf=UTF8ToANSI(buf_noNull); printf("%s\n",ansi_buf); CloseHandle(read_file_handle); /* MMIO()을 통한 읽기 후 출력*/ read_file_handle = CreateFileW( copy_file_name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); if(read_file_handle == INVALID_HANDLE_VALUE) { print("err, CreateFile(path=%ws), gle=0x%08x", read_file_handle, GetLastError()); return false; } HANDLE file_map = CreateFileMapping( read_file_handle, NULL, PAGE_READONLY, 0, 0, NULL ); if (NULL == file_map) { print("err, CreateFileMapping(%ws) failed, gle = %u", file_name, GetLastError()); CloseHandle(read_file_handle); return false; } PCHAR file_view = (PCHAR) MapViewOfFile( file_map, FILE_MAP_READ, 0, 0, 0 ); if(file_view == NULL) { print("err, MapViewOfFile(%ws) failed, gle = %u", file_name, GetLastError()); CloseHandle(file_map); CloseHandle(read_file_handle); return false; } // read_buf의 글자 사이사이에 있는 NULL값 제거 ( UTF8ToANSI 에서 strlen을 사용하기 때문) j=0; for(int i=0;i<256;i++){ if(read_buf[i] != '\0'){ buf_noNull[j++] = file_view[i]; } } buf_noNull[j]='\0'; // 출력 ansi_buf=UTF8ToANSI(buf_noNull); printf("%s\n",ansi_buf); UnmapViewOfFile(file_view); CloseHandle(file_map); CloseHandle(read_file_handle); /* BOB.txt 파일 삭제*/ ::DeleteFileW(file_name); return true; }
bool CTaskMgr::Init() { const char* str = NULL; bool r = CSysSettingMgr::GetInstance()->GetStringValue("Chapter_Map_Born_Point", str); ASSERT(r && str); CHECK_FALSE_RETURN_FALSE(r && str); split_string_2i(std::string(str), m_CharpterMapBornPoint, "|"); //1. 读配置 组织基本map //2. 组织各个查询map,并且一旦有问题 就报错!! MYSQL* con = CDBMgr::GetInstance()->GetConnection(); //解析task表! char* sql = SELECT_TASK; int code = CDBMgr::Query(con, sql, (unsigned int)strlen(sql)); if (0 != code) //非0查询失败 { printf("query failed! [%s]\n", sql); return false; } MYSQL_RES* res = mysql_store_result(con); //保存查询结果 { //检查表结构是否有改变 //string task_need_fields[] = {"id", "task_id", "type", "name", "desc", "mappoint_id", "priority", "has_enter_condition", "prize_element_type", "prize_element_id", "prize_element_num", "img", "unlock_mappoint_id", "hidden"}; string need_fields[] = {"id", "task_id", "type", "name", "desc", "mappoint_id", "priority", "total_enter_num_per_day", "enter_condition_arr", "prize_element_type_id_num_arr", "unlock_mappoint_id_arr", "background_img", "hidden"}; std::vector<std::string> v_task_need_field(need_fields, need_fields + sizeof(need_fields)/sizeof(need_fields[0])); ASSERT(CDBMgr::check_fields(res, v_task_need_field)); } MYSQL_ROW row; CMapMgr* pMapMgr = CMapMgr::GetInstance(); while (row = mysql_fetch_row(res)) { int col = 1; CTask *pTask = new CTask(); pTask->iTaskID = atoi(row[col++]); pTask->eType = (CTask::ETASKTYPE)atoi(row[col++]); ASSERT( pTask->eType > CTask::ETASKTYPE_MIN && pTask->eType < CTask::ETASKTYPE_MAX); char* pBuf = UTF8ToANSI(row[col++]); int len = strlen(pBuf); ASSERT( len <= MAXLEN_TASKNAME); memcpy(pTask->strName, pBuf, len); free(pBuf); pBuf = UTF8ToANSI(row[col++]); len = strlen(pBuf); ASSERT( len <= MAXLEN_TASKDESC); memcpy(pTask->strDesc, pBuf, len); free(pBuf); pTask->iMapPointID = atoi(row[col++]); ASSERT(CMapMgr::GetInstance()->GetMapPointByID(pTask->iMapPointID));//验证所属据点存在 pTask->iPriority = atoi(row[col++]); pTask->iTotalEnterNumPerDay = atoi(row[col++]); //解析显示条件配置 <--服务器已经去掉这个设定 //解析进入条件配置! pBuf = UTF8ToANSI(row[col++]); bool r = ParseCondition(pBuf, pTask->enterConditions); free(pBuf); pBuf = UTF8ToANSI(row[col++]); //len = strlen(pBuf); //ASSERT( len <= MAXNUM_TASKPRIZE_STRLEN); //分割字符串 vector<string> prizes; split_string(string(pBuf), prizes, "|"); free(pBuf); int i = 0; //ASSERT(prizes.size()<= MAXNUM_TASKPRIZE_NUM); for (vector<string>::iterator it = prizes.begin() ; it != prizes.end() ; ++it){ vector<int> aprize; split_string_2i(*it, aprize, "-"); ASSERT(aprize.size() == 3); pTask->prizes.push_back(stGameElement((EELEMENTTYPE)aprize[0], aprize[1], 0, stGameElement::INCREASE, aprize[2])); IsElementValid(pTask->prizes[i]); ++i; } //pTask->iPrizeElementType = (EELEMENTTYPE)atoi(row[8]); //ASSERT(pTask->iPrizeElementType < EELEMENTTYPE::EELEMENTTYPE_MAX); //pTask->iPrizeElementID = atoi(row[9]); //pTask->iPrizeElementNum = atoi(row[10]); pBuf = UTF8ToANSI(row[col++]); len = strlen(pBuf); ASSERT( len <= MAXLEN_TASKIMG); memcpy(pTask->strImg, pBuf, len); free(pBuf); //pTask->iUnlockMappointID = atoi(row[12]); //ASSERT(CMapMgr::GetInstance()->GetMapPointByID(pTask->iUnlockMappointID));//验证所属据点存在 pBuf = UTF8ToANSI(row[col++]); //len = strlen(pBuf); vector<int> mappoints; split_string_2i(string(pBuf), mappoints, "|"); ASSERT(mappoints.size() <= MAXNUM_JOINEDMAPPOINT); i = 0; for (vector<int>::iterator it = mappoints.begin() ; it != mappoints.end() ;++it) { UINT32 iMappointID = *it; ASSERT(pMapMgr->GetMapPointByID(iMappointID)); pTask->iUnlockMappointID[i++] = iMappointID; } free(pBuf); pTask->bHidden = (bool)atoi(row[col]); ASSERT(AddTask(pTask)); } mysql_free_result(res); sql = SELECT_SUBTASK; code = CDBMgr::Query(con, sql, (unsigned int)strlen(sql)); if (0 != code) //非0查询失败 { printf("query failed! [%s]\n", sql); return false; } res = mysql_store_result(con); //保存查询结果 //检查表结构是否有改变 //, "battle_id_difficulty_time_arr"去掉这个配置! 《-还是保留了 { string need_fields[] = {"id", "sub_task_id", "task_id", "priority", "spend_health", "total_enter_num_per_day", "normal_packet_id", "auto_battle_packet_id", "extra_packet_id", "extra_packet_time"}; std::vector<std::string> v_subtask_need_field(need_fields, need_fields + sizeof(need_fields)/sizeof(need_fields[0])); ASSERT(CDBMgr::check_fields(res, v_subtask_need_field)); } while (row = mysql_fetch_row(res)) { stSubTask *pSubTask = new stSubTask(); memset(pSubTask, 0, sizeof stSubTask); int col = 1;//忽略主键id pSubTask->iSubTaskID = atoi(row[col++]); pSubTask->iTaskID = atoi(row[col++]); ASSERT(GetTaskByTaskID(pSubTask->iTaskID) && "子任务初始化有问题 其父任务不存在!"); pSubTask->iPriority = atoi(row[col++]); pSubTask->iSpendHealth = atoi(row[col++]); pSubTask->iTotalEnterNumPerDay = atoi(row[col++]); pSubTask->iBurstPacketID = atoi(row[col++]); ASSERT(CBurstMgr::GetInstance()->GetPacketByPacketID(pSubTask->iBurstPacketID)); pSubTask->iAutoBattleBurstPacketID = atoi(row[col++]); ASSERT(CBurstMgr::GetInstance()->GetPacketByPacketID(pSubTask->iAutoBattleBurstPacketID)); pSubTask->iExtraPacketID = atoi(row[col++]); if (pSubTask->iExtraPacketID){ ASSERT(CBurstMgr::GetInstance()->GetPacketByPacketID(pSubTask->iExtraPacketID)); //解析额外掉落包条件配置! char* pBuf = UTF8ToANSI(row[col++]); bool r = ParseTimeCondition(pBuf, pSubTask->ExtraPacketCondition); free(pBuf); } //char* pBuf = UTF8ToANSI(row[col++]); ////len = strlen(pBuf); ////ASSERT( len <= MAXLEN_TASKIMG); //vector<string> battlecontrols; //split_string(string(pBuf), battlecontrols, "|"); //free(pBuf); //int i = 0; //ASSERT(battlecontrols.size()<= MAXNUM_TASKBATTLE); //for (vector<string>::iterator it = battlecontrols.begin() ; it != battlecontrols.end() ; ++it) //{ // vector<int> aBattle; // split_string_2i(*it, aBattle, "-"); // ASSERT(aBattle.size() == 2); // ASSERT(CFightMgr::GetInstance()->GetBattleByID(aBattle[0])); // pSubTask->battles[i].iBattleID = aBattle[0]; // //pSubTask->battles[i].iDifficulty = aBattle[1]; // //pSubTask->battles[i].iTimeLimit = aBattle[2]; // pSubTask->battles[i].iTimeLimit = aBattle[1]; // ++i; //} bool r = AddSubTask(pSubTask); ASSERT(r); } sql = SELECT_SUB_TASK_MAIN_BRANCH_ID; code = CDBMgr::Query(con, sql, (unsigned int)strlen(sql)); if (0 != code) { printf("query failed! [%s]\n", sql); return false; } res = mysql_store_result(con); { string need_fields[] = {"chapter_id", "sub_task_id"}; std::vector<std::string> v_subtask_need_field(need_fields, need_fields + sizeof(need_fields)/sizeof(need_fields[0])); ASSERT(CDBMgr::check_fields(res, v_subtask_need_field)); } while (row = mysql_fetch_row(res)) { m_mapChapterID2MainBranchSubTaskIDs; int col = 0; UINT32 iChapterID = atoi(row[col++]); UINT32 iSubTaskID = atoi(row[col++]); m_mapChapterID2MainBranchSubTaskIDs[iChapterID].push_back(iSubTaskID); } mysql_free_result(res); return true; }
DWORD Enum_Current_Drectory(LPSTR sz_Utf8_Path,Json::Value *root) { Json::Value dir; Json::Value data; Json::Value attr; CHAR szFilePath[MAX_PATH]; CHAR szFullPath[MAX_PATH]; WIN32_FIND_DATA FindFileData; HANDLE hListFile; string szPath; string utf8_FullPath,utf8_FileName; UTF8ToANSI(sz_Utf8_Path,&szPath); if (szPath.c_str()[szPath.length()-1]!='\\') { sprintf_s(szFilePath,"%s\\*",szPath.c_str()); } else { sprintf_s(szFilePath,"%s*",szPath.c_str()); } hListFile = FindFirstFile(szFilePath,&FindFileData); if(hListFile==INVALID_HANDLE_VALUE) { Msg("开始遍历文件时出错:%d\r\n",GetLastError()); return 1; } else { do { if(lstrcmp(FindFileData.cFileName,TEXT("."))==0|| lstrcmp(FindFileData.cFileName,TEXT(".."))==0) { continue; } if (szPath.c_str()[szPath.length()-1]!='\\') { sprintf_s(szFullPath,"%s\\%s",szPath.c_str(),FindFileData.cFileName); } else { sprintf_s(szFullPath,"%s%s",szPath.c_str(),FindFileData.cFileName); } dwTotalFileNum++; utf8_FileName.erase(); utf8_FullPath.erase(); ANSIToUTF8(szFullPath,&utf8_FullPath); attr["path"]=Json::Value(utf8_FullPath.c_str()); ANSIToUTF8(FindFileData.cFileName,&utf8_FileName); data["title"]=Json::Value(utf8_FileName.c_str()); attr["title"]=Json::Value(utf8_FileName.c_str()); dir["data"]=data; dir["attr"]=attr; if(FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) { dir["state"]=Json::Value("closed"); } else { continue; } root->append(dir); } while(FindNextFile(hListFile, &FindFileData)); FindClose(hListFile); //谨记MSDN的教导: //When the search handle is no longer needed, close it by using the FindClose function, not CloseHandle. } szPath.erase(); utf8_FullPath.erase(); utf8_FileName.erase(); return 0; }
bool CSignMgr::Init() { MYSQL* con = CDBMgr::GetInstance()->GetConnection(); char* sql = SELECT_SIGN; int code = CDBMgr::Query(con, sql, (unsigned int)strlen(sql)); if (0 != code) { printf("query failed! [%s]\n", sql); return false; } MYSQL_RES* res = mysql_store_result(con); { std::string need_fields[] = {"id", "time", "sign_reward_1", "sign_reward_n"}; std::vector<std::string> v_task_need_field(need_fields, need_fields + sizeof(need_fields)/sizeof(need_fields[0])); ASSERT(CDBMgr::check_fields(res, v_task_need_field)); } MYSQL_ROW row; while(row = mysql_fetch_row(res)) { int col = 1; ASSERT(row[col++]); if (!row[col - 1]) { return false; } char* pBuf = UTF8ToANSI(row[col - 1]); std::vector<int> eleTime; split_string_2i(pBuf, eleTime, "|"); ASSERT(eleTime.size() == 2); std::pair<int, int> key = std::make_pair(eleTime[0], eleTime[1]); ASSERT(m_mapSign1Time2Rewards.find(key) == m_mapSign1Time2Rewards.end()); std::vector<stGameElement>& vecEleSign1 = m_mapSign1Time2Rewards[key]; free(pBuf); ASSERT(row[col++]); pBuf = UTF8ToANSI(row[col - 1]); std::vector<std::string> elements; split_string(pBuf, elements, "|"); ASSERT(GetDaysInMonth(eleTime[0], eleTime[1]) == elements.size()); free(pBuf); for (std::vector<std::string>::iterator it = elements.begin(); it != elements.end(); ++it) { std::vector<int> ele; split_string_2i(*it, ele, "-"); ASSERT(ele.size() == 3); stGameElement gameEle((EELEMENTTYPE)ele[0], ele[1], NULL, stGameElement::INCREASE, ele[2]); vecEleSign1.push_back(gameEle); } ASSERT(row[col++]); if (!row[col - 1]) { return false; } ASSERT(m_mapSignNTime2Rewards.find(key) == m_mapSignNTime2Rewards.end()); std::vector<stGameElement>& vecEleSignN = m_mapSignNTime2Rewards[key]; pBuf = UTF8ToANSI(row[col - 1]); elements.clear(); split_string(pBuf, elements, "|"); ASSERT(elements.size() == 4); free(pBuf); for (std::vector<std::string>::iterator it = elements.begin(); it != elements.end(); ++it) { std::vector<int> ele; split_string_2i(*it, ele, "-"); ASSERT(ele.size() == 3); stGameElement gameEle((EELEMENTTYPE)ele[0], ele[1], NULL, stGameElement::INCREASE, ele[2]); vecEleSignN.push_back(gameEle); } } return true; }
void Enum_All_File_in_Drectory(void *cmd) { Json::FastWriter fast_writer; Json::Value dir; Json::Value file; string Base64Result; cmdinfo *pci=(cmdinfo*)cmd; CHAR szFilePath[MAX_PATH]; CHAR szFullPath[MAX_PATH]; WIN32_FIND_DATA FindFileData; HANDLE hListFile; string szPath; string utf8_FullPath,utf8_FileName; //file["title"]=Json::Value("Dir1"); //file["path"]=Json::Value("C:\\Dir1"); //file["icon"]=Json::Value("dir.png"); //file["name"]=Json::Value("Dir1"); UTF8ToANSI((char *)pci->cmd_content.c_str(),&szPath); if (szPath.c_str()[szPath.length()-1]!='\\') { sprintf_s(szFilePath,"%s\\*",szPath.c_str()); } else { sprintf_s(szFilePath,"%s*",szPath.c_str()); } hListFile = FindFirstFile(szFilePath,&FindFileData); if(hListFile==INVALID_HANDLE_VALUE) { Msg("开始遍历文件时出错:%d\r\n",GetLastError()); } else { do { if(lstrcmp(FindFileData.cFileName,TEXT("."))==0|| lstrcmp(FindFileData.cFileName,TEXT(".."))==0) { continue; } if (szPath.c_str()[szPath.length()-1]!='\\') { sprintf_s(szFullPath,"%s\\%s",szPath.c_str(),FindFileData.cFileName); } else { sprintf_s(szFullPath,"%s%s",szPath.c_str(),FindFileData.cFileName); } dwTotalFileNum++; utf8_FileName.erase(); ANSIToUTF8(FindFileData.cFileName,&utf8_FileName); file["title"]=Json::Value(utf8_FileName.c_str()); file["name"]=Json::Value(utf8_FileName.c_str()); if(FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) { // Msg("<DIR>"); file["icon"]=Json::Value("grid_dir.png"); } else { file["icon"]=Json::Value("file.png"); } dir["dir"].append(file); } while(FindNextFile(hListFile, &FindFileData)); FindClose(hListFile); } szPath.erase(); utf8_FileName.erase(); Base64Encode(reinterpret_cast<const unsigned char*>((char *)fast_writer.write(dir).c_str()),fast_writer.write(dir).length(),Base64Result); Replace_plus(Base64Result); SubmitResult(pci,Base64Result.c_str()); Base64Result.erase(); dir.clear(); }