// // keyvalipv2.cc // // Copyright (C) 1996 Limit Point Systems, Inc. // // Author: Curtis Janssen // Maintainer: LPS // // This file is part of the SC Toolkit. // // The SC Toolkit is free software; you can redistribute it and/or modify // it under the terms of the GNU Library General Public License as published by // the Free Software Foundation; either version 2, or (at your option) // any later version. // // The SC Toolkit is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Library General Public License for more details. // // You should have received a copy of the GNU Library General Public License // along with the SC Toolkit; see the file COPYING.LIB. If not, write to // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. // // The U.S. Government is granted a limited license as per AL 91-7. // #include #include #ifdef HAVE_SSTREAM # include #else # include #endif #include #include #include #include #include #include #include #include using namespace std; using namespace sc; ParsedKeyVal::ParsedKeyVal(IPV2*i): nfile(0), file(0), nfp(0) { ipv2 = i; } ParsedKeyVal::ParsedKeyVal(): nfile(0), file(0), nfp(0) { ipv2 = new IPV2; } ParsedKeyVal::ParsedKeyVal(const char* name): nfile(0), file(0), nfp(0) { ipv2 = new IPV2; read(name); } ParsedKeyVal::ParsedKeyVal(istream& fp): nfile(0), file(0), nfp(0) { ipv2 = new IPV2; read(fp); } ParsedKeyVal::ParsedKeyVal(const char* keyprefix, const Ref& keyval): nfile(0), file(0), nfp(0) { ipv2 = new IPV2; char* filespec = new char[strlen(keyprefix)+6]; strcpy(filespec,keyprefix); strcat(filespec,"files"); char* dirspec = new char[strlen(keyprefix)+6]; strcpy(dirspec,keyprefix); strcat(dirspec,"dir"); char* directory = keyval->pcharvalue(dirspec); if (!directory) { directory = getenv("SCLIBDIR"); if (directory) { char *tmp = strchr(directory,'='); if (!tmp) tmp = directory; else tmp = &tmp[1]; directory = strcpy(new char[strlen(tmp)+1], tmp); } else { struct stat sb; const char *dir = INSTALLED_SCLIBDIR; #ifdef SRC_SCLIBDIR if (stat(dir, &sb) != 0) { ExEnv::out0() << indent << "WARNING: could not find " << dir << endl; dir = SRC_SCLIBDIR; } #endif directory = strcpy(new char[strlen(dir)+1], dir); } } int nfiles = keyval->count(filespec); for (int i=0; ipcharvalue(filespec,i); char* fullname; if (directory) { fullname = new char[strlen(directory)+strlen(filename)+1]; strcpy(fullname,directory); strcat(fullname,filename); } else { fullname = filename; } read(fullname); if (directory) { delete[] filename; } delete[] fullname; } if (directory) delete[] directory; delete[] dirspec; delete[] filespec; } void ParsedKeyVal::cat_files(const char* keyprefix, const Ref& keyval, ostream &ostr) { char* filespec = new char[strlen(keyprefix)+6]; strcpy(filespec,keyprefix); strcat(filespec,"files"); char* dirspec = new char[strlen(keyprefix)+6]; strcpy(dirspec,keyprefix); strcat(dirspec,"dir"); char* directory = keyval->pcharvalue(dirspec); if (!directory) { directory = getenv("SCLIBDIR"); if (directory) { char *tmp = strchr(directory,'='); if (!tmp) tmp = directory; else tmp = &tmp[1]; directory = strcpy(new char[strlen(tmp)+1], tmp); } else { struct stat sb; const char *dir = INSTALLED_SCLIBDIR; #ifdef SRC_SCLIBDIR if (stat(dir, &sb) != 0) { ExEnv::out0() << indent << "WARNING: could not find " << dir << endl; dir = SRC_SCLIBDIR; } #endif directory = strcpy(new char[strlen(dir)+1], dir); } } int nfiles = keyval->count(filespec); for (int i=0; ipcharvalue(filespec,i); char* fullname; if (directory) { fullname = new char[strlen(directory)+strlen(filename)+1]; strcpy(fullname,directory); strcat(fullname,filename); } else { fullname = filename; } ifstream is(fullname); is >> ostr.rdbuf(); if (directory) { delete[] filename; } delete[] fullname; } if (directory) delete[] directory; delete[] dirspec; delete[] filespec; } void ParsedKeyVal::read(const char* name) { ifstream infp(name,ios::in); if (infp.bad()) { ExEnv::errn() << "ParsedKeyVal couldn't open " << name << endl; exit(1); } int i; char**newfile = new char*[nfile+1]; for (i=0; iread(infp,ExEnv::errn(),""); } void ParsedKeyVal::parse_string(const char *str) { #ifdef HAVE_SSTREAM istringstream in(str); #else istrstream in(str); #endif ipv2->read(in,ExEnv::errn(),""); } ParsedKeyVal::~ParsedKeyVal() { delete ipv2; for (int i=0; ivalue_v((char *)key,&result,0,0))); if (error() != OK) { result = 0; } return result; } const char* ParsedKeyVal::classname(const char* key) { const char* result; seterror(maperr(ipv2->classname_v((char *)key,&result,0,0))); return result; } const char* ParsedKeyVal::truekeyword(const char*key) { const char* result; seterror(maperr(ipv2->truekeyword_v((char *)key,&result,0,0))); if (!result && error() == OK) return key; else return result; } void ParsedKeyVal::errortrace(ostream&fp) { fp << indent << "ParsedKeyVal: error: \"" << errormsg() << "\"" << endl; if (nfp) { fp << indent << " reading from " << nfp << " files with unknown names" << endl; } for (int i=0; iprint_tree(fp); } void ParsedKeyVal::print_unseen(ostream&fp) { ipv2->print_unseen(fp); } int ParsedKeyVal::have_unseen() { return ipv2->have_unseen(); } ///////////////////////////////////////////////////////////////////////////// // Local Variables: // mode: c++ // c-file-style: "CLJ" // End: