📄 params.cc
字号:
//Copyright (c) 2004, Charles Killian, Adolfo Rodriguez, Dejan Kostic, Sooraj Bhat, and Amin Vahdat//All rights reserved.////Redistribution and use in source and binary forms, with or without//modification, are permitted provided that the following conditions are met://// * Redistributions of source code must retain the above copyright// notice, this list of conditions and the following disclaimer.// * Redistributions in binary form must reproduce the above copyright// notice, this list of conditions and the following disclaimer in// the documentation and/or other materials provided with the// distribution.// * Neither the names of Duke University nor The University of// California, San Diego, nor the names of its contributors// may be used to endorse or promote products derived from// this software without specific prior written permission.////THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"//AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE//IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE//DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE//FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL//DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER//CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,//OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE//USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.#include "params.h"#include <stdio.h>#include <string.h>#include <stdlib.h>int param::getint() { if(intvalue != NULL) { return *intvalue; } intvalue = new int; *intvalue = strtol(strvalue,NULL,10); return *intvalue;}double param::getdouble() { if(doublevalue != NULL) { return *doublevalue; } doublevalue = new double; *doublevalue = strtod(strvalue,NULL); return *doublevalue;}int param::print(FILE* ostream) { return fprintf(ostream,"REPLAY PARAMETER: %s = \"%s\"\n",key,strvalue);}param* params::find(char* key) { for(int i=0; i < cur_data; i++) { if(!strcmp(data[i].getkey(),key)) { return data+i; } } return NULL;}bool params::isset(char* key) { param* found = find(key); return found != NULL;}int params::getint(char* key) { param* found = find(key); return (found != NULL)?found->getint():0;}double params::getdouble(char* key) { param* found = find(key); return (found != NULL)?found->getdouble():0;}char* params::getstr(char* key) { param* found = find(key); return (found != NULL)?found->getstr():NULL;}char* params::set(char* key, char* value) { param* found = find(key); if(found) { return found->set(value); } else { if(cur_data == max_data) { param* olddata = data; max_data += 20; data = new param[max_data]; if(cur_data > 0) { memcpy(data,olddata,sizeof(param)*cur_data); } if(olddata != NULL) { delete[] olddata; } } data[cur_data] = param(key, value); cur_data++; return NULL; }}int params::loadparams(int argc, char** argv) { int startarg = 1; if(argc==1 || argv[1][0] == '-') {// printf("REPLAY PARAMETER FILE: params.default\n"); fflush(stdout); loadfile("params.default"); } else { startarg = 2;// printf("REPLAY PARAMETER FILE: %s\n",argv[1]); fflush(stdout); loadfile(argv[1]); } for(int i = startarg; i<argc; i++) { char* arg = *(argv+i); // printf("arg:%s\n",arg); if(arg[0] != '-' || strlen(arg) < 2) continue; arg++; if(arg[0] == '-') arg++;// printf("Node REPLAY_PARM: %s %s\n",argv[i],argv[i+1]); //NOTE: For now assuming no '=' if(i <= argc-1) { set(arg,argv[i+1]); } else { set(arg, ""); } }}int params::loadfile(char* filename) { FILE *infile = fopen(filename, "r"); if(!infile) { perror("loadfile file open failed:"); return 0; } char* buffer = new char[81]; char* key = new char[81]; char* value = new char[81]; char* temp; char* begincomment; char* beginquote; char* endquote; while(fgets(buffer,80,infile)) { if(sscanf(buffer," %[#]",key)) continue; if(sscanf(buffer," %[^=]",key) != 1) continue; temp = index(buffer, '=')+1; if(temp == NULL) continue; if(sscanf(temp," %[a-zA-Z._0-9-]",value) != 1) { if(sscanf(temp, " '%[^']'",value) != 1) { if(sscanf(temp, " \"%[^\"]\"",value) != 1) { continue; } } } set(key,value); key = new char[81]; value = new char[81]; } }int params::print(FILE* ostream) { int total=fprintf(ostream,"REPLAY PARAMETERS:\n"); for(int i=0; i<cur_data; i++) { total += data[i].print(ostream); } return total;}params::params() { data = NULL; cur_data = 0; max_data = 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -