lugre_shell.cpp

Go to the documentation of this file.
00001 #include "lugre_prefix.h"
00002 #include <time.h>
00003 #include <string.h>
00004 #include <stdlib.h>
00005 #include <math.h> // myround
00006 #include <ctype.h> // tolower
00007 #include "lugre_shell.h"
00008 #include "lugre_input.h"
00009 #include <vector>
00010 #include <string>
00011 
00012 #ifdef WIN32
00013 // TODO : #elseif MAC or something like that
00014 // TODO : #elseif LINUX or something like that
00015     #include <windows.h>
00016     #include <direct.h> // mkdir, rmdir
00017 #else
00018     // this is the linux code
00019     #include <dirent.h>
00020     #include <sys/time.h>
00021     #include <sys/stat.h> // mkdir?
00022     #include <unistd.h> // mkdir?
00023 #endif
00024 
00025 
00026 namespace Lugre {
00027 
00028 // ****** ****** ****** badly portable lowlevel functions
00032 int mystricmp (const char *str1, const char *str2) {
00033     for (;;++str1,++str2) {
00034         if (*str1 == 0 && *str2 == 0) return 0;
00035         if (*str1 == 0 || *str2 == 0) return *str1 - *str2;
00036         int diff = tolower(*str1) - tolower(*str2);
00037         if (diff == 0) continue;
00038         return diff;
00039     }
00040     return 0;
00041 }
00042 
00043 float myround (const float x) { return floor(x+0.5); }
00044 
00045 // ****** ****** ****** cShell
00046 
00047 int rob_mkdir           (const char* path,int perm) {
00048     // found on http://www.devx.com/cplus/10MinuteSolution/26748/1954   // perm ignored for win
00049     //~ printf("rob_mkdir(%s,0x%x)\n",path,perm);
00050     #ifdef WIN32
00051     return _mkdir(path); // http://msdn.microsoft.com/en-us/library/2fkk4dzw.aspx, currently (15.01.2010) only needed for iris install using home path
00052     #else
00053     return mkdir(path,(mode_t)perm);
00054     #endif
00055     return -2;
00056 }
00057 
00058 int rob_rmdir           (const char* path) {
00059     #ifdef WIN32
00060     return _rmdir(path); // http://msdn.microsoft.com/en-us/library/2fkk4dzw.aspx, currently (15.01.2010) only needed for iris install using home path
00061     #else
00062     return rmdir(path);
00063     #endif
00064     return -2;
00065 }
00066 
00069 void    rob_dirlist         (const char* path,std::vector<std::string>& res,const bool bDirs,const bool bFiles) {
00070     #ifdef WIN32
00071         // WARNING ! this win part is not tested
00072         std::string pattern = std::string(path) + std::string("/*"); // warning !  / might be wrong, we should detect the slashtype from path
00073         WIN32_FIND_DATA finddata;
00074         HANDLE search = FindFirstFile(pattern.c_str(),&finddata);
00075         if (search == INVALID_HANDLE_VALUE) return;
00076 
00077         do {
00078             bool bIsDir = (finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
00079             if ((bIsDir && bDirs) || (!bIsDir && bFiles))
00080                 res.push_back(std::string(finddata.cFileName));
00081         } while (FindNextFile(search,&finddata)) ;
00082     
00083     // TODO : #elseif MAC or something like that
00084     // TODO : #elseif LINUX or something like that
00085     #else
00086         // this is the linux code
00087         std::string sPath = (*path) ? path : ".";
00088         DIR *d = opendir(sPath.c_str());
00089         if (!d) return;
00090         struct dirent *e;
00091         e = readdir(d);
00092         while (e != NULL) {
00093             std::string subname(e->d_name);
00094             std::string path = sPath + "/" + subname + "/.";
00095             bool bIsDir = opendir(path.c_str()) != 0;
00096             //printf("bIsDir(%s)=%d\n",subname.c_str(),bIsDir?1:0);
00097             if ((bDirs && bIsDir) || (bFiles && !bIsDir)) res.push_back(subname);
00098             e = readdir(d);
00099         }
00100         closedir(d); 
00101     #endif
00102 }
00103 
00104 
00105 // ****** ****** ****** cShell
00106 
00107 long    gStartTicks = 0;
00108 bool    cShell::mbAlive = false;
00109 cShell::cShell() : miArgC(0), mpszArgV(0) {}
00110 
00111 void    cShell::Init    (const int iArgC, char **pszArgV) { PROFILE
00112     gStartTicks = cShell::GetTicks();
00113         
00114     mbAlive = true;
00115     miArgC = iArgC;
00116     mpszArgV = pszArgV;
00117 
00118     // init random
00119     srand(time(NULL));
00120 }
00121 
00122 void    cShell::DeInit      () { }
00123 
00124 long    cShell::GetTicks    () {
00125     #ifdef WIN32
00126         return GetTickCount() - gStartTicks;
00127     #else
00128         static struct timeval now;
00129         gettimeofday(&now, NULL);
00130         return ((long)(now.tv_sec)*1000 + (long)(now.tv_usec)/1000) - gStartTicks;
00131     #endif
00132 }
00133 
00134 };

Generated on Wed May 23 06:00:15 2012 for cpp by  doxygen 1.5.6