platInd.cpp

00001 
00008 #include <fstream>
00009 #include "options.h"
00010 #include<string>
00011 
00012 #include "opencv/cv.h"
00013 #include "opencv/cxcore.h"
00014 #include "opencv/highgui.h"
00015 
00016 #include "platInd/platInd.hpp"
00017 
00018 
00019 #ifdef _WIN32
00020         #include<direct.h>
00021         #include<windows.h>
00022 #elif defined __linux__
00023         #include<sys/stat.h>
00024         #include<sys/types.h>
00025         #include<dirent.h>
00026 #else
00027         #include<direct.h>
00028 #endif
00029 
00030 using namespace std;
00031 
00032 //windows return -1 if directory exists, return 0 if created successfully
00033 //same behavior on linux as well
00034 int PlatInd::makedir(string path)
00035 {
00036         int ret;
00037         #ifdef _WIN32
00038                 ret=_mkdir(path.c_str());
00039         #elif defined __linux__
00040                 ret=mkdir(path.c_str(),0777);
00041         #else
00042                 ret=_mkdir(path.c_str());
00043         #endif
00044 
00045                 return ret;
00046 }
00047 
00048 //return 1 if file exist
00049 //return 0 if file not exist
00050 
00051 bool PlatInd::fexist(string path)
00052 {
00053         ifstream ifile(path.c_str());
00054         if(ifile)
00055         {
00056                 ifile.close();
00057                 return true;
00058         }
00059         else
00060         {
00061                 return false;
00062         }
00063         
00064 }
00065 
00066 //list all directories inside a directory
00067 int PlatInd::getDirList(string dir, std::vector<string> &files)
00068 {
00069 #ifdef __linux__
00070         return PlatInd::getdirlinux(dir,files);
00071 #endif
00072 
00073 #ifdef _WIN32
00074         return PlatInd::getdirWindows(dir,files);
00075 #endif
00076 
00077 }
00078 
00079 
00080 #ifdef __linux__
00081 int PlatInd::getdirlinux(string dir, std::vector<string> &files)
00082 {
00083         DIR *dp;
00084         struct dirent *dirp;
00085         if((dp  = opendir(dir.c_str())) == NULL) 
00086         {
00087                 //cout << "Error(" << errno << ") opening " << dir << endl;
00088                 return -1;
00089         }
00090 
00091         while ((dirp = readdir(dp)) != NULL) 
00092         {
00093                 if(dirp->d_type==DT_DIR)  //only look for directories
00094                 {
00095                         if(strcmp(dirp->d_name, ".") != 0 && strcmp(dirp->d_name, "..") != 0)
00096                         files.push_back(string(dirp->d_name));
00097                 }
00098                 
00099         }
00100         closedir(dp);
00101         return 0;
00102 }
00103 #endif
00104 
00105 #ifdef _WIN32
00106 string wchar_t2string(const wchar_t *wchar)
00107 {
00108         string str = "";
00109         int index = 0;
00110         while(wchar[index] != 0)
00111         {
00112                 str += (char)wchar[index];
00113                 ++index;
00114         }
00115         return str;
00116 }
00117 
00118 void string2wchar_t(const string &str,wchar_t *wchar)
00119 {
00120         
00121         int index = 0;
00122         while(index < str.size())
00123         {
00124                 wchar[index] = (wchar_t)str[index];
00125                 ++index;
00126         }
00127         wchar[index] = 0;       
00128 }
00129 
00130 int PlatInd::getdirWindows(string directoryName, std::vector<string> &listFileNames)
00131 {
00132         wchar_t FileName[260];
00133         WIN32_FIND_DATA FindFileData;
00134         directoryName=directoryName+"/*.*";
00135         string2wchar_t(directoryName,FileName);
00136         HANDLE hFind = FindFirstFile(FileName, &FindFileData);
00137 
00138         if(!hFind)
00139         {
00140                 return -1;
00141         }
00142 
00143         if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
00144         {
00145                 string temp=wchar_t2string(FindFileData.cFileName);
00146                 if(strcmp(temp.c_str(), ".") != 0 && strcmp(temp.c_str(), "..") != 0)
00147                 {
00148                         listFileNames.push_back(wchar_t2string(FindFileData.cFileName));
00149                 }
00150 
00151         }
00152         
00153 
00154         while (FindNextFile(hFind, &FindFileData))
00155         {
00156                 if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
00157                 {
00158                         string temp=wchar_t2string(FindFileData.cFileName);
00159                         if(strcmp(temp.c_str(), ".") != 0 && strcmp(temp.c_str(), "..") != 0)
00160                         {
00161                                 listFileNames.push_back(wchar_t2string(FindFileData.cFileName));
00162                         }
00163                 }
00164                 
00165         }
00166 
00167         return 0;
00168 }
00169 #endif
00170 
00171 
00172 

Generated on 18 Dec 2014 for libdt-OpenCV by  doxygen 1.6.1