最近有一個需求,須要修改EOS名稱,將全部文件裏面的EOS改成UOS,文件夾名稱也須要修改,而後從新構建項目,因而寫了一個小程序進行修改。若是有相同項目相似的修改,能夠在下面這個程序稍作修改就能夠了。ios
因爲時間限制,沒有進一步完善,之後有時間再修改一下成爲工具。小程序
EOS version: v1.2.5app
VS version: 2017工具
運行環境: win10ui
編寫代碼以下:spa
1 #include<iostream> 2 #include<boost/filesystem.hpp> 3 #include<boost/filesystem/path.hpp> 4 #include<boost/filesystem/operations.hpp> 5 #include<boost/program_options.hpp> 6 #include<cstring> 7 #include<vector> 8 #include<boost/thread.hpp> 9 #include<boost/container/flat_map.hpp> 10 #include<algorithm> 11 12 13 namespace bpo = boost::program_options; 14 namespace bfs = boost::filesystem; 15 16 17 enum enFileType{ 18 ENU_FILE = 1, 19 ENU_DIR, 20 }; 21 22 const uint32_t max_char_line = 65535; 23 uint8_t g_lineBuf[max_char_line]; 24 25 typedef std::set<bfs::path> SetPath; 26 27 const std::string oldfield = "eos"; 28 const std::string newfield = "uos"; 29 30 const std::string oldfieldupper = "EOS"; 31 const std::string newfieldupper = "UOS"; 32 33 34 bool scanFilesUseRecursive(const std::string& rootPath, SetPath& fileSet); 35 bfs::path modifyName( bfs::path path); 36 bool modifyContent(bfs::path path); 37 char* modify(char* pBuf, uint64_t fsize); 38 void printPath(const SetPath& fileSet); 39 40 int main(int argc, char* argv[]) { 41 42 std::string filePath; 43 44 bpo::options_description opts("options"); 45 opts.add_options() 46 ("help", "help info") 47 ("dir", bpo::value<std::string>(), "the directory of need to parse"); 48 49 bpo::variables_map vm; 50 try { 51 bpo::store(parse_command_line(argc, argv, opts), vm); 52 } 53 catch (bpo::error_with_no_option_name &ex) { 54 std::cout << ex.what() << std::endl; 55 } 56 57 bpo::notify(vm); 58 59 if (vm.count("help")) 60 { 61 std::cout << opts << std::endl; 62 } 63 64 if (vm.count("dir")) 65 { 66 filePath = vm["dir"].as<std::string>(); 67 } 68 69 std::string root("\\eos"); 70 71 bfs::path curPath = bfs::current_path(); 72 std::cout << curPath << std::endl; 73 74 curPath += root; 75 76 std::cout << curPath << std::endl; 77 78 SetPath fileSet; 79 80 scanFilesUseRecursive(curPath.string(), fileSet); 81 //printPath(fileSet); 82 std::cout << fileSet.size() << std::endl; 83 84 try 85 { 86 for (SetPath::reverse_iterator iter = fileSet.rbegin(); iter != fileSet.rend(); ++iter) 87 { 88 modifyContent(*iter); 89 } 90 } 91 catch (const std::exception& e) 92 { 93 std::cout << std::string("modifyContent exception: ") + e.what() << std::endl; 94 } 95 96 try 97 { 98 for (SetPath::reverse_iterator iter = fileSet.rbegin(); iter != fileSet.rend(); ++iter) 99 { 100 modifyName(*iter); 101 } 102 } 103 catch (const std::exception& e) 104 { 105 std::cout << std::string("modifyName exception: ") + e.what() << std::endl; 106 } 107 108 system("pause"); 109 110 return 0; 111 } 112 113 114 bool scanFilesUseRecursive(const std::string& rootPath, SetPath& fileSet) { 115 116 bfs::path fullpath(rootPath, bfs::native); 117 118 if (!bfs::exists(fullpath)) 119 { 120 std::cout << std::string("scanFilesUseRecursive : file not exist! ") << rootPath.c_str() << std::endl; 121 return false; 122 } 123 124 bfs::recursive_directory_iterator end_iter; 125 126 try { 127 for (bfs::recursive_directory_iterator iter(fullpath); iter != end_iter; iter++) { 128 fileSet.insert(iter->path()); 129 } 130 } 131 catch(bfs::filesystem_error& e) 132 { 133 std::cout << e.what() << std::endl; 134 } 135 136 return true; 137 } 138 139 bfs::path modifyName(const bfs::path path) 140 { 141 std::string name = path.filename().string(); 142 143 size_t pos = name.find(oldfield); 144 if (pos != std::string::npos) 145 { 146 name[pos] = 'u'; 147 } 148 149 pos = name.find(oldfieldupper); 150 if (pos != std::string::npos) 151 { 152 name[pos] = 'U'; 153 } 154 155 bfs::path newpath = path.parent_path() / name; 156 157 bfs::rename(path, newpath); 158 159 return newpath; 160 } 161 162 void printPath(const SetPath& fileSet) 163 { 164 for (SetPath::iterator iter = fileSet.begin(); iter != fileSet.end(); ++iter) 165 { 166 std::cout << iter->string().c_str() << std::endl; 167 } 168 } 169 170 bool modifyContent(bfs::path path) { 171 172 if (bfs::is_directory(path)) { 173 return true; 174 } 175 176 std::fstream file; 177 uint64_t fsize; 178 char* pBuf = NULL; 179 try 180 { 181 fsize = bfs::file_size(path); 182 if (fsize == 0) 183 { 184 return true; 185 } 186 187 file.open(path.string().c_str(), std::ios::in | std::ios::out | std::ios::binary); 188 if (!file.is_open()) 189 { 190 std::cout << "modifyContent() open file failed! path: " << path.string().data() << std::endl; 191 return false; 192 } 193 194 char* pBuf = new char[fsize]; 195 memset(pBuf, 0, fsize); 196 file.read(pBuf, fsize); 197 char* pHead = modify(pBuf, fsize); 198 199 file.seekg(std::ios::beg); 200 file.write(pHead, fsize); 201 202 file.close(); 203 delete pBuf; 204 pBuf = NULL; 205 } 206 catch (const std::exception&) 207 { 208 delete pBuf; 209 file.close(); 210 std::cout << "modifyContent() exception! path: " << path.string().data() << std::endl; 211 } 212 213 return true; 214 } 215 216 char* modify(char* pBuf, uint64_t fsize) 217 { 218 char* ret = pBuf; 219 uint64_t nCount = 0; 220 221 while (nCount < fsize) 222 { 223 if (memcmp(pBuf, oldfield.c_str(), oldfield.length()) == 0) 224 { 225 *pBuf = 'u'; 226 } 227 228 if (memcmp(pBuf, oldfieldupper.c_str(), oldfieldupper.length()) == 0) 229 { 230 *pBuf = 'U'; 231 } 232 233 ++nCount; 234 ++pBuf; 235 } 236 237 return ret; 238 }
把執行文件與目錄放在同一級就能夠了,運行可能須要2分鐘左右,修改完成後,從新編譯EOS,會有一個報錯,在UOS/libraries/appbase文件下,註釋掉version.cmake.in便可編譯成功。code