• EOS 修改文件名称与文件夹名称


      最近有一个需求,需要修改EOS名称,将所有文件里面的EOS改为UOS,文件夹名称也需要修改,然后重新构建项目,于是写了一个小程序进行修改。如果有相同项目类似的修改,可以在下面这个程序稍做修改就可以了。

    由于时间限制,没有进一步完善,以后有时间再修改一下成为工具。

      EOS version: v1.2.5

      VS version: 2017

      运行环境: win10

      编写代码如下:

      

      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即可编译成功。

  • 相关阅读:
    pythonday06数据类型(四)
    pythonday05数据类型(三)
    pythonday04数据类型(二)
    pythonday03数据类型(一)
    Apollo自动驾驶实践——第0讲:导论
    图论学习:生成树的Matrix-tree定理
    2020杭电多校6 Expectation
    2020牛客暑期多校第九场 B Groundhog and Apple Tree
    图论:二分图最大权匹配KM算法
    第十章 百度Apollo实战
  • 原文地址:https://www.cnblogs.com/hbright/p/9721805.html
Copyright © 2020-2023  润新知