//****************************************************************************** //** SCATMECH: Polarized Light Scattering C++ Class Library //** //** File: askuser.cpp //** //** Thomas A. Germer //** Optical Technology Division, National Institute of Standards and Technology //** 100 Bureau Dr. Stop 8443; Gaithersburg, MD 20899-8443 //** Phone: (301) 975-2876; FAX: (301) 975-6991 //** Email: thomas.germer@nist.gov //** //** Version: 6.00 (February 2008) //** //****************************************************************************** #include #include #include #include #include #include #include "scatmech.h" #include "optconst.h" #include "askuser.h" #include "dielfunc.h" using namespace std; namespace SCATMECH { string Get_SCATMECH_Version() { return std::string("SCATMECH 6.01 (Build: ") + std::string(__DATE__) + std::string(")"); } optical_constant default_substrate(4.15,0.05); optical_constant default_particle(1.59,0); double default_lambda=0.532; optical_constant vacuum(1.,0.); dielectric_function vacuum_function(vacuum); istream_with_comments SCATMECH_input(cin.rdbuf()); std::ostream SCATMECH_output(cerr.rdbuf()); class SCATMECH_output_starter { public: SCATMECH_output_starter() { SCATMECH_output.setf(ios::unitbuf); } } _SCATMECH_output_starter; char SCATMECH_comment=';'; stack instack; // Environment variables can be set... char* SCATMECH_DIRECTORY=0; // setenv SCATMECH_DIRECTORY void check_SCATMECH_DIRECTORY() { if (!SCATMECH_DIRECTORY) { SCATMECH_DIRECTORY = getenv("SCATMECH"); } } string find_file(const string& name) { check_SCATMECH_DIRECTORY(); string pname = (SCATMECH_DIRECTORY!=NULL) ? SCATMECH_DIRECTORY : ""; ifstream file(name.c_str()); if (!file) { int begin=0; while (beginsbumpc()!='\n'); } void streambuf_with_comments:: pushfile() { std::string filename; while (!isspace(istr.top()->sgetc())) filename += istr.top()->sbumpc(); std::filebuf *fb=new std::filebuf; istr.push(fb); std::filebuf *r = fb->open(filename.c_str(),std::ios::in); if (r==NULL) { delete fb; istr.pop(); throw SCATMECH_exception("Could not open file:" + filename); } } void streambuf_with_comments:: popfile() { if (istr.size()>1) { delete istr.top(); istr.pop(); } } int streambuf_with_comments:: underflow() { int c = istr.top()->sbumpc(); if (c==EOF && istr.size()>1) { popfile(); return underflow(); } if (c==';') { clear_comment(); c='\n'; } else if (c=='<') { pushfile(); return underflow(); } else if (c=='>') { popfile(); return underflow(); } istr.top()->sputbackc(c); return c; } int streambuf_with_comments:: uflow() { int c = istr.top()->sbumpc(); if (c==EOF && istr.size()>1) popfile(); if (c==';') { clear_comment(); return '\n'; } if (c=='<') { pushfile(); c = istr.top()->sbumpc(); } if (c=='>') { popfile(); c = istr.top()->sbumpc(); } return c; } int streambuf_with_comments:: pbackfail(char c) { return (c!=EOF) ? istr.top()->sputbackc(c) : EOF; } streambuf_with_comments:: streambuf_with_comments(std::streambuf *s) { istr.push(s); setp(0, 0); setg(0,0,0); } streambuf_with_comments& streambuf_with_comments:: operator=(std::streambuf *s) { unwind(); istr.pop(); istr.push(s); setp(0, 0); setg(0,0,0); return *this; } streambuf_with_comments:: ~streambuf_with_comments() { while (istr.size()>1) { delete istr.top(); istr.pop(); } } void streambuf_with_comments:: unwind() { while (istr.size()>1) { delete istr.top(); istr.pop(); } } string istream_with_comments:: getstrline() { char c; string result; while ((c=get())!='\n') { if (eof()) return result; result += c; } return result; } string istream_with_comments:: getquoted() { const char quote='\"'; string result; ws(*this); if (peek()==quote) { get(); char c; while ((c=get())!=quote&&!fail()) result += c; if (fail()) throw SCATMECH_exception("Failed to find closing quotation mark"); } else *this >> result; return result; } string get_token(istream &is,const char* delim) { string result; do { char c = is.peek(); for (const char* p=delim;*p;p++) if (c==*p) return result; result += is.get(); if (is.fail()) return result; } while (1); } } // namespace SCATMECH