fv::variable_value parse_variable_value(const std::string& arg) { std::vector<std::string> tokens; boost::iter_split(tokens, arg, boost::algorithm::first_finder(":")); if (tokens.size() != 2) { print_error_and_exit( oac::format("cannot parse variable value in '%s'", arg)); } auto& type = tokens[0]; auto& value = tokens[1]; std::transform(type.begin(), type.end(), type.begin(), std::tolower); std::transform(value.begin(), value.end(), value.begin(), std::tolower); if (type == "bool") return fv::variable_value::from_bool( value == "1" || value == "true" || value == "yes"); else if (type == "byte") return fv::variable_value::from_byte( boost::lexical_cast<std::uint8_t>(value)); else if (type == "word") return fv::variable_value::from_word( boost::lexical_cast<std::uint16_t>(value)); else if (type == "dword") return fv::variable_value::from_dword( boost::lexical_cast<std::uint32_t>(value)); else print_error_and_exit( oac::format("invalid variable value type '%s'", type)); exit(1); // never reached }
action parse_action(int argc, char* argv[]) { if (argc < 2) { print_error_and_exit("invalid argument count"); } std::string act(argv[1]); std::transform(act.begin(), act.end(), act.begin(), std::tolower); if (act == "watch") { if (argc < 3) print_error_and_exit("invalid argument count for 'watch' action"); return action::WATCH; } else if (act == "set") { if (argc < 4) print_error_and_exit("invalid argument count for 'set' action"); return action::SET; } else print_error_and_exit(oac::format("unknown action '%s'", act)); exit(1); // never reached }
Connection::Connection(char _time2die, int _is_beacon) { time2die = _time2die; is_beacon = _is_beacon; // Initialize mutex int state1 = pthread_mutex_init(&mutex, NULL); if(state1) { print_error_and_exit("Connection::Connection(): mutex init failed."); } int state2 = pthread_cond_init(&cv, NULL); if(state2) { print_error_and_exit("Connection::Connection(): condigion var init failed."); } }
/** * strdup() replacement */ char * parser_strdup(char * str) { parser_string_t * new_string = malloc(sizeof(parser_string_t)); if (new_string == NULL) print_error_and_exit("Out of memory!"); new_string->next = NULL; new_string->string = malloc(sizeof(char) * (strlen(str) + 1)); if (new_string->string == NULL) print_error_and_exit("Out of memory!"); strcpy(new_string->string, str); if (strings == NULL) { strings = new_string; } else { strings_end->next = new_string; } strings_end = new_string; return new_string->string; }
/** * Concatenate str1 and str2 */ char * parser_strcat(char * str1, char * str2) { char * new_string = malloc(sizeof(char) * (strlen(str1) + strlen(str2) + 1)); if (!new_string) print_error_and_exit("Out of memory!"); strcpy(new_string, str1); strcpy(new_string + strlen(str1), str2); parser_string_t * s1 = container_of(&str1, parser_string_t, string); free(s1->string); s1->string = new_string; return new_string; }
void create_new_file_from_template( generator const& gen, std::string const& class_name, std::string const& file_extension) { std::string template_file_name = gen.m_template_file + file_extension + ".template"; std::ifstream template_file(template_file_name); if(!template_file.is_open()) { print_error_and_exit("Error opening template file \"" + template_file_name + "\""); } // Read file into string std::string file_string( (std::istreambuf_iterator<char>(template_file)), std::istreambuf_iterator<char>()); // Search for each token through the entire file, replacing as found. for(auto token_value_pair : gen.m_tokens) { size_t remove_it = file_string.find(token_value_pair.first); while(remove_it != std::string::npos) { // Find end of token (we searched for the whole token including %'s thus it is guaranteed // that it exists) size_t remove_end_it = file_string.find('%', remove_it + 1); size_t token_length = (remove_end_it + 1) - remove_it; // Replace entire token from remove_it to and including remove_end. file_string.replace(remove_it, token_length, token_value_pair.second); remove_it = file_string.find(token_value_pair.first.c_str(), remove_it); } } std::string output_file_name = class_name + file_extension; std::ofstream output_file(output_file_name); if(!output_file.is_open()) { std::cerr << "Error opening ouput file \"" << output_file_name << "\"exiting..." << std::endl; std::exit(-1); } output_file << file_string; std::cout << output_file_name << " written..." << std::endl; }
void read_tokens_from_config_file(generator & gen) { std::string config_file_name("cppclassgen.cfg"); std::ifstream config_file(config_file_name); if(!config_file.is_open()) { print_error_and_exit("Error opening config file \"" + config_file_name + "\""); } std::string curr_line; while(std::getline(config_file, curr_line)) { std::stringstream curr_line_stream(curr_line); std::string curr_token, curr_replacement_value; curr_line_stream >> curr_token; curr_line_stream >> curr_replacement_value; // Ignore lines with < 2 entires (ex. final empty line) if(!curr_token.empty() && !curr_replacement_value.empty()) { gen.m_tokens.push_back(std::make_pair(curr_token, curr_replacement_value)); } } }
int init(char *filename) { FILE *fp; int read, length; char *line; char *temp; char *word1, *word2; char *att, *att_value; int count1 = 0; int count2 = 0; int len; int count; line = NULL; len = 0; fp = fopen(filename, "r"); if(!fp) { print_error_and_exit("File not exist."); } count = 0; while ((read = readline(fp, &line, &len)) != -1) { length = strlen(line); if(length == 0) continue; //line[length - 1] = '\0'; //printf("%s\n", line); if(line[0] == ';') continue; word1 = line; temp = line; word2 = NULL; while(*temp != '\0') { if(*temp == '=') { *temp = '\0'; word2 = temp + 1; break; } temp++; } if((strcmp(word1, "[init]")) == 0) { count1++; if(count2 == 1) count2++; continue; } if((strcmp(word1, "[beacons]")) == 0 ) { count2++; if(count1 == 1) count1++; continue; } att = rem_space(word1); att_value = rem_space(word2); ///printf("%sTTT%s\n", att, att_value); if(strlen(att) == 0) continue; if(count1 == 1) { if(( strcmp(att, "port")) == 0) { ini.port = atol(att_value); count++; } else if(( strcmp(att, "location")) == 0) { ini.location = atoll(att_value); count++; } else if(( strcmp(att, "homedir")) == 0) { ini.homedir = (char *)calloc(strlen(att_value), sizeof(char)); strcpy(ini.homedir, att_value); count++; } else if(( strcmp(att, "logfilename")) == 0) { strcpy(ini.logfile, att_value); } else if(( strcmp(att, "autoshutdown")) == 0) { ini.autoshutdown = atoi(att_value); } else if(( strcmp(att, "ttl")) == 0) { ini.ttl = atoi(att_value); } else if(( strcmp(att, "msglifetime")) == 0) { ini.msglifetime = atoi(att_value); } else if(( strcmp(att, "getmsglifetime")) == 0) { ini.getmsglifetime = atoi(att_value); } else if(( strcmp(att, "initneighbors")) == 0) { ini.initneighbor = atoi(att_value); } else if(( strcmp(att, "jointimeout")) == 0) { ini.jointo = atoi(att_value); } else if(( strcmp(att, "keepalivetimeout")) == 0) { ini.keepaliveto = atoi(att_value); } else if(( strcmp(att, "minneighbors")) == 0) { ini.minneighbor = atoi(att_value); } else if(( strcmp(att, "nocheck")) == 0) { ini.nocheck = atoi(att_value); } else if(( strcmp(att, "cacheprob")) == 0) { ini.cacheprob = strtod(att_value, NULL); } else if(( strcmp(att, "storeprob")) == 0) { ini.storeprob = strtod(att_value, NULL); } else if(( strcmp(att, "neighborstoreprob")) == 0) { ini.nstoreprob = strtod(att_value, NULL); } else if(( strcmp(att, "cachesize")) == 0) { ini.cachesize = atol(att_value); } else if(( strcmp(att, "permsize")) == 0) { ini.permsize = atoi(att_value); } else { //printf("bad attribute in the ini file =%s... ignoring it\n", att); } } if(count2 == 1) { if(( strcmp(att,"retry")) == 0) { ini.retry = atoi(att_value); continue; } temp = att; while(*temp != '\0') { if(*temp == ':') { *temp = '\0'; att_value = temp + 1; } temp++; } string str = att; ini.beacon_name.push_back(str); ini.beacon_port.push_back(atoi(att_value)); } free(line); // free(att); } unsigned int i; char *ip, *myip, hostname[NAME_LEN]; int len_hostname = strlen(ini.homedir); if(ini.homedir[len_hostname - 1] != '/') { char *new_home_dir; new_home_dir = (char *)calloc(len_hostname + 2, sizeof(char)); fflush(stdout); if(new_home_dir == NULL) { printf("Memory allocation Failed !!!"); exit(1); } strcpy(new_home_dir, ini.homedir); if(DEBUG_RC1) printf("old: %s\n", new_home_dir); new_home_dir[len_hostname] = '/'; new_home_dir[len_hostname + 1] = '\0'; if(DEBUG_RC1) printf("new: %s\n", new_home_dir); free(ini.hostname); ini.homedir = new_home_dir; } const char *buffer; struct hostent *h; gethostname(hostname, NAME_LEN); //cout<<hostname; ini.hostname = (char *)calloc(sizeof(hostname) + 1, sizeof(char)); ini.file_path = ini.homedir; ini.file_path +="files/"; strcpy(ini.hostname, hostname); ini.node_id = (char *)calloc(sizeof(hostname) + 6, sizeof(char)); snprintf(ini.node_id, sizeof(hostname) + 6, "%s_%d", ini.hostname, ini.port); ini.node_inst_id = (char *)calloc(sizeof(hostname) + 26, sizeof(char)); snprintf(ini.node_inst_id, sizeof(hostname) + 26, "%s_%ld", ini.node_id, time(NULL)); if((h = gethostbyname(hostname)) == NULL) { //perror("Address Request in init()"); return 1; } myip = inet_ntoa(*((struct in_addr *)h->h_addr)); //printf("%s\n", myip); for(i=0; i< ini.beacon_name.size(); i++) { buffer = ini.beacon_name[i].c_str(); if((h = gethostbyname(buffer)) == NULL) { //perror("Address Request in init()"); return 1; } ip = inet_ntoa(*((struct in_addr *)h->h_addr)); //printf("%s\n", ip); if((strcmp(myip, ip) == 0) && (ini.port == ini.beacon_port[i])) return 1; //True for a beacon node } return 0; }
int main(int argc, char *argv[]){ int i, j; point *points; char *str, *endptr; long x, y; double *sides, angle; points = (point *)malloc(sizeof(point)*6); sides = (double *)malloc(sizeof(double)*3); /*angles = (double *)malloc(sizeof(double)*3);*/ /* there should be six args */ if(argc < 7) /*fprintf(stderr, "Usage: %s x1 y1 x2 y2 x3 y3\n", argv[0]);*/ print_error_and_exit(); /* each arg should be an integer */ for(i=1; i<7; i++){ j = i / 2; str = argv[i++]; x = strtol(str, &endptr, 10); if(errno != 0 && x == 0) /*perror("strtol");*/ print_error_and_exit(); if(endptr == str) /*fprintf(stderr, "No digits were found\n");*/ print_error_and_exit(); str = argv[i]; y = strtol(str, &endptr, 10); if(errno != 0 && y == 0) /*perror("strtol");*/ print_error_and_exit(); if(endptr == str) /*fprintf(stderr, "No digits were found\n");*/ print_error_and_exit(); points[j].x = x; points[j].y = y; } /* foreach arg: -(2^30-1) <= arg <= 2^30-1 */ for(i=0; i<3; i++) if(points[i].x < (-(pow(2, 30) - 1)) || points[i].x > (pow(2, 30) - 1) || points[i].y < (-(pow(2, 30) - 1)) || points[i].y > (pow(2, 30) - 1)) /*fprintf(stderr, "All coordinates must be >= -(2^30-1) and <= 2^30-1\n");*/ print_error_and_exit(); /* find sides*/ find_sides(points, sides); /* reorder so largest side is at index 1 */ put_longest_in_middle(sides); /* use cosine rule to find largest angle*/ /* cosine rule:*/ /* Let B be the largest angle and*/ /* a,b,c be the side lengths*/ /* with b the longest, then*/ /* B = cos^(-1)((a^2 + c^2 - b^2) / 2ac)*/ angle = acos((pow(sides[0], 2) + pow(sides[2], 2) - pow(sides[1], 2)) / (2 * sides[0] * sides[2])); /* DEBUGGERY */ /*for(i=0; i<3; i++) printf("sides[%d] is %f\n", i, sides[i]); printf("angle is %f\n", angle); printf("M_PI is %f\n", M_PI); printf("M_PI / 2 is %f\n", M_PI / 2); printf("fabs(M_PI / 2 - angle) is %f\n", fabs(M_PI / 2 - angle)); printf("M_PI / 2 - angle is %f\n", M_PI / 2 - angle); printf("fabs(sides[0] - sides[2]) is %f\n", fabs(sides[0] - sides[2]));*/ /* DEBUGGERY */ /*if largest angle == 180 then not a triangle */ if(fabs(M_PI - angle) < e){ printf("not a triangle\n"); return 0; } /* scalene || isosceles || equilateral (impossible) */ if(fabs(sides[1] - sides[0]) < e || fabs(sides[2] - sides[0]) < e || fabs(sides[2] - sides[1]) < e) printf("isosceles "); else printf("scalene "); /* acute || obtuse || right */ if(fabs(M_PI / 2 - angle) < e) printf("right\n"); else if(angle < M_PI / 2) printf("acute\n"); else printf("obtuse\n"); }