void MainWindow::parse_packet_sequences() { int slot_idx = 0; for(auto slot_raw : packet_sequences_raw_) { for(auto packet : slot_raw) { auto splitted = split(packet, ' '); std::string frame_id = splitted[0]; if(splitted.size() > 1) { //format1: "frame_id packet_num value periodicity jitter slack" unsigned packet_num = string_to_unsigned(splitted[1]); unsigned periodicity = string_to_unsigned(splitted[3]); unsigned jitter = string_to_unsigned(splitted[4]); unsigned slack = string_to_unsigned(splitted[5]); unsigned tau = slot_idx + (packet_num-1) * periodicity + jitter; packet_sequences[frame_id].second = slot_interval(tau, tau + slack); } packet_sequences[frame_id].first.resize(slot_idx + 1, 0); packet_sequences[frame_id].first[slot_idx] = 1; } ++slot_idx; } for(auto& packet_seq : packet_sequences) { frame_id_labels_.push_back(new QLabel(packet_seq.first.c_str(), this)); packet_seq.second.first.resize(packet_sequences_raw_.size() + col_count_, 0); for(unsigned i=packet_seq.second.second.first; i<packet_seq.second.second.second; ++i) packet_seq.second.first[i] = 2; packet_seq.second.first[packet_seq.second.second.second] = 3; } }
/**************************************************************************** * create_entry * * Create a CMOS entry structure representing the given information. Perform * sanity checking on input parameters. ****************************************************************************/ static void create_entry(cmos_entry_t * cmos_entry, const char start_bit_str[], const char length_str[], const char config_str[], const char config_id_str[], const char name_str[]) { cmos_entry->bit = string_to_unsigned(start_bit_str, "start-bit"); cmos_entry->length = string_to_unsigned(length_str, "length"); if (config_str[1] != '\0') goto bad_config_str; switch (config_str[0]) { case 'e': cmos_entry->config = CMOS_ENTRY_ENUM; break; case 'h': cmos_entry->config = CMOS_ENTRY_HEX; break; case 's': cmos_entry->config = CMOS_ENTRY_STRING; break; case 'r': cmos_entry->config = CMOS_ENTRY_RESERVED; break; default: goto bad_config_str; } cmos_entry->config_id = string_to_unsigned(config_id_str, "config-ID"); if (strlen(name_str) >= CMOS_MAX_NAME_LENGTH) { fprintf(stderr, "%s: Error on line %d of CMOS layout file: name too " "long (max length is %d).\n", prog_name, line_num, CMOS_MAX_NAME_LENGTH - 1); exit(1); } strcpy(cmos_entry->name, name_str); return; bad_config_str: fprintf(stderr, "%s: Error on line %d of CMOS layout file: 'e', 'h', or " "'r' expected for config value.\n", prog_name, line_num); exit(1); }
/**************************************************************************** * create_enum * * Create a CMOS enumeration structure representing the given information. * Perform sanity checking on input parameters. ****************************************************************************/ static void create_enum(cmos_enum_t * cmos_enum, const char id_str[], const char value_str[], const char text_str[]) { cmos_enum->config_id = string_to_unsigned(id_str, "ID"); cmos_enum->value = string_to_unsigned_long(value_str, "value"); if (strlen(text_str) >= CMOS_MAX_TEXT_LENGTH) { fprintf(stderr, "%s: Error on line %d of CMOS layout file: text too " "long (max length is %d).\n", prog_name, line_num, CMOS_MAX_TEXT_LENGTH - 1); exit(1); } strcpy(cmos_enum->text, text_str); }
BOOLEAN string_to_cents(char *c_string, unsigned *cents){ unsigned u_cents; if(!string_to_unsigned(c_string, &u_cents)) { return FALSE; } if(u_cents==5 || u_cents== 10 || u_cents== 20 || u_cents== 50|| u_cents==100 || u_cents==200 || u_cents== 500 || u_cents== 1000) { *cents= u_cents; return TRUE; } return FALSE; }
void generate_id(char *new_id, struct ppd_list *list){ struct ppd_node *node = list->head; unsigned highest_id= 0; unsigned current_id; while(node!= NULL) { string_to_unsigned((node->data->id)+1, ¤t_id); if(current_id> highest_id) { highest_id= current_id; } node=node->next; } sprintf(new_id, "I000%u",highest_id+1); }
int string_to_ninteger(int* n, const char* buffer, unsigned base) { int s=1; if (*buffer == '-') { s=-1; buffer++; } unsigned u=0; int cnt = string_to_unsigned(&u, buffer); if (cnt != -1) { *n = s*(int)u; if (s == -1) { ++cnt; // for leading '-' } } return cnt; }
inf string_to_inf(const std::string& str, unsigned radix) { inf result; if (radix != 0 && (radix < 2 || radix > 36)) throw std::invalid_argument("invalid radix value " + unsigned_to_string(radix)); unsigned i = 0; // the radix passed as a parameter is just the default - it can be // overridden by either the C prefix or the hash prefix // Note: a leading zero is the C-style prefix for octal - I only make this // override the default when the default radix is not specified // first check for a C-style prefix bool c_style = false; if (i < str.size() && str[i] == '0') { // binary or hex if (i+1 < str.size() && tolower(str[i+1]) == 'x') { c_style = true; radix = 16; i += 2; } else if (i+1 < str.size() && tolower(str[i+1]) == 'b') { c_style = true; radix = 2; i += 2; } else if (radix == 0) { c_style = true; radix = 8; i += 1; } } // now check for a hash-style prefix if a C-style prefix was not found if (i == 0) { // scan for the sequence {digits}# bool hash_found = false; unsigned j = i; for (; j < str.size(); j++) { if (!isdigit(str[j])) { if (str[j] == '#') hash_found = true; break; } } if (hash_found) { // use the hash prefix to define the radix // i points to the start of the radix and j points to the # character std::string slice = str.substr(i, j-i); radix = string_to_unsigned(slice); i = j+1; } } if (radix == 0) radix = 10; if (radix < 2 || radix > 36) throw std::invalid_argument("invalid radix value"); if (c_style) { // the C style formats are bit patterns not integer values - these need // to be sign-extended to get the right value std::string binary; if (radix == 2) { for (unsigned j = i; j < str.size(); j++) { switch(str[j]) { case '0': binary += '0'; break; case '1': binary += '1'; break; default: throw std::invalid_argument("invalid binary character in string " + str); } } } else if (radix == 8) { for (unsigned j = i; j < str.size(); j++) { switch(str[j]) { case '0': binary += "000"; break; case '1': binary += "001"; break; case '2': binary += "010"; break; case '3': binary += "011"; break; case '4': binary += "100"; break; case '5': binary += "101"; break; case '6': binary += "110"; break; case '7': binary += "111"; break; default: throw std::invalid_argument("invalid octal character in string " + str); } } } else { for (unsigned j = i; j < str.size(); j++) { switch(tolower(str[j])) { case '0': binary += "0000"; break; case '1': binary += "0001"; break; case '2': binary += "0010"; break; case '3': binary += "0011"; break; case '4': binary += "0100"; break; case '5': binary += "0101"; break; case '6': binary += "0110"; break; case '7': binary += "0111"; break; case '8': binary += "1000"; break; case '9': binary += "1001"; break; case 'a': binary += "1010"; break; case 'b': binary += "1011"; break; case 'c': binary += "1100"; break; case 'd': binary += "1101"; break; case 'e': binary += "1110"; break; case 'f': binary += "1111"; break; default: throw std::invalid_argument("invalid hex character in string " + str); } } } // now convert the value result.resize((unsigned)binary.size()); for (unsigned j = 0; j < (unsigned)binary.size(); j++) result.preset((unsigned)binary.size() - j - 1, binary[j] == '1'); } else { // now scan for a sign and find whether this is a negative number bool negative = false; if (i < str.size()) { switch (str[i]) { case '-': negative = true; i++; break; case '+': i++; break; } } for (; i < str.size(); i++) { result *= inf(radix); int ch = from_char[(unsigned char)str[i]] ; if (ch == -1) throw std::invalid_argument("invalid character in string " + str + " for radix " + unsigned_to_string(radix)); result += inf(ch); } if (negative) result.negate(); } return result; }
/**************************************************************************** * set_checksum_info * * Set CMOS checksum information according to input parameters and perform * sanity checking on input parameters. ****************************************************************************/ static void set_checksum_info(const char start_str[], const char end_str[], const char index_str[]) { cmos_checksum_layout_t layout; /* These are bit positions that we want to convert to byte positions. */ layout.summed_area_start = string_to_unsigned(start_str, "CMOS checksummed area start"); layout.summed_area_end = string_to_unsigned(end_str, "CMOS checksummed area end"); layout.checksum_at = string_to_unsigned(index_str, "CMOS checksum location"); switch (checksum_layout_to_bytes(&layout)) { case OK: break; case LAYOUT_SUMMED_AREA_START_NOT_ALIGNED: fprintf(stderr, "%s: Error on line %d of CMOS layout file. CMOS " "checksummed area start is not byte-aligned.\n", prog_name, line_num); goto fail; case LAYOUT_SUMMED_AREA_END_NOT_ALIGNED: fprintf(stderr, "%s: Error on line %d of CMOS layout file. CMOS " "checksummed area end is not byte-aligned.\n", prog_name, line_num); goto fail; case LAYOUT_CHECKSUM_LOCATION_NOT_ALIGNED: fprintf(stderr, "%s: Error on line %d of CMOS layout file. CMOS " "checksum location is not byte-aligned.\n", prog_name, line_num); goto fail; case LAYOUT_INVALID_SUMMED_AREA: fprintf(stderr, "%s: Error on line %d of CMOS layout file. CMOS " "checksummed area end must be greater than CMOS checksummed " "area start.\n", prog_name, line_num); goto fail; case LAYOUT_CHECKSUM_OVERLAPS_SUMMED_AREA: fprintf(stderr, "%s: Error on line %d of CMOS layout file. CMOS " "checksum overlaps checksummed area.\n", prog_name, line_num); goto fail; case LAYOUT_SUMMED_AREA_OUT_OF_RANGE: fprintf(stderr, "%s: Error on line %d of CMOS layout file. CMOS " "checksummed area out of range.\n", prog_name, line_num); goto fail; case LAYOUT_CHECKSUM_LOCATION_OUT_OF_RANGE: fprintf(stderr, "%s: Error on line %d of CMOS layout file. CMOS " "checksum location out of range.\n", prog_name, line_num); goto fail; default: BUG(); } cmos_checksum_start = layout.summed_area_start; cmos_checksum_end = layout.summed_area_end; cmos_checksum_index = layout.checksum_at; return; fail: exit(1); }