📄 gaparameter.cpp
字号:
case GAParameter::CHAR: cval = *((char*)(p[i]->value())); os << cval << "\n"; break; case GAParameter::STRING: sval = ((char*)(p[i]->value())); os << sval << "\n"; break; case GAParameter::FLOAT: fval = *((float*)(p[i]->value())); os << fval << "\n"; break; case GAParameter::DOUBLE: dval = *((double*)(p[i]->value())); os << dval << "\n"; break; case GAParameter::POINTER: default: os << "(pointer)\n";// os << p[i]->value() << "\n"; break; } } return 0;}intGAParameterList::write(const char* filename) const { STD_OFSTREAM outfile(filename, (STD_IOS_OUT | STD_IOS_TRUNC));// should be done this way, but SGI systems (and others?) don't do it right...// if(! outfile.is_open()){ if(outfile.fail()){ GAErr(GA_LOC, "GAParameterList", "write", gaErrWriteError, filename); return 1; } int status = write(outfile); outfile.close(); return status;}// Read name-value pairs from the stream. If the first item is a number, then// we expect that many name-value pairs. If not, then we read until the end of// the stream. Be sure not to duplicate the last one, and be sure to dump any// items from a # to end-of-line.// Parse for name-value pairs, where the pairs are separated by whitespace.// *Every* parameter must be specified as a name-value pair (no single name // with no value allowed). If we get a single name with no value that will// screw up the name-value pairing for any remaining pairs. We could check for// this situation, but for now we just let it happen and dump the errors. (i'll// do a better parsing algorithm at some later point).// We return the number of items that we were able to read from the stream.// If we come across an item that is unrecognized, we dump an error message.// The buffer length is limited here, so don't try to read in any really long// lines.// We parse only for items that we know about - if we come across a pair// whose name we do not know about, we ignore the pair and go on to the next.// There's no global list to tell us what type things are, and we don't assume.// We don't allow setting pointers using this method.intGAParameterList::read(STD_ISTREAM& is, GABoolean flag){ int nfound = 0; if(n == 0) return nfound; char buf[BUFSIZE]; char name[NAMESIZE]; int toggle = 0, count = 0, npairs = -1; is.width(sizeof(buf)); // don't allow to overrun buffer do{ is >> buf; if(npairs == -1){ if(IsNumeric(buf)){ npairs = atoi(buf); is >> buf; } else{ npairs = MAX_PAIRS; } } if(is.eof()){ break; } else if(buf[0] == '#'){ // dump to end-of-line is.get(buf, BUFSIZE); // fails on lines longer than BUFSIZE } else if(toggle == 0){ strcpy(name, buf); toggle = 1; } else if(toggle == 1){ int found = 0; count += 1; toggle = 0; for(unsigned int i=0; i<n && !found; i++){ if(strcmp(name, p[i]->fullname()) == 0 || strcmp(name, p[i]->shrtname()) == 0){ found = 1; int ival; float fval; double dval; switch(p[i]->type()){ case GAParameter::BOOLEAN: case GAParameter::INT: ival = atoi(buf); set(name, (void*)&ival); nfound += 1; break; case GAParameter::CHAR: case GAParameter::STRING: set(name, (void*)buf); nfound += 1; break; case GAParameter::FLOAT: fval = (float)atof(buf); set(name, (void*)&fval); nfound += 1; break; case GAParameter::DOUBLE: dval = (double)atof(buf); set(name, (void*)&dval); nfound += 1; break; case GAParameter::POINTER: default: break; }// Move this parameter to the front of the list// if(i > 0) {// GAParameter *tmpptr = p[i];// memmove(&(p[1]), &(p[0]), i * sizeof(GAParameter*));// p[0] = tmpptr;// } if(i < n-1) { GAParameter *tmpptr = p[i]; memmove(&(p[i]), &(p[i+1]), (n-i-1) * sizeof(GAParameter*)); p[n-1] = tmpptr; } } } if(!found && flag == gaTrue){ strcpy(_gaerrbuf1, ""); strcat(_gaerrbuf1, "unrecognized variable name '"); strcat(_gaerrbuf1, name); strcat(_gaerrbuf1, "'"); GAErr(GA_LOC, "GAParameterList", "read", _gaerrbuf1); } } } while(!is.eof() && count < npairs); if(toggle == 1){ strcpy(_gaerrbuf1, ""); strcat(_gaerrbuf1, "variable "); strcat(_gaerrbuf1, name); strcat(_gaerrbuf1, " has no value"); strcat(_gaerrbuf2, "be sure there is a newline at end of the file"); GAErr(GA_LOC, "GAParameterList", "read", _gaerrbuf1, _gaerrbuf2); is.clear(STD_IOS_BADBIT | is.rdstate()); } return nfound;}intGAParameterList::read(const char* filename, GABoolean flag){ STD_IFSTREAM infile(filename, STD_IOS_IN); if(!infile){ GAErr(GA_LOC, "GAParameterList", "read", gaErrReadError, filename); return 1; } int status = read(infile, flag); infile.close(); return status;}#endif// Parse the arglist for any recognized arguments. If we find a string we// know, then we set the value. If we find a string we don't know then we// don't do anything unless the flag is set. If the flag is set then we// complain about unknown arguments.// You should set up the list before you do the parsing in order to grab// arguments from the arglist.// When we encounter names we know with valid values, we put the list into// order as we get the new values. When we recognize a pair, we pull the// parameter from the list, set its value, then stick it at the end of the// parameter list. So if something turns up more than once, we'll remove-// then-append it more than once and the ordering from argv will be properly// maintained.// We assume that argv[0] is the name of the program, so we don't barf on // it if it is not a recognized name.intGAParameterList::parse(int& argc, char *argv[], GABoolean flag){ int nfound = 0; if(n == 0) return nfound; char **argvout = new char* [argc]; int argcu = argc-1; int argcl = 0; for(int i=0; i<argc; i++){ int found = 0;// Loop through all of the parameters to see if we got a match. If there is// no value for the name, complain. Otherwise, set the value. for(unsigned int j=0; j<n && found == 0; j++){ if(strcmp(p[j]->shrtname(), argv[i]) == 0 || strcmp(p[j]->fullname(), argv[i]) == 0){ found = 1; argvout[argcu] = argv[i]; argcu--; if(++i >= argc){ GAErr(GA_LOC, argv[0], argv[i-1], " needs a value"); } else{ int ival; float fval; double dval; switch(p[j]->type()){ case GAParameter::BOOLEAN: if(IsNumeric(argv[i])){ ival = atoi(argv[i]); ival = (ival == 0) ? 0 : 1; } else { if(strcmp(argv[i], "true") == 0 || strcmp(argv[i], "True") == 0 || strcmp(argv[i], "TRUE") == 0 || strcmp(argv[i], "t") == 0 || strcmp(argv[i], "T") == 0) ival = 1; } set(argv[i-1], (void*)&ival); nfound += 1; break; case GAParameter::INT: ival = atoi(argv[i]); set(argv[i-1], (void*)&ival); nfound += 1; break; case GAParameter::CHAR: case GAParameter::STRING: set(argv[i-1], (void*)argv[i]); nfound += 1; break; case GAParameter::FLOAT: fval = (float)atof(argv[i]); set(argv[i-1], (void*)&fval); nfound += 1; break; case GAParameter::DOUBLE: dval = (double)atof(argv[i]); set(argv[i-1], (void*)&dval); nfound += 1; break; case GAParameter::POINTER: default: break; }// Move this parameter to the front of the list// if(j > 0) {// GAParameter *tmpptr = p[j];// memmove(&(p[1]), &(p[0]), j * sizeof(GAParameter*));// p[0] = tmpptr;// } if(j < n-1) { GAParameter *tmpptr = p[j]; memmove(&(p[j]), &(p[j+1]), (n-j-1) * sizeof(GAParameter*)); p[n-1] = tmpptr; }// Now update the argv array and argc count to indicate we understood this one argvout[argcu] = argv[i]; argcu--; continue; } } } if(!found){ if(flag && i!=0){ _gaerrbuf1[0] = '\0'; strcat(_gaerrbuf1, "unrecognized name "); strcat(_gaerrbuf1, argv[i]); GAErr(GA_LOC, "GAParameterList", "parse", _gaerrbuf1); } argvout[argcl] = argv[i]; argcl++; } }// bcopy(argvout, argv, argc * sizeof(char*)); memcpy(argv, argvout, argc*sizeof(char*)); argc = argcl; delete [] argvout; return nfound;} static intIsNumeric(const char* str){ for(int i=strlen(str)-1; i>=0; i--) if(! isdigit(str[i]) && str[i] != '.') return 0; return 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -