fileUtils.cpp

00001 /*
00002   ファイル操作
00003   Satofumi KAMIMURA
00004   $Id$
00005 */
00006 
00007 #include "fileUtils.h"
00008 #include "detect_os.h"
00009 #if defined Linux || defined Cygwin
00010 #include <unistd.h>
00011 #else
00012 #include <windows.h>
00013 #include <direct.h>
00014 #include <io.h>
00015 #ifndef F_OK
00016 #define F_OK 06
00017 #endif
00018 #ifndef W_OK
00019 #define W_OK 06
00020 #endif
00021 #endif
00022 #include <fcntl.h>
00023 #include <stdio.h>
00024 #include <sys/stat.h>
00025 #include <sys/types.h>
00026 #include <fstream>
00027 
00028 
00029 std::string VXV::searchFile(const char* file, const char* path[]) {
00030 
00031   for (const char** p = path; *p != NULL; ++p) {
00032     std::string full_path = std::string(*p);
00033     if ((!full_path.empty() && full_path[full_path.size()-1] != '/') &&
00034         (file[0] != '/')) {
00035       full_path += "/";
00036     }
00037     full_path += file;
00038 
00039     if (access(full_path.c_str(), F_OK) == 0) {
00040       return full_path;
00041     }
00042   }
00043   return "";
00044 }
00045 
00046 
00047 void VXV::createArgs(std::vector<char*>& args, const char* file) {
00048   std::ifstream fin(file);
00049 
00050   std::string line;
00051   while (!fin.eof()) {
00052     getline(fin, line);
00053 
00054     // 空白の除去
00055     int index = 0;
00056     do {
00057       index = static_cast<int>(line.find_first_of(" \t\r\n", index));
00058       if (index >= 0) {
00059         line.erase(index, 1);
00060       }
00061     } while (index >= 0);
00062 
00063     // コメントの除去
00064     int last = static_cast<int>(line.find("#"));
00065     if (last >= 0) {
00066       line.erase(last);
00067     }
00068 
00069     // 行の内容を args に追加
00070     if (!line.empty()) {
00071       char* buffer = new char [line.size() +1];
00072       sprintf(buffer, "%s", line.c_str());
00073       args.push_back(buffer);
00074     }
00075   }
00076   fin.close();
00077 }
00078 
00079 
00080 void VXV::deleteArgs(std::vector<char*>& args) {
00081   for (std::vector<char*>::iterator it = args.begin();
00082        it != args.end(); ++it) {
00083     delete [] *it;
00084   }
00085 }
00086 
00087 
00088 int VXV::createDirs(std::string dir_path) {
00089   if (!access(dir_path.c_str(), W_OK)) {
00090     return 0;
00091   }
00092   createDirs(dir_path.substr(0, dir_path.find_last_of('/')));
00093 
00094   // ディレクトリの作成
00095 #if defined Linux || defined Cygwin
00096   return mkdir(dir_path.c_str(), 0755);
00097 #else
00098   return _mkdir(dir_path.c_str());
00099 #endif
00100 }
00101 

Generated on Mon Apr 13 22:52:05 2009 by  doxygen 1.5.7.1