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

convert.cpp

Go to the documentation of this file.
00001 /*
00002  * ******** fete: From ENDF To ENDL *********
00003  * 
00004  * Copyright (c) 2006, The Regents of the University of California. 
00005  * All rights reserved.
00006  * 
00007  * Produced at the Lawrence Livermore National Laboratory. 
00008  * Written by David A. Brown, Gerry Hedstrom, Tony Hill
00009  * 
00010  * This file is part of fete v1.0  (UCRL-CODE-218718)
00011  * 
00012  * Please read the COPYING file for "Our Notice and GNU General 
00013  * Public License" in the root of this software distribution.  
00014  * 
00015  * This program is free software; you can redistribute it and/or modify 
00016  * it under the terms of the GNU General Public License (as published by 
00017  * the Free Software Foundation) version 2, dated June 1991. 
00018  * 
00019  * This program is distributed in the hope that it will be useful, 
00020  * but WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF 
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms 
00022  * and conditions of the GNU General Public License for more details. 
00023  * 
00024  * You should have received a copy of the GNU General Public License along 
00025  * with this program; if not, write to the Free Software Foundation, Inc., 
00026  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
00027  * 
00028  * $Revision: 1781 $
00029  * $Date: 2006-03-22 09:55:41 -0800 (Wed, 22 Mar 2006) $
00030  * $Author: dbrown $
00031  * $Id: convert.cpp 1781 2006-03-22 17:55:41Z dbrown $
00032  * 
00033  * ******** fete: From ENDF To ENDL *********
00034  */
00035 
00036 // function methods for conversion functions
00037 
00038 #include "convert.hpp"
00039 #include "messaging.hpp"
00040 
00041 double stod( string instring )
00042 { 
00043   // First remove all the blanks from the string
00044   int nspace=instring.find(' ');
00045   while ( nspace < instring.size() )
00046     {
00047       instring.erase(nspace,1);
00048       nspace=instring.find(' ');
00049     }
00050 
00051   // Remove a possible single "e" from "properly" formatted numbers
00052   // so that they can be handled just like those that aren't
00053   instring=tolower(instring);
00054   int espace=instring.find('e');
00055   if ( espace >= 0 )
00056     {  
00057       //Need to make sure the next character is a "+" or "-",
00058       //if not, insert a "+" sign (i.e. 1.03e12 -> 1.03e+12)
00059       if ( instring.at(espace+1) != '+' &&
00060        instring.at(espace+1) != '-' ) 
00061     instring.insert(espace+1,"+");
00062 
00063       instring.erase(espace,1);
00064     }
00065 
00066   // Must have only acceptable characters at this point
00067   if ( instring.find_first_not_of("0123456789+-.") != string::npos )
00068     {
00069       SevereError("stod","Unknown character in '"+instring+"'");
00070     }
00071 
00072   //Strip off leading + or -
00073   double fsign=1.0;  //either +1 or -1, depending on sign
00074   int lpsign = instring.find_first_of("+"); //Look for + sign
00075   if ( lpsign == 0 )
00076     {
00077       instring.erase(lpsign,1);
00078     }
00079   int lmsign = instring.find_first_of("-"); //Look for leading - sign
00080   if ( lmsign == 0 )
00081     {
00082       fsign=-1.0;
00083       instring.erase(lmsign,1);
00084     }
00085 
00086   // Find the first and last occurrence of signs
00087   int fpm = instring.find_first_of("+-");
00088   int lpm = instring.find_last_of("+-");
00089   if ( fpm !=  lpm )  //Should be the same
00090     {
00091       SevereError("stod","Too many signs!");
00092     }
00093 
00094   // The first and last occurrence of a decimal point should be the same
00095   // If not, we need to exit.  Program continues if no decimal is encountered.
00096   int fdp = instring.find_first_of(".");
00097   int ldp = instring.find_last_of(".");
00098   if ( fdp !=  ldp ) //Should be the same
00099     {
00100       SevereError("stod","Too many decimals!");
00101     }
00102 
00103   if ( fpm < 0 ) // There is no exponent
00104     {
00105       return fsign*atof(instring.c_str());
00106     }
00107   else if ( fpm == instring.size()-1 ) //Cannot end with a sign
00108     {
00109       SevereError("stod","Number cannot end with a sign!");
00110     }
00111   else if ( fdp > fpm )  //Cannot have decimal in exponent
00112     {
00113       SevereError("stod","No decimals allowed in exponent "+instring);
00114     }
00115   else
00116     {
00117       instring.insert(fpm,"e");      //Insert an "e" for easy conversion
00118       return fsign*atof(instring.c_str());
00119     }
00120   return 0.0; // should never get here
00121 }
00122 
00123 float stof( string instring )
00124 { 
00125 
00126   return static_cast<float>( stod( instring ) );
00127 
00128 }
00129 
00130 int stoi( string instring )
00131 { 
00132   
00133   return static_cast<int>( stod( instring ) );
00134 }
00135 
00136 //!Removes the leading blanks in a string.
00137 string remove_leading_blanks( string instring )
00138 {
00139   if( instring.size() <= 0 ) return( instring );
00140 
00141   int nspace=instring.find(' ');
00142   while ( nspace == 0 )
00143     {
00144       instring.erase(nspace,1);
00145       nspace=instring.find(' ');
00146     }
00147   return( instring );
00148 }
00149 
00150 //!Removes the trailing blanks in a string.
00151 string remove_trailing_blanks( string instring )
00152 {
00153   if ( instring.size() <= 0 ) return( instring );  //If there's nothing here, get out
00154 
00155   int nspace=instring.rfind(' ');
00156   while ( nspace == instring.size()-1 )
00157     {
00158       instring.erase(instring.size()-1,1);
00159       nspace=instring.rfind(' ');
00160     }
00161   return( instring );
00162 }
00163 
00164 //!Removes all extra blanks from a string.
00165 string remove_extra_blanks( string instring )
00166 { 
00167   if( instring.size() == 0 ) return( instring );
00168 
00169   instring = remove_leading_blanks( instring );
00170   instring = remove_trailing_blanks( instring );
00171 
00172   if( instring.size() == 0 ) return( instring );
00173 
00174   int x = instring.find("  "); //Find a double space
00175   while( x < instring.size() && x >= 0 )
00176     {
00177       instring.erase(x,1);
00178       x = instring.find("  ");
00179     }
00180   return( instring );
00181 }
00182 
00183 //! Removes all blanks from a string.
00184 string remove_all_blanks( string instring )
00185 {
00186   if( instring.size() <= 0 ) return( instring );
00187 
00188   int nspace=instring.find(' ');
00189   while ( nspace < instring.size() )
00190     {
00191       instring.erase(nspace,1);
00192       nspace=instring.find(' ');
00193     }
00194   return( instring );
00195 }
00196 
00197 //! splits a string at all occurances of "pattern" and returns 
00198 //! the substrings in a list 
00199 vector< string > split(const string& s, const string pattern)
00200 {
00201     vector< string > result;
00202     int begin_slice = 0;
00203     int end_slice = 0;
00204     int slice_length = 0;
00205     while ( s.find( pattern, begin_slice ) != string::npos )
00206     {
00207         end_slice = s.find( pattern, begin_slice )+1;
00208         slice_length = end_slice - begin_slice - 1;
00209         if ( slice_length > 0 )
00210             result.push_back( s.substr( begin_slice, slice_length ) );
00211         begin_slice = end_slice;
00212     }
00213     result.push_back( s.substr( begin_slice ) );
00214     return result;
00215 }
00216 
00217 //! joins a list of strings using "pattern" as glue
00218 string join(const vector< string >& ls, const string pattern)
00219 {
00220     string result="";
00221     for ( vector<string>::const_iterator itr = ls.begin(); itr != ls.end(); ++itr )
00222     {
00223         vector<string>::const_iterator next_itr = itr;
00224         ++next_itr;
00225         result += *itr;
00226         if ( next_itr != ls.end() ) result += pattern;
00227     }
00228     return result;
00229 }
00230 
00231 //! A function that lower-cases strings
00232 string tolower(const string& s)
00233 {
00234     string t = s;
00235     int length = t.length();
00236     for (int i = 0; i < length; i++) t[i] = tolower(t[i]);
00237     return t;
00238 }
00239 
00240 //! i copies of a string x pasted together
00241 string operator*(int i, const string x){
00242     string xTimes("");
00243     for (int j=0;j<i;++j) xTimes+=x;
00244     return xTimes;
00245 }
00246 
00247 //! center a string in a field of a certain length
00248 string center(const string stuff, int len){
00249     int numSpaces = len-stuff.size();
00250     numSpaces/=2;
00251     string aSpace(" ");
00252     string output(numSpaces*aSpace+stuff+numSpaces*aSpace);
00253     if (output.size()==len) return output; else return output+" ";
00254 }
00255 
00256 //! left justify a string in a field of a certain length
00257 string ljust(const string stuff, int len){
00258     int numSpaces = len-stuff.size();
00259     string aSpace(" ");
00260     return stuff+numSpaces*aSpace;
00261 }
00262 
00263 //! right justify a string in a field of a certain length
00264 string rjust(string stuff,int len){
00265     int numSpaces = len-stuff.size();
00266     string aSpace(" ");
00267     return numSpaces*aSpace+stuff;
00268 }
00269 
00270 
00271 

Generated on Thu Sep 7 10:30:02 2006 for fete -- From ENDFB6 To ENDL by doxygen 1.3.4