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

ChString.cxx

Go to the documentation of this file.
00001 
00002 // $Id: ChString.cxx,v 1.4 2009/09/01 20:10:53 fmwk Exp $
00003 //
00005 //
00006 // messier@indiana.edu
00008 #include "XMLInterface/ChString.h"
00009 #include <iostream>
00010 #include <cstdio>
00011 extern "C" {
00012 #include <wctype.h>
00013 }
00014 
00015 //......................................................................
00016 
00017 int xmli::ChString::Split(const char* line, 
00018                         const char* tok, 
00019                         std::vector<std::string>& fields) 
00020 {
00021 //======================================================================
00022 // Split the text line "line" into sub-strings deliminated by the
00023 // character tokens listed in the string "tok". Returns the number
00024 // of fields added to the vector "fields"
00025 //======================================================================
00026   bool        inDQuotedStr = false;
00027   bool        inSQuotedStr = false;
00028   bool        specialChar  = false;
00029   const char* c            = line;
00030   int         nfields      = 0;
00031   std::string f;
00032 
00033   for (; *c!='\0'; ++c) {
00034     int         istoken = 0;
00035     const char* t;
00036 
00037     // If last character was '\' interpret this character literally
00038     if (specialChar)  { f+= *c; specialChar = false; continue; }
00039 
00040     // If this character is a '\' raise the special character flag
00041     if (*c == '\\') { specialChar = true; continue; }
00042     
00043     // Determine if we're entering or leaving a quoted string
00044     if (*c == '\"') { inDQuotedStr = !inDQuotedStr; continue; }
00045     if (*c == '\'') { inSQuotedStr = !inSQuotedStr; continue; }
00046     
00047     // In quoted string just copy everything until the close quotes
00048     if (inDQuotedStr)  { f+= *c; continue; }    
00049     if (inSQuotedStr)  { f+= *c; continue; }    
00050     
00051     // Determine if this character is one of the deliminators
00052     for (t=tok; *t!='\0'; ++t) if (*t==*c) ++istoken;
00053 
00054     // Handle non-token charaters
00055     if (istoken == 0) { f+= *c; continue; }
00056 
00057     // Handle token charaters
00058     if (istoken>0) {
00059       if (f!="") { // Don't allow empty strings
00060         fields.push_back(f);
00061         ++nfields;
00062         f = "";
00063       }
00064     }
00065   }
00066   // Take care of the last one
00067   if (f!="") {
00068     fields.push_back(f);
00069     ++nfields;
00070     f = "";
00071   }
00072   if (inSQuotedStr || inDQuotedStr) {
00073     std::cerr << "Unterminated string: '"<<line<<"'"<<std::endl;
00074     abort();
00075   }
00076   return nfields;
00077 }
00078 
00079 //......................................................................
00080 
00081 bool xmli::ChString::IsTrue(const std::string& s) 
00082 {
00083 //======================================================================
00084 // Is the string s a representation of "true"
00085 //======================================================================
00086   if (s == "1")    return true;
00087   if (s == "t")    return true;
00088   if (s == "true") return true;
00089   if (s == "T")    return true;
00090   if (s == "True") return true;
00091   if (s == "TRUE") return true;
00092   if (s == "on")   return true;
00093   if (s == "On")   return true;
00094   if (s == "ON")   return true;
00095   if (s == "y")    return true;
00096   if (s == "Y")    return true;
00097   if (s == "yes")  return true;
00098   if (s == "Yes")  return true;
00099   if (s == "YES")  return true;
00100   return false;
00101 }
00102 
00103 //......................................................................
00104 
00105 bool xmli::ChString::IsFalse(const std::string& s) 
00106 {
00107 //======================================================================
00108 // Is the string s a representation of "false"
00109 //======================================================================
00110   if (s == "0")     return true;
00111   if (s == "f")     return true;
00112   if (s == "false") return true;
00113   if (s == "F")     return true;
00114   if (s == "False") return true;
00115   if (s == "FALSE") return true;
00116   if (s == "off")   return true;
00117   if (s == "Off")   return true;
00118   if (s == "OFF")   return true;
00119   if (s == "n")     return true;  
00120   if (s == "N")     return true;  
00121   if (s == "no")    return true;
00122   if (s == "No")    return true;
00123   if (s == "NO")    return true;
00124   return false;
00125 }
00126 
00127 //......................................................................
00128 
00129 void xmli::ChString::Trim(std::string& s)
00130 {
00131 //======================================================================
00132 // Remove extra white space from the string s
00133 //======================================================================
00134   const char* s1 = s.c_str();
00135   const char* s2 = s1 + s.length() - 1;  
00136   while ( iswspace(*s1) ) ++s1;
00137   while ( iswspace(*s2) ) --s2;
00138   const char* si;
00139   std::string stmp;
00140   for (si=s1; si<=s2; ++si) {
00141     if (iswspace(*si) && !iswspace(*(si-1))) stmp += " ";
00142     else                                     stmp += (*si);
00143   }
00144   s = stmp;
00145 }
00146 
00147 //......................................................................
00148 
00149 time_t xmli::ChString::GetTime(std::string& s)
00150 {
00151 //======================================================================
00152 // Extract a time stamp from the string
00153 //======================================================================
00154 
00155   int year, month, day, hour, min, sec;
00156   bool isGood = true;
00157 
00158   if (sscanf(s.c_str(),"%d-%d-%d %d-%d-%d",
00159              &year, &month, &day, &hour, &min, &sec) == 6) {}
00160   else {
00161     if (sscanf(s.c_str(),"%d/%d/%d %d-%d-%d",
00162                &year, &month, &day, &hour, &min, &sec) == 6) {}
00163     else {
00164       if (sscanf(s.c_str(),"%d/%d/%d %d:%d:%d",
00165                  &year, &month, &day, &hour, &min, &sec) == 6) {}
00166       else {
00167         if (sscanf(s.c_str(),"%d-%d-%d %d:%d:%d",
00168                    &year, &month, &day, &hour, &min, &sec) == 6) {}
00169         else {
00170           if (sscanf(s.c_str(),"%d-%d-%d ",
00171                      &year, &month, &day) == 3) 
00172             {
00173               hour = 0;
00174               min = 0;
00175               sec = 0;
00176             }
00177           else {
00178             std::cerr << "Invalid TimeStamp!  Setting time to _now_" 
00179                       << std::endl;
00180             isGood = false;
00181           }
00182         }
00183       }
00184     }
00185   }
00186   
00187   struct tm ts;
00188   ts.tm_sec   = sec;
00189   ts.tm_min   = min;
00190   ts.tm_hour  = hour;
00191   ts.tm_mday  = day;
00192   ts.tm_mon   = month-1;
00193   ts.tm_year  = year-1900;
00194   time_t ttime = mktime(&ts);
00195   
00196   if (!isGood) ttime = time(NULL);
00197   
00198   return ttime;
00199 
00200 }
00201 

Generated on Mon Nov 23 04:45:24 2009 for NOvA Offline by  doxygen 1.3.9.1