/** * Iterates to the next bucket and sets the value and key elements of the * iterator to ones belonging to that bucket. * * Parameters: * - iter: a pointer to an initialized iterator object * * Returns: * - 1 when a new bucket was found, or 0 when there were no more buckets */ int qhi_iterator_forward(qhit *iter) { if (iter->current_bucket == NULL) { /* First entry, so we search for the first key */ if (forward_to_bucket_list(iter)) { iter->current_bucket = iter->hash->bucket_list[iter->bucket_list_idx].head; iter->key = iter->current_bucket->key; read_values((qhit**) &iter); return 1; } } else { /* Check if there are more buckets in this list */ if (iter->current_bucket->next) { iter->current_bucket = iter->current_bucket->next; iter->key = iter->current_bucket->key; read_values((qhit**) &iter); return 1; } /* If not, go to the next bucket if it exists */ iter->bucket_list_idx++; if (forward_to_bucket_list(iter)) { iter->current_bucket = iter->hash->bucket_list[iter->bucket_list_idx].head; iter->key = iter->hash->bucket_list[iter->bucket_list_idx].head->key; read_values((qhit**) &iter); return 1; } } return 0; }
int main() { double sum=0; int values; values = read_values(sum); printf("Average: %g\n",sum/values); return 0; }
//adds a new entry to the list with the values user inputs entry* set(char *ptr, char key[MAX_KEY], entry *entryHead) { //list is empty, initialize a new list if (entryHead == NULL) { entryHead = (entry *) entrylist_init(key); } //find entry with the specified key entry *chosenEntry = get_entry(entryHead, key); //entry with such a key does not exist. add new entry to the list if (chosenEntry == NULL) { chosenEntry = entry_add(entryHead, key); entryHead = chosenEntry; } //if the entry already has values, free the memory that is allocated for those values if (chosenEntry->values != NULL) { free(chosenEntry->values); } size_t length = 0; chosenEntry->values = read_values(ptr, &length); chosenEntry->length = length; printf("ok\n"); return entryHead; //updated head of the list }
uint8_t receiveRemote() { uint8_t data[2]; read_values(17, data, 1); //showRemoteSignalInDisplay(data[0]); return data[0]; }
//depending on the command passed executes either PUSH or APPEND function void push_append(char *ptr, char key[MAX_KEY], entry *entryHead, char command[MAX_COMMAND]) { entry *chosenEntry = get_entry(entryHead, key); if (chosenEntry == NULL) { printf("no such key\n"); return; } else { printf("ok\n"); } size_t newValLength = 0; int *newValues = read_values(ptr, &newValLength); //get values to push or append int *oldValues = chosenEntry->values; //get values that the entry already stores size_t oldValLength = chosenEntry->length; size_t totalLength = oldValLength + newValLength; //PUSH if (strcasecmp(command, "PUSH") == 0) { int *allValues = (int *) malloc(sizeof(int)*totalLength); //allocate enough memory for both new and old values int counter = 0; //push new values to the allocated space for (int i = newValLength - 1; i >= 0; i--) { allValues[counter] = newValues[i]; counter++; } //copy old values to the allocated space for (int i = 0; i < oldValLength; i++) { allValues[counter] = oldValues[i]; counter++; } free(oldValues); chosenEntry->values = allValues; } //APPEND else { oldValues = (int *) realloc(oldValues, sizeof(int)*totalLength); //allocate more space to the memory where old values are stored //copy all new values to the memory space where the old values are stored for (int counter = 0; counter < newValLength; counter++) { oldValues[oldValLength + counter] = newValues[counter]; } chosenEntry->values = oldValues; } free(newValues); chosenEntry->length = totalLength; }
static ssize_t ms5611_read(struct file *filp, char *buffer, size_t buflen) { /* if the buffer is large enough, and data are available, return success */ if (buflen >= 6) { if (read_values((int16_t *)buffer)) return 6; /* no data */ return 0; } /* buffer too small */ errno = ENOSPC; return ERROR; }
void get_cpuinfo(cpuinfo_t *cpuinfo) { char buffer[BUFSIZ]; unsigned int values[4]; if (read_file("/proc/stat", buffer, sizeof (buffer)) != -1) { read_values(buffer, "cpu", values, 4); cpuinfo->user = values[0]; cpuinfo->nice = values[1]; cpuinfo->system = values[2]; cpuinfo->idle = values[3]; cpuinfo->irqs = read_value(buffer, "intr"); cpuinfo->cntxts = read_value(buffer, "ctxt"); } else { memset(cpuinfo, 0, sizeof (cpuinfo_t)); } }
void get_netinfo(netinfo_t *netinfo) { struct ifreq ifreq; char buffer[BUFSIZ]; char name[16]; unsigned int values[16]; int fd; int i; if (read_file("/proc/net/dev", buffer, sizeof (buffer)) == -1) { buffer[0] = '\0'; } fd = socket(AF_INET, SOCK_DGRAM, 0); for (i = 0; i < g_interface_list_length; i++) { snprintf(ifreq.ifr_name, sizeof (ifreq.ifr_name), "%s", g_interface_list[i]); if (fd != -1 && ioctl(fd, SIOCGIFFLAGS, &ifreq) != -1) { if (ifreq.ifr_flags & IFF_UP) { netinfo->status[i] = (ifreq.ifr_flags & IFF_RUNNING) ? 1 : 7; } else { netinfo->status[i] = 2; } } else { netinfo->status[i] = 4; } if (buffer[0] != '\0') { snprintf(name, sizeof (name), "%s:", g_interface_list[i]); read_values(buffer, name, values, 16); } else { memset(values, 0, sizeof (values)); } netinfo->rx_bytes[i] = values[0]; netinfo->rx_packets[i] = values[1]; netinfo->rx_errors[i] = values[2]; netinfo->rx_drops[i] = values[3]; netinfo->tx_bytes[i] = values[8]; netinfo->tx_packets[i] = values[9]; netinfo->tx_errors[i] = values[10]; netinfo->tx_drops[i] = values[11]; } if (fd != -1) { close(fd); } }
int main(int argc, char** argv) { modbus_param_t mb_param; int half_duplex = 0; int c; char ** svp; int i; int set_args_index = -1; char set_args[MAX_SETS][MAX_STR]; char *device = NULL; memset(set_args, 0, sizeof(set_args)); while( (c = getopt(argc, argv, "hnvd:s:")) != -1) { switch(c){ case 's': /* NOTE IMPORTANT! This block MUST COME FIRST in the switch, before any opts, otherwise GCC or getopts or something gets the indexes all wrong */ // Thanks to http://stackoverflow.com/questions/3939157/c-getopt-multiple-value // Special case the first arg set_args_index = 0; strcpy(set_args[0], optarg); // Loop the rest i=1; while(optind < argc && *argv[optind] != '-'){ strcpy(set_args[i], argv[optind]); if(debug > 0){ printf("getopts multiopt: %s %s\n", argv[optind], set_args[i] ); } // Ugly but explicit set_args_index++; i++; optind++; } break; case 'h': half_duplex = 1; break; case 'v': debug = 1; break; case 'n': dry = 1; break; case 'd': device = optarg; default: break; } } if(!device){ printf("need to give serial port on command line\n"); usage(); return(1); } /* Setup the serial port parameters */ modbus_init_rtu(&mb_param, device, 9600, "none", 8, 2, half_duplex); if(debug > 0){ modbus_set_debug(&mb_param, TRUE); } /* Open the MODBUS connection */ if (modbus_connect(&mb_param) == -1) { printf("ERROR Connection failed\n"); return(1); } if(debug > 0){ printf("We have %d set args to process...\n", set_args_index + 1); } i = set_args_index; while(i >= 0){ printf("\nSetting values... %s\n", set_args[i]); set_options(&mb_param, set_args[i]); i--; } if(dry < 1 && set_args_index >= 0){ commit_options(&mb_param); } if(dry < 1){ read_values(&mb_param); } /* Close the MODBUS connection */ modbus_close(&mb_param); return(0); }
int main(int argc, char *argv[]) { Context ctx; int i, tic, toc, total_toc, total_tic; double duration; int error; if (argc < 6) { fprintf(stderr, "%s TEST.conf TEST.data SEED_VAL DEBUG_VAL " "ENGINE_TYPE(0 fuzzy, 1 ann, 2 naive, " "3 fuzzy_no_simplification) [MAX_MEMORY_FOR_OBSERVATION] " "[ANN_CACHE_SIZE] [ANN_PSEUDO_REHEARSAL_STRATETY (1 for true, 0 for false)]\n", argv[0]); fprintf(stderr, "Eg: %s simple_office.conf simple_office.forget_lights_on.data " "30 1 1\n", argv[0]); return 1; } ctx.rand = g_rand_new_with_seed(atoi(argv[3])); ctx.debug = !!atoi(argv[4]); ctx.engine_type = atoi(argv[5]); ctx.sml = _sml_new(ctx.engine_type); if (!ctx.sml) { fprintf(stderr, "Failed to create sml\n"); return 2; } if (!read_config(argv[1], &ctx)) { fprintf(stderr, "Failed to read configuration: %s\n", argv[1]); return 3; } if (!read_values(argv[2], &ctx)) { fprintf(stderr, "Failed to read data: %s\n", argv[2]); return 4; } add_time_day(&ctx); sml_set_read_state_callback(ctx.sml, read_state_cb, &ctx); sml_set_output_state_changed_callback(ctx.sml, output_state_changed_cb, &ctx); sml_set_stabilization_hits(ctx.sml, 0); if (ctx.engine_type == FUZZY_ENGINE_NO_SIMPLIFICATION) sml_fuzzy_set_simplification_disabled(ctx.sml, true); if (argc >= 7) { int observations = atoi(argv[6]); if (observations < 0) { fprintf(stderr, "MAX_MEMORY_FOR_OBSERVATIOS (%s) must be a non " "negative value\n", argv[6]); return 5; } sml_set_max_memory_for_observations(ctx.sml, observations); } if (ctx.engine_type == ANN_ENGINE) { if (argc >= 8) { int cache_size = atoi(argv[7]); if (cache_size < 0 || cache_size >= UINT16_MAX) { fprintf(stderr, "ANN_CACHE_SIZE (%s) must be greater or equal " "to 0 an less or equal to %d\n", argv[7], UINT16_MAX); return 6; } sml_ann_set_cache_max_size(ctx.sml, cache_size); } if (argc >= 9) sml_ann_use_pseudorehearsal_strategy(ctx.sml, atoi(argv[8]) != 0); } if (ctx.debug) print_scenario(&ctx); ctx.max_iteration_duration = -1; total_tic = clock(); for (i = 0; i < ctx.reads; i++) { tic = clock(); if ((error = sml_process(ctx.sml))) { fprintf(stderr, "=== Unexpected error in simulation. " "Error code: %d ===\n", error); break; } toc = clock(); duration = ((double)toc - tic) / CLOCKS_PER_SEC; _process_results(&ctx, duration); } total_toc = clock(); ctx.duration = ((double)total_toc - total_tic) / CLOCKS_PER_SEC; // scenario may changes thanks to new events created automatically if (ctx.debug) { print_scenario(&ctx); sml_print_debug(ctx.sml, true); } print_results(&ctx); sml_free(ctx.sml); g_list_free_full(ctx.inputs, free_variable); g_list_free_full(ctx.outputs, free_variable); g_list_free_full(ctx.expectations, free_expectation); g_list_free_full(ctx.expectation_blocks, free_element); g_rand_free(ctx.rand); return 0; }
void actuarial_table::parse_table() { LMI_ASSERT(-1 == table_type_ ); LMI_ASSERT(-1 == min_age_ ); LMI_ASSERT(-1 == max_age_ ); LMI_ASSERT(-1 == select_period_ ); LMI_ASSERT(-1 == max_select_age_); fs::path data_path(filename_); data_path = fs::change_extension(data_path, ".dat"); fs::ifstream data_ifs(data_path, ios_in_binary()); if(!data_ifs) { fatal_error() << "File '" << data_path << "' is required but could not be found. Try reinstalling." << LMI_FLUSH ; } data_ifs.seekg(table_offset_, std::ios::beg); LMI_ASSERT(table_offset_ == data_ifs.tellg()); while(data_ifs) { boost::int16_t record_type; read_datum(data_ifs, record_type, sizeof(boost::int16_t)); soa_table_length_type nominal_length; read_datum(data_ifs, nominal_length, sizeof(boost::int16_t)); switch(record_type) { case 2: // 4-byte integer: Table number. { boost::int32_t z; read_datum(data_ifs, z, nominal_length); LMI_ASSERT(z == table_number_); } break; case 3: // [unsigned] char: Table type. { // Meaning: {A, D, S} --> {age, duration, select}. // SOA apparently permits upper or lower case. LMI_ASSERT(-1 == table_type_); unsigned char z; read_datum(data_ifs, z, nominal_length); z = static_cast<unsigned char>(std::toupper(z)); LMI_ASSERT('A' == z || 'D' == z || 'S' == z); table_type_ = z; } break; case 12: // 2-byte integer: Minimum age. { LMI_ASSERT(-1 == min_age_); boost::int16_t z; read_datum(data_ifs, z, nominal_length); LMI_ASSERT(0 <= z && z <= methuselah); min_age_ = z; } break; case 13: // 2-byte integer: Maximum age. { LMI_ASSERT(-1 == max_age_); boost::int16_t z; read_datum(data_ifs, z, nominal_length); LMI_ASSERT(0 <= z && z <= methuselah); max_age_ = z; } break; case 14: // 2-byte integer: Select period. { LMI_ASSERT(-1 == select_period_); boost::int16_t z; read_datum(data_ifs, z, nominal_length); LMI_ASSERT(0 <= z && z <= methuselah); select_period_ = z; } break; case 15: // 2-byte integer: Maximum select age. { LMI_ASSERT(-1 == max_select_age_); boost::int16_t z; read_datum(data_ifs, z, nominal_length); LMI_ASSERT(0 <= z && z <= methuselah); max_select_age_ = z; } break; case 17: // 8-byte doubles: Table values. { read_values(data_ifs, nominal_length); } break; case 9999: // End of table. { goto done; } default: { char skipped[65536]; data_ifs.read(skipped, nominal_length); } } } done: LMI_ASSERT(-1 != table_type_ ); LMI_ASSERT(-1 != min_age_ ); LMI_ASSERT(-1 != max_age_ ); LMI_ASSERT(-1 != select_period_ ); LMI_ASSERT(-1 != max_select_age_); }
void dequeue_stl(const std::string& fname) { std::shared_ptr<std::istream> input = get_input(fname); const std::uint32_t T = read_values(input)[0]; #ifdef DEBUG std::cout << "T " << T << std::endl; #endif for (std::uint32_t i = 0; i < T; ++i) { const std::vector<std::uint32_t> val = read_values(input); const std::uint32_t N = val[0]; const std::uint32_t K = val[1]; #ifdef DEBUG std::cout << "N = " << N << " K = " << K << std::endl; #endif const std::vector<std::uint32_t> A = read_values(input); print_output(A); std::vector<std::uint32_t> output; const std::uint32_t ITER = N - K + 1; output.reserve(ITER); if (N == 1 || K == 1 || K >= N) { print_output(A); return; } else { std::uint32_t max; std::uint32_t max2; if (a[0] < a[1]) { max = a[1]; max2 = a[0]; } else { max = a[0]; max2 = a[1]; } if (N == 2) { output.push_back(max); output.push_back(max2); print_output(output); return } else { for (std::uint32_t j = 2; j < N; ++j) { if (a[j] > max) { max2 = max; max = a[j]; } else if (a[j] > max2) { max2 = a[j]; } if (j == K) { } } //print_output(output); } }
/* The versatile printf formatting routine. It expects a callback function OUTFNC and an opaque argument OUTFNCARG used for actual output of the formatted stuff. FORMAT is the format specification and VAARGS a variable argumemt list matching the arguments of FORMAT. */ int estream_format (estream_printf_out_t outfnc, void *outfncarg, const char *format, va_list vaargs) { /* Buffer to hold the argspecs and a pointer to it.*/ struct argspec_s argspecs_buffer[DEFAULT_MAX_ARGSPECS]; argspec_t argspecs = argspecs_buffer; size_t argspecs_len; /* Number of specifications in ARGSPECS. */ /* Buffer to hold the description for the values. */ struct valueitem_s valuetable_buffer[DEFAULT_MAX_VALUES]; valueitem_t valuetable = valuetable_buffer; int rc; /* Return code. */ size_t argidx; /* Used to index the argspecs array. */ size_t validx; /* Used to index the valuetable. */ int max_pos;/* Highest argument position. */ size_t nbytes = 0; /* Keep track of the number of bytes passed to the output function. */ int myerrno = errno; /* Save the errno for use with "%m". */ /* Parse the arguments to come up with descriptive list. We can't do this on the fly because we need to support positional arguments. */ rc = parse_format (format, &argspecs, DIM(argspecs_buffer), &argspecs_len); if (rc) goto leave; /* Check that all ARG_POS fields are set. */ for (argidx=0,max_pos=0; argidx < argspecs_len; argidx++) { if (argspecs[argidx].arg_pos != -1 && argspecs[argidx].arg_pos > max_pos) max_pos = argspecs[argidx].arg_pos; if (argspecs[argidx].width_pos > max_pos) max_pos = argspecs[argidx].width_pos; if (argspecs[argidx].precision_pos > max_pos) max_pos = argspecs[argidx].precision_pos; } if (!max_pos) { /* Fill in all the positions. */ for (argidx=0; argidx < argspecs_len; argidx++) { if (argspecs[argidx].width == STAR_FIELD_VALUE) argspecs[argidx].width_pos = ++max_pos; if (argspecs[argidx].precision == STAR_FIELD_VALUE) argspecs[argidx].precision_pos = ++max_pos; if (argspecs[argidx].arg_pos != -1 ) argspecs[argidx].arg_pos = ++max_pos; } } else { /* Check that they are all filled. More test are done later. */ for (argidx=0; argidx < argspecs_len; argidx++) { if (!argspecs[argidx].arg_pos || (argspecs[argidx].width == STAR_FIELD_VALUE && !argspecs[argidx].width_pos) || (argspecs[argidx].precision == STAR_FIELD_VALUE && !argspecs[argidx].precision_pos)) goto leave_einval; } } /* Check that there is no overflow in max_pos and that it has a reasonable length. There may never be more elements than the number of characters in FORMAT. */ if (max_pos < 0 || max_pos >= strlen (format)) goto leave_einval; #ifdef DEBUG dump_argspecs (argspecs, argspecs_len); #endif /* Allocate a table to hold the values. If it is small enough we use a stack allocated buffer. */ if (max_pos > DIM(valuetable_buffer)) { valuetable = calloc (max_pos, sizeof *valuetable); if (!valuetable) goto leave_error; } else { for (validx=0; validx < DIM(valuetable_buffer); validx++) valuetable[validx].vt = VALTYPE_UNSUPPORTED; } for (argidx=0; argidx < argspecs_len; argidx++) { if (argspecs[argidx].arg_pos != - 1) { validx = argspecs[argidx].arg_pos - 1; if (valuetable[validx].vt) goto leave_einval; /* Already defined. */ valuetable[validx].vt = argspecs[argidx].vt; } if (argspecs[argidx].width == STAR_FIELD_VALUE) { validx = argspecs[argidx].width_pos - 1; if (valuetable[validx].vt) goto leave_einval; /* Already defined. */ valuetable[validx].vt = VALTYPE_INT; } if (argspecs[argidx].precision == STAR_FIELD_VALUE) { validx = argspecs[argidx].precision_pos - 1; if (valuetable[validx].vt) goto leave_einval; /* Already defined. */ valuetable[validx].vt = VALTYPE_INT; } } /* Read all the arguments. This will error out for unsupported types and for not given positional arguments. */ rc = read_values (valuetable, max_pos, vaargs); if (rc) goto leave_einval; /* for (validx=0; validx < max_pos; validx++) */ /* fprintf (stderr, "%2d: vt=%d\n", validx, valuetable[validx].vt); */ /* Everything has been collected, go ahead with the formatting. */ rc = do_format (outfnc, outfncarg, format, argspecs, argspecs_len, valuetable, myerrno, &nbytes); goto leave; leave_einval: errno = EINVAL; leave_error: rc = -1; leave: if (valuetable != valuetable_buffer) free (valuetable); if (argspecs != argspecs_buffer) free (argspecs); return rc; }