Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

vuCommandLineTool.cpp

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <fstream.h>
00003 #include "vuCommandLineTool.h"
00004 
00005 vuCommandLineTool::vuCommandLineTool()
00006 {
00007   m_argc = 0;
00008   m_argv = NULL;
00009 }
00010 
00011 vuCommandLineTool::vuCommandLineTool(int argc, const char ** argv)
00012 {
00013   m_argc = argc;
00014   m_argv = NULL;
00015   if (m_argc > 0) {
00016     m_argv = new vuString[m_argc];
00017     for (int i=0; i<m_argc; i++) {
00018       m_argv[i] = argv[i];
00019     }
00020   }
00021 }
00022 
00023 vuCommandLineTool::vuCommandLineTool(const vuCommandLineTool &other)
00024 {
00025   throw "vuCommandLineTool::copyConstructor is not implemented";
00026 }
00027 
00028 vuCommandLineTool::~vuCommandLineTool()
00029 {
00030   if (m_argv != NULL) {
00031     delete [] m_argv;
00032     m_argv = NULL;
00033   }
00034 }
00035 
00036 bool vuCommandLineTool::hasParameter(const char *param)
00037 {
00038   return (indexForParamter(vuString(param)) >= 0);
00039 }
00040 
00041 vuString vuCommandLineTool::stringForParameter(const char *param)
00042 {
00043   vuString str(param);
00044 
00045   if (!str.hasPrefix("--")) return vuString("");
00046 
00047   int i = indexForParamter(param);
00048 
00049   if (i>0) {
00050     str = m_argv[i];
00051     dword i, cnt = str.getLength();
00052 
00053     for (i=2; (i<cnt); i++) {
00054       if (str[i] == '=') {
00055         i++;
00056         break;
00057       }
00058     }
00059     return str.substr(i,cnt);
00060   }
00061   return vuString("");
00062 }
00063 
00064 float vuCommandLineTool::floatForParameter(const char *param)
00065 {
00066   vuString str    = stringForParameter(vuString(param));
00067   float    result = 0.0;
00068 
00069   sscanf(str.c_str(), "%f", &result);
00070 
00071   return result;
00072 }
00073 
00074 int vuCommandLineTool::intForParameter(const char *param)
00075 {
00076   vuString str    = stringForParameter(vuString(param));
00077   int      result = 0;
00078 
00079   sscanf(str.c_str(), "%d", &result);
00080 
00081   return result;
00082 }
00083 
00084 
00085 word vuCommandLineTool::numberOfNonParameters()
00086 {
00087   bool isValid;
00088   return numberOfNonParameters(isValid);
00089 }
00090 
00091 word vuCommandLineTool::numberOfNonParameters(bool &isValid)
00092 {
00093   word result  = 0;
00094   bool isFound = false; // is first '-' found?
00095 
00096   isValid = true;
00097   for (word i=m_argc-1; i>0; i--) {
00098     if (!isFound && !m_argv[i].hasPrefix("-")) {
00099       result++;
00100       continue;
00101     }
00102     isFound = true;
00103     if (!m_argv[i].hasPrefix("-")) {
00104       isValid = false;
00105       break;
00106     }
00107   }
00108   return result;
00109 }
00110 
00111 vuString vuCommandLineTool::getNonParameter(const dword idx)
00112 {
00113     dword nums = numberOfNonParameters();
00114 
00115     return getArgument(m_argc-nums+idx);
00116 }
00117 
00118 
00119 vuString vuCommandLineTool::getArgument(word idx)
00120 {
00121   if (idx >= m_argc) idx = m_argc -1;
00122   return m_argv[idx];
00123 }
00124 
00125 
00126 bool vuCommandLineTool::fileExists(const vuString &fileName)
00127 {
00128   bool     result;
00129   ifstream ifp;
00130 
00131 #ifdef IS_NOCREATE_NEEDED
00132   ifp.open(fileName.c_str(), ios::in|ios::binary|ios::nocreate);
00133 #else
00134   // The nocreate is not available on the IRIX boxes that we were compiling
00135   // this code on, so we had to remove this part from
00136   ifp.open(fileName, ios::in|ios::binary);
00137 #endif
00138   result = ifp.is_open();
00139   if (result) ifp.close();
00140   
00141   return result;
00142 }
00143 
00144 vuString vuCommandLineTool::toolName()
00145 {
00146   return (m_argc > 0) ? m_argv[0].getLastPathComponent() : vuString("toolname");
00147 }
00148 
00149 /* ------------------------------------------------------------------------- */
00150 /* --- private methods ----------------------------------------------------- */
00151 /* ------------------------------------------------------------------------- */
00152 
00153 // returns index of found param, otherwise -1
00154 int vuCommandLineTool::indexForParamter(const vuString &param)
00155 {
00156   vuString str = param;
00157   bool isLong = false; // true for "--"paramters
00158   if (str.isEmpty()) return -1;
00159   if (str.hasPrefix("--")) {
00160     isLong = true;
00161     str += "=";
00162   }
00163   for (int i=1; i<m_argc; i++) {
00164     if (m_argv[i] == param) return i;
00165     if (isLong && m_argv[i].hasPrefix(str)) return i;
00166   }
00167   return -1;
00168 }

Generated on Wed Dec 15 21:20:33 2004 for vuVolume by  doxygen 1.3.9.1