compositionMap parseCompString(const std::string& ss, const std::vector<std::string>& names) { compositionMap x; for (size_t k = 0; k < names.size(); k++) { x[names[k]] = 0.0; } size_t start = 0; size_t stop = 0; while (stop < ss.size()) { size_t colon = ss.find(':', start); if (colon == npos) { break; } size_t valstart = ss.find_first_not_of(" \t\n", colon+1); stop = ss.find_first_of(", ;\n\t", valstart); std::string name = stripws(ss.substr(start, colon-start)); if (!names.empty() && x.find(name) == x.end()) { throw CanteraError("parseCompString", "unknown species '" + name + "'"); } x[name] = fpValueCheck(ss.substr(valstart, stop-colon-1)); start = ss.find_first_not_of(", ;\n\t", stop+1); } if (stop != npos && !stripws(ss.substr(stop)).empty()) { throw CanteraError("parseCompString", "Found non-key:value data " "in composition string: '" + ss.substr(stop) + "'"); } return x; }
void Phase::addElement(const XML_Node& e) { warn_deprecated("Phase::addElement(XML_Node&)", "To be removed after Cantera 2.2."); doublereal weight = 0.0; if (e.hasAttrib("atomicWt")) { weight = fpValue(stripws(e["atomicWt"])); } int anum = 0; if (e.hasAttrib("atomicNumber")) { anum = atoi(stripws(e["atomicNumber"]).c_str()); } string symbol = e["name"]; doublereal entropy298 = ENTROPY298_UNKNOWN; if (e.hasChild("entropy298")) { XML_Node& e298Node = e.child("entropy298"); if (e298Node.hasAttrib("value")) { entropy298 = fpValueCheck(stripws(e298Node["value"])); } } if (weight != 0.0) { addElement(symbol, weight, anum, entropy298); } else { addElement(symbol); } }
int main() { int sck = socket ( AF_INET, SOCK_DGRAM, 0 ); struct sockaddr_in srv_addr; srv_addr.sin_family = AF_INET; if (!inet_aton("192.168.7.13", &srv_addr.sin_addr)) { printf("FAIL"); } //srv_addr.sin_family = AF_INET; srv_addr.sin_port = htons(5887); //srv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); char * nck = NULL; int a = 0; size_t sz = 0; printf("Enter your nickname :\n"); getline(&nck, &sz, stdin); stripws(nck); if (strlen(nck) >= 30 || strlen(nck) <= 0) { printf("Too long\n"); close(sck); exit(-1); } while (1) { char* nstr = NULL; size_t ssz = 0; getline(&nstr, &ssz, stdin); stripws(nstr); char msgbuff[256]; msgbuff[0] = 0; int l = 0; if (strcmp(nstr, "shut") == 0) { msgbuff[0] = 3; l = 10; } else if (strcmp(nstr, "close") == 0) { break; } else { msgbuff[0] = 1; strcpy(msgbuff+1, nck); strcpy(msgbuff+40, nstr); l = 40 + 1 + strlen(nstr); } sendto(sck, msgbuff, l, 0, (struct sockaddr*) &srv_addr, sizeof(struct sockaddr)); if (msgbuff[0] == 3) break; } printf("Connection closed \n"); close(sck); }
/*! \fn swText::op_bg( std::string::const_iterator& ) */ bool swText::op_bg( std::string::const_iterator& _e) { swTAttr::Color C(0,0); if( _clear) { C.fg = attr.FgColor(); C.bg = _defaultAttributes.BgColor(); attr << C; _clear = false; return true; } ++_e; int v; stripws(_e); // gDebug << *_e; if( (v=cnum( *_e )) < 0 ){ Dbg << "Illegal value:" << *_e << " <=> " << v ; DEND; return false; } int fg = attr.FgColor(); C.bg = v; C.fg = fg; attr << C; // Dbg << " Bg set to " << v; DEND; _clear = false; return true; }
static const char * staticCharset(int i) { static QCString localcharset; switch ( i ) { case 0: return "UTF-8"; case 1: return "ISO-10646-UCS-2"; case 2: return ""; // in the 3rd place - some Xdnd targets might only look at 3 case 3: if ( localcharset.isNull() ) { QTextCodec *localCodec = QTextCodec::codecForLocale(); if ( localCodec ) { localcharset = localCodec->name(); localcharset = localcharset.lower(); stripws(localcharset); } else { localcharset = ""; } } return localcharset; } return 0; }
/* * The composition is a double. * Example * * Input is * * "fire:0 ice:1 snow:2" * * Output is * x["fire"] = 0 * x["ice"] = 1 * x["snow"] = 2 * * @param ss original string consisting of multiple key:composition * pairs on multiple lines * @param x Output map consisting of a composition * map, which is a string to double map */ void parseCompString(const std::string &ss, Cantera::compositionMap& x) { std::string s = ss; std::string::size_type icolon, ibegin, iend; std::string name, num, nm; do { ibegin = s.find_first_not_of(", ;\n\t"); if (ibegin != std::string::npos) { s = s.substr(ibegin,s.size()); icolon = s.find(':'); iend = s.find_first_of(", ;\n\t"); //icomma = s.find(','); if (icolon != std::string::npos) { name = s.substr(0, icolon); if (iend != std::string::npos) { num = s.substr(icolon+1, iend-icolon); s = s.substr(iend+1, s.size()); } else { num = s.substr(icolon+1, s.size()); s = ""; } nm = stripws(name); if (x.find(nm) == x.end()) { throw CanteraError("parseCompString", "unknown species " + nm); } x[nm] = atof(num.c_str()); } else s = ""; } } while (s != ""); }
static bool ParsePass( TechniquePass &pass, PHYSFS_File *f, const char *passName ) { pass.pass = interpretPassName( passName ); char line[512]; while ( PHYSFS_gets( line, sizeof(line), f ) ) { char *tok = stripws( line ); if ( tok == NULL ) { continue; } char str[256]; if ( sscanf( tok, "blendSrc %s", str ) == 1 ) { pass.blendSrc = interpretBlend( str ); } else if ( sscanf( tok, "blendDst %s", str ) == 1 ) { pass.blendDst = interpretBlend( str ); } else if ( sscanf( tok, "shader %s", str ) == 1 ) { pass.shader = shaderManager()->Load( str ); } else { tok = strtok( tok, " \t\n\r" ); if ( tok && _stricmp( tok, "endpass" ) == 0 ) { return true; } } } return false; }
//====================================================================================================================== std::string parseSpeciesName(const std::string& nameStr, std::string& phaseName) { std::string s = stripws(nameStr); std::string::size_type ibegin, iend, icolon; phaseName = ""; ibegin = s.find_first_not_of(" ;\n\t"); if (ibegin != std::string::npos) { s = s.substr(ibegin,s.size()); icolon = s.find(':'); iend = s.find_first_of(" ;\n\t"); if (icolon != std::string::npos) { phaseName = s.substr(0, icolon); s = s.substr(icolon+1, s.size()); icolon = s.find(':'); if (icolon != std::string::npos) { throw CanteraError("parseSpeciesName()", "two colons in name: " + nameStr); } } if (iend != std::string::npos) { throw CanteraError("parseSpeciesName()", "Species name has \" ;/\n/\t\" in the middle of it: " + nameStr); } } return s; }
/* * parse_args Parse the arguments from the command line into the fields * structure. Inputs: argc, argv Output: the table of name/value pairs for the * object being created is filled in with values from the arg list. Returns 0 * Success 1 if error For every name/value pair in the arg list, search for * the name in the field_table (all possible argument names) and fill the * value in the table. Note: An error message is printed for every argument * that is not in the fields table. All arguments are parsed. */ int parse_args( int argc, char **argv, int index, struct object_field *tbl) { int i, n, err = 0; char *cur, *name, *value; int name_len; memset(parse_errstr, 0, sizeof(parse_errstr)); // for every argument in the list, update the table with the new value // first arg is program name and second is object type. The remainder are // object fields for (i = index; i < argc; i++) { cur = argv[i]; name = cur; value = strchr(cur, '='); if (value == NULL) { if (err != 0) // not first error, add comma strcat(parse_errstr, ", "); strcat(parse_errstr, cur); err = 1; } else { // find the right place in the table to put // the value name_len = value - name; if ((n = fieldInTable(name, name_len, tbl)) >= 0) tbl[n].value = stripws(++value); else { if (err != 0) // not first error, add comma strcat(parse_errstr, ", "); strcat(parse_errstr, name); err = 1; } } } // done #ifdef DEBUG if (err == 0) print_table(tbl); #endif return err; }
/* * @todo call addUniqueElement(symbol, weight) instead of * addElement. */ void Elements:: addUniqueElement(const XML_Node& e) { doublereal weight = 0.0; if (e.hasAttrib("atomicWt")) weight = atof(stripws(e["atomicWt"]).c_str()); int anum = 0; if (e.hasAttrib("atomicNumber")) anum = atoi(stripws(e["atomicNumber"]).c_str()); string symbol = e["name"]; doublereal entropy298 = ENTROPY298_UNKNOWN; if (e.hasChild("entropy298")) { XML_Node& e298Node = e.child("entropy298"); if (e298Node.hasAttrib("value")) { entropy298 = atofCheck(stripws(e298Node["value"]).c_str()); } } if (weight != 0.0) { addUniqueElement(symbol, weight, anum, entropy298); } else { addUniqueElement(symbol); } }
/*! * Use the environment variable PYTHON_CMD if it is set. If not, return * the string 'python'. * * Note, there are hidden problems here that really direct us to use * a full pathname for the location of python. Basically the system * call will use the shell /bin/sh, in order to launch python. * This default shell may not be the shell that the user is employing. * Therefore, the default path to python may be different during * a system call than during the default user shell environment. * This is quite a headache. The answer is to always set the * PYTHON_CMD environmental variable in the user environment to * an absolute path to locate the python executable. Then this * issue goes away. */ static string pypath() { string s = "python"; const char* py = getenv("PYTHON_CMD"); if (py) { string sp = stripws(string(py)); if (sp.size() > 0) { s = sp; } } return s; }
/*! * Use the environment variable PYTHON_CMD if it is set. If not, return * the string 'python'. * * Note, there are hidden problems here that really direct us to use * a full pathname for the location of python. Basically the system * call will use the shell /bin/sh, in order to launch python. * This default shell may not be the shell that the user is employing. * Therefore, the default path to python may be different during * a system call than during the default user shell environment. * This is quite a headache. The answer is to always set the * PYTHON_CMD environmental variable in the user environment to * an absolute path to locate the python executable. Then this * issue goes away. */ static string pypath() { string s = "python"; const char* py = getenv("PYTHON_CMD"); if (!py) { const char* hm = getenv("HOME"); string home = stripws(string(hm)); string cmd = string(". ")+home +string("/setup_cantera &> /dev/null"); system(cmd.c_str()); py = getenv("PYTHON_CMD"); } if (py) { string sp = stripws(string(py)); if (sp.size() > 0) { s = sp; } } //else { // throw CanteraError("ct2ctml", // "set environment variable PYTHON_CMD"); //} return s; }
static QTextCodec* findcharset(const QCString& mimetype) { int i=mimetype.find("charset="); if ( i >= 0 ) { QCString cs = mimetype.mid(i+8); stripws(cs); i = cs.find(';'); if ( i >= 0 ) cs = cs.left(i); // May return 0 if unknown charset return QTextCodec::codecForName(cs,cs.length()*3/4); } // no charset=, use locale return QTextCodec::codecForLocale(); }
/*! \fn swText::op_fg( std::string::const_iterator& ) */ bool swText::op_fg( std::string::const_iterator& _e) { //Debug; swTAttr::Color C(0,0); ++_e; int v; stripws(_e); if( (v=cnum( *_e )) < 0 ) return false; int bg = attr.BgColor(); //Dbg << "CUrrent bg=" << bg;DEND; C.fg = v; //Dbg "Expected fg: " << v; C.bg = bg; attr << C; _clear = false; return true; }
//======================================================================================================================== doublereal fpValueCheck(const std::string& val) { std::string str = stripws(val); if (str.empty()) { throw CanteraError("fpValueCheck", "string has zero length"); } int numDot = 0; int numExp = 0; char ch; int istart = 0; ch = str[0]; if (ch == '+' || ch == '-') { istart = 1; } for (size_t i = istart; i < str.size(); i++) { ch = str[i]; if (isdigit(ch)) { } else if (ch == '.') { numDot++; if (numDot > 1) { throw CanteraError("fpValueCheck", "string has more than one ."); } if (numExp > 0) { throw CanteraError("fpValueCheck", "string has decimal point in exponent"); } } else if (ch == 'e' || ch == 'E' || ch == 'd' || ch == 'D') { numExp++; str[i] = 'E'; if (numExp > 1) { throw CanteraError("fpValueCheck", "string has more than one exp char"); } ch = str[i+1]; if (ch == '+' || ch == '-') { i++; } } else { throw CanteraError("fpValueCheck", "Trouble processing string, " + str); } } return fpValue(str); }
void ct2ctml(const char* file, const int debug) { #ifdef HAS_NO_PYTHON /* * Section to bomb out if python is not * present in the computation environment. */ string ppath = file; throw CanteraError("ct2ctml", "python cti to ctml conversion requested for file, " + ppath + ", but not available in this computational environment"); #endif string python_output; int python_exit_code; try { exec_stream_t python; python.set_wait_timeout(exec_stream_t::s_all, 1800000); // 30 minutes python.start(pypath(), "-i"); stringstream output_stream; python.in() << "if True:\n" << // Use this so that the rest is a single block " import sys\n" << " sys.stderr = sys.stdout\n" << " try:\n" << " from cantera import ctml_writer\n" << " except ImportError:\n" << " import ctml_writer\n" << " ctml_writer.convert(r'" << file << "')\n" << " sys.exit(0)\n\n" "sys.exit(7)\n"; python.close_in(); std::string line; while (python.out().good()) { std::getline(python.out(), line); output_stream << line << std::endl;; } python.close(); python_exit_code = python.exit_code(); python_output = stripws(output_stream.str()); } catch (std::exception& err) { // Report failure to execute Python stringstream message; message << "Error executing python while converting input file:\n"; message << "Python command was: '" << pypath() << "'\n"; message << err.what() << std::endl; throw CanteraError("ct2ctml", message.str()); } if (python_exit_code != 0) { // Report a failure in the conversion process stringstream message; message << "Error converting input file \"" << file << "\" to CTML.\n"; message << "Python command was: '" << pypath() << "'\n"; message << "The exit code was: " << python_exit_code << "\n"; if (python_output.size() > 0) { message << "-------------- start of converter log --------------\n"; message << python_output << std::endl; message << "--------------- end of converter log ---------------"; } else { message << "The command did not produce any output." << endl; } throw CanteraError("ct2ctml", message.str()); } if (python_output.size() > 0) { // Warn if there was any output from the conversion process stringstream message; message << "Warning: Unexpected output from CTI converter\n"; message << "-------------- start of converter log --------------\n"; message << python_output << std::endl; message << "--------------- end of converter log ---------------\n"; writelog(message.str()); } }
/* * parse_config Parse the config file into the fields structure. Inputs: * configfile Output: the table of name/value pairs for the object being * created is filled in with values from the arg list. Returns 0 Success 1 if * error For every name/value pair in the config file, search for the name in * * the field_table (all possible argument names) and fill the value in the * table. */ int parse_config( char *configfile, struct object_field *tbl) { int n, err = 0; char *name, *value; int name_len; FILE *fp; char *buf; int flen; struct stat stbuf; memset(parse_errstr, 0, sizeof(parse_errstr)); if (stat(configfile, &stbuf) != 0) { fprintf(stderr, "Error getting file size %s\n", configfile); return 1; } flen = stbuf.st_size; if ((buf = calloc(flen, sizeof(char))) == NULL) { fprintf(stderr, "Memory Error\n"); return 1; } // Open the config file fp = fopen(configfile, "r"); if (fp == NULL) { fprintf(stderr, "Error Opening Config File %s\n", configfile); return 1; } // read each line and process while (fgets(buf, flen, fp) != NULL) { name = buf; while (isspace((int)(unsigned char)*name)) name++; if ((strncmp(name, ";", 1) == 0) || strlen(buf) <= 0) continue; value = strchr(buf, '='); if ((value == NULL) || (strlen(value + 1) <= 0)) { fprintf(stderr, "Warning: blank value (line: %s)\n", buf); continue; } name_len = value - name; if ((n = fieldInTable(name, name_len, tbl)) >= 0) tbl[n].value = stripQuotes(stripws(++value)); else { if (err != 0) // not first error, add comma strcat(parse_errstr, ", "); strcat(parse_errstr, name); err = 1; } } return err; }
static doublereal fpValue(std::string val) { return atof(stripws(val).c_str()); }
int intValue(const std::string& val) { return std::atoi(stripws(val).c_str()); }
doublereal fpValueCheck(std::string val) { return atofCheck(stripws(val).c_str()); }
void ck2cti(const std::string& in_file, const std::string& thermo_file, const std::string& transport_file, const std::string& id_tag) { #ifdef HAS_NO_PYTHON /* * Section to bomb out if python is not * present in the computation environment. */ string ppath = in_file; throw CanteraError("ct2ctml", "python ck to cti conversion requested for file, " + ppath + ", but not available in this computational environment"); #endif string python_output; int python_exit_code; try { exec_stream_t python; python.set_wait_timeout(exec_stream_t::s_all, 1800000); // 30 minutes python.start(pypath(), "-i"); stringstream output_stream; ostream& pyin = python.in(); pyin << "if True:\n" << // Use this so that the rest is a single block " import sys\n" << " sys.stderr = sys.stdout\n" << " try:\n" << " from cantera import ck2cti\n" << // Cython module " except ImportError:\n" << " import ck2cti\n" << // legacy Python module " ck2cti.Parser().convertMech(r'" << in_file << "',"; if (thermo_file != "" && thermo_file != "-") { pyin << " thermoFile=r'" << thermo_file << "',"; } if (transport_file != "" && transport_file != "-") { pyin << " transportFile=r'" << transport_file << "',"; } pyin << " phaseName='" << id_tag << "',"; pyin << " permissive=True,"; pyin << " quiet=True)\n"; pyin << " sys.exit(0)\n\n"; pyin << "sys.exit(7)\n"; python.close_in(); std::string line; while (python.out().good()) { std::getline(python.out(), line); output_stream << line << std::endl;; } python.close(); python_exit_code = python.exit_code(); python_output = stripws(output_stream.str()); } catch (std::exception& err) { // Report failure to execute Python stringstream message; message << "Error executing python while converting input file:\n"; message << "Python command was: '" << pypath() << "'\n"; message << err.what() << std::endl; throw CanteraError("ct2ctml", message.str()); } if (python_exit_code != 0) { // Report a failure in the conversion process stringstream message; message << "Error converting input file \"" << in_file << "\" to CTI.\n"; message << "Python command was: '" << pypath() << "'\n"; message << "The exit code was: " << python_exit_code << "\n"; if (python_output.size() > 0) { message << "-------------- start of converter log --------------\n"; message << python_output << std::endl; message << "--------------- end of converter log ---------------"; } else { message << "The command did not produce any output." << endl; } throw CanteraError("ck2cti", message.str()); } if (python_output.size() > 0) { // Warn if there was any output from the conversion process stringstream message; message << "Warning: Unexpected output from CTI converter\n"; message << "-------------- start of converter log --------------\n"; message << python_output << std::endl; message << "--------------- end of converter log ---------------\n"; writelog(message.str()); } }