t_map ft_read(char *filename) { int fd; char *line; char **tab; t_index index; t_map grid; index.j = 0; grid.nb_lines = count_lines(filename); grid.tab = (int **)ft_memalloc(sizeof(int *) * count_lines(filename)); fd = open(filename, O_RDONLY); while (get_next_line(fd, &line) > 0) { grid.nb_col = count_col(line); //attention ne fonctionne que pour les maps rectangle (prend la valeur de la derniere line) grid.tab[index.j] = (int *)ft_memalloc(sizeof(int) * count_col(line)); tab = ft_strsplit(line, ' '); index.i = 0; while (tab[index.i]) { grid.tab[index.j][index.i] = ft_atoi(tab[index.i]); index.i++; } index.j++; } return (grid); }
char test(int cur_col, int cur_line, t_lines *lines, t_pos *pos) { char *cur_char; int length; cur_char = get_pos(cur_line, cur_col, lines); length = my_strlen(get_line(cur_line, lines)->buffer); if (cur_line == 0 && cur_col == 0 && *cur_char != pos->top_left) return (-1); if (cur_line == 0 && cur_col == (length-1) && *cur_char != pos->top_right) return (-2); if (cur_line == count_lines(lines)-1 && cur_col == 0 && *cur_char != pos->bottom_left) return (-3); if (cur_line == count_lines(lines)-1 && cur_col == (length-1) && *cur_char != pos->bottom_right) return (-3); if (cur_line > 0 && cur_line < count_lines(lines)-1 && cur_col == 0 && *cur_char != pos->left) return (-4); if (cur_line > 0 && cur_line < count_lines(lines)-1 && cur_col == (length-1) && *cur_char != pos->right) return (-5); return (1); }
END_TEST START_TEST (count_lines_test) { int l = count_lines( "a\n" "sdijb\n" "aosjfblk\n" "alks\n" "lkfn\n" "kajs\n" "nfk\n" "lf"); ck_assert_int_eq(l, 8); l = count_lines( "a\n" "sdijb\n"); ck_assert_int_eq(l, 2); l = count_lines("a"); ck_assert_int_eq(l, 1); }
/* * Prompts user to input paths to data files, then loads it. */ void prompt_for_files(){ char fname[100]; printf("Please, give a path to the file containing " "event name, date and start time:\n "); scanf("%s", &fname); load_event_data(&event, fname); printf("\nPlease, give a path to the file containing " "nodes:\n "); scanf("%s", &fname); TOTAL_NODES = count_lines(fname); nodes = load_nodes_from_file(fname); printf("\nPlease, give a path to the file containing " "tracks:\n "); scanf("%s", &fname); TOTAL_TRACKS = count_lines(fname); tracks = load_tracks_from_file(fname); printf("\nPlease, give a path to the file containing " "courses:\n "); scanf("%s", &fname); TOTAL_COURSES = count_lines(fname); courses = load_courses_from_file(fname); printf("\nPlease, give a path to the file containing " "entrants:\n "); scanf("%s", &fname); TOTAL_ENTRANTS = count_lines(fname); entrants = load_entrants_from_file(fname); }
char parse_pos(t_pos *pos, t_lines *lines) { int cur_line; int return_v; int cur_col; int length; cur_line = 0; while (cur_line < count_lines(lines)) { cur_col = 0; length = my_strlen(get_line(cur_line, lines)->buffer); while (cur_col < length) { if (count_lines(lines) > 2) { if (test(cur_col, cur_line, lines, pos) <= 0) return (0); } cur_col = cur_col + 1; } cur_line = cur_line + 1; } return (1); }
wxString pgsRecord::value() const { wxString data; pgsVarMap vars; // Go through each line and enclose it into braces for (USHORT i = 0; i < count_lines(); i++) { data += wxT("("); // Go through each column and separate them with commas for (USHORT j = 0; j < count_columns(); j++) { wxString elm(m_record[i][j]->eval(vars)->value()); if (!m_record[i][j]->is_number()) { elm.Replace(wxT("\\"), wxT("\\\\")); elm.Replace(wxT("\""), wxT("\\\"")); elm = wxT("\"") + elm + wxT("\""); } data += elm + (j != count_columns() - 1 ? wxT(",") : wxT("")); } data += (i == count_lines() - 1) ? wxT(")") : wxT(")\n"); } // Return the string representation of the record return data; }
int main(int argc,char *argv[]){ int i=0; srand(time(NULL)); net mlpnet; int layer_neurons[]={2,10,5,1}; char sigmods[]={'l','l','l','l'}; init_net(&mlpnet,4,layer_neurons,sigmods); float *inputs=(float*)malloc(sizeof(float)*2); sample *samples; int num_lines=count_lines(argv[1]); samples=(sample*)malloc(sizeof(sample)*num_lines); read_samples(argv[1],samples,num_lines); train(&mlpnet,num_lines,samples,EPOCHS); free_samples(samples,num_lines); //================================================== num_lines=count_lines(argv[2]); sample *test_samples=(sample*)malloc(sizeof(sample)*num_lines); read_samples(argv[2],test_samples,num_lines); float *scores=(float*)malloc(sizeof(float)*num_lines); predict(&mlpnet,num_lines,test_samples,scores); free_samples(test_samples,num_lines); free_net(&mlpnet,4,layer_neurons); char scorefile[100]; sprintf(scorefile,"%s.score",argv[1]); FILE *score=fopen(scorefile,"w"); for(i=0;i<num_lines;i++) { fprintf(score,"%f\n",scores[i]); } fclose(score); return 0; }
void do_refresh() { int count = 0; char wrap_output[STRING_LENGTH]; int L; int i; for (i = 0; i < HISTORY; i++) { wrap(wrap_output, lines[i], x); L = count_lines(wrap_output); count = count + L; if (count < y) { move(y - 1 - count, 0); printw(wrap_output); clrtoeol(); } } move(y - 1, 0); clrtoeol(); printw(">> "); printw(line); clrtoeol(); refresh(); }
extern void parse_file(char *CONFIG_FILE_PATH, char **PARSED_LINES){ char *line; FILE *CONFIG_FILE; ssize_t read; size_t len = 0; int lines = 0; int number_of_lines = 0; CONFIG_FILE = fopen(CONFIG_FILE_PATH, "r"); lines = count_lines(CONFIG_FILE); while((read = getline(&line, &len, CONFIG_FILE)) != -1){ PARSED_LINES[number_of_lines] = malloc(strlen(line)*sizeof(char)); snprintf(PARSED_LINES[number_of_lines], strlen(line), "%s", line); number_of_lines++; } // cleanup fclose(CONFIG_FILE); free(line); }
static void marked_message_internal (char *msg, int mark_start, int mark_end) { rp_screen *s = current_screen (); int num_lines; int width; int height; PRINT_DEBUG (("msg = %s\n", msg?msg:"NULL")); PRINT_DEBUG (("mark_start = %d, mark_end = %d\n", mark_start, mark_end)); /* Calculate the width and height of the window. */ num_lines = count_lines (msg, strlen(msg)); width = defaults.bar_x_padding * 2 + max_line_length(msg); height = FONT_HEIGHT (s) * num_lines + defaults.bar_y_padding * 2; prepare_bar (s, width, height, num_lines > 1 ? 1 : 0); if (defaults.bar_sticky) /* Sticky bar is only showing the current window title, don't mark it */ mark_start = mark_end = -1; else { /* Draw the mark over the designated part of the string. */ correct_mark (strlen (msg), &mark_start, &mark_end); draw_mark (s, msg, mark_start, mark_end); } draw_string (s, msg, mark_start, mark_end); /* Keep a record of the message. */ update_last_message (msg, mark_start, mark_end); }
int deltafile_init_strbuf(struct deltafile *df, struct strbuf *buf, char delim) { size_t lines = count_lines(buf); deltafile_init(df, lines + 1, delim); df->size = split_in_place(&df->file, df->arr, delim); return 0; }
void do_refresh() { int count = 0; char wrap_output[STRING_LENGTH_WRAPPED]; int i; for (i = 0; i < HISTORY; i++) { if (flag[i]) wrap_bars(wrap_output, lines[i], x); else wrap(wrap_output, lines[i], x); int L = count_lines(wrap_output); count = count + L; if (count < y) { move(y - 1 - count, 0); printw("%s", wrap_output); clrtoeol(); } } move(y - 1, 0); clrtoeol(); printw(">> "); printw("%s", input_line); clrtoeol(); refresh(); }
void send_request (int socket_fd, char *cmd, int argc, char *argv[], char* dir, char* classpath) { // Send request type byte: unsigned char type[1] = {CMD_REQ}; if ( write(socket_fd, type, 1) != 1 ) error("Failes to send request type byte to server: %s", error_msg()); // Open stream for writing to server: FILE *socket_in = fdopen(socket_fd, "w"); if ( socket_in == NULL ) error("Failed to create i/o stream for writing to server: %s", error_msg()); // Write request to stream: fprintf(socket_in, "pid\n%i\n\n", getpid()); fprintf(socket_in, "cmd\n%s\n\n", cmd); fprintf(socket_in, "argc\n%i\n\n", argc); if ( argv != NULL ) { int i; for (i = 0; i < argc; i++) fprintf(socket_in, "arg\n%i\n%s\n\n", count_lines(argv[i]), argv[i]); } fprintf(socket_in, "dir\n%s\n\n", dir); if ( classpath != NULL ) fprintf(socket_in, "classpath\n%s\n\n", classpath); fprintf(socket_in, "start\n\n"); fflush(socket_in); }
void wc(FILE *ofile, FILE *infile, char *inname) { char *code; int c; size_t n = 0; code = malloc(1000); int char_count = 0; int line_count = 0; int word_count = 0; char path[1024]; char result[1024]; int fd = fileno(ofile); while ((c = fgetc(ofile)) != EOF) { code[n++] = (char) c; } code[n] = '\0'; char_count = count_characters(code); line_count = count_lines(code); word_count = count_words(code); sprintf(path, "/proc/self/fd/%d",fd); memset(result, 0, sizeof(result)); readlink(path, result, sizeof(result)-1); printf(" %d\t%d\t%d\t%s\t%s\n", line_count, word_count, char_count, result, path); }
/* Recalculates virtual lines of a view with line wrapping. */ static void calc_vlines_wrapped(void) { const char *p; char *q; int i; const int nlines = count_lines(text, INT_MAX); data = reallocarray(NULL, nlines, sizeof(*data)); nvlines = 0; p = text; q = text - 1; for(i = 0; i < nlines; ++i) { char saved_char; q = until_first(q + 1, '\n'); saved_char = *q; *q = '\0'; data[i][0] = nvlines++; data[i][1] = utf8_strsw_with_tabs(p, cfg.tab_stop); data[i][2] = p - text; nvlines += data[i][1]/viewport_width; *q = saved_char; p = q + 1; } }
int encode (FILE *fdsrc, FILE *fdtgt, FILE *fdcfg, FILE *fdkey) { int *configvalues = 0; int lines = 0; int error = 0; printf ("[INFO] ... Start encoding...\n"); // count lines in the read files lines = count_lines (fdsrc); if (lines < 0) { printf ("[ERROR] ... Unable to count lines in file...\n"); return -1; } printf ("[INFO] ... Read %d lines in source file...\n", lines); configvalues = malloc (sizeof(int)*lines); if (configvalues == NULL) { printf ("[ERROR] ... Unable to allocate memory for config values...\n"); return -1; } // interpret values error = interpret_config_values (fdsrc, configvalues, lines); if (error != 0) { printf ("[ERROR] ... Unable to interpret config values...\n"); return -1; } return 0; }
/* count how many lines in a number of files input: vector of filenames output: number of lines */ int count_lines(const vector<string>& files) { int total = 0; for (size_t i = 0; i < files.size(); ++i) { total += count_lines(files[i]); } return total; }
//========================================================================= // // void doeclim_load_calibration(DICE *dice) // // Loads a file containing the calibration results. File should have 3 // columns (cs, kv, alpha). // // Input parameters: // *dice = pointer to DICE object // //========================================================================= void doeclim_load_calibration(DICE *dice) { // Calibration file name const char calfile[] = "calibration_results.txt"; // Find the number of calibration data points from file int n = 0; count_lines(calfile, &n); dice->clim.n_calpoints = n; // Open the file and read in the calibration data FILE * fp; fp = fopen(calfile, "r"); int counter = 0; float this_cs, this_kv, this_alpha; while(!feof(fp)) { if (fscanf(fp, "%g %g %g", &this_cs, &this_kv, &this_alpha) != 3) { break; } dice->clim.cs_cal[counter] = this_cs; dice->clim.kv_cal[counter] = this_kv; dice->clim.alpha_cal[counter] = this_alpha; counter++; if (counter >= n) { break; } } fclose(fp); }
char *pl02x100_get_word(char *aword) { FILE *fp; char filename[250]; int lines,cnt,i; set_crash(); lines=cnt=i=0; sprintf(filename,"%s/%s", PLFILES, PL02x100_DICT); lines=count_lines(filename); srand(time(0)); cnt=rand()%lines; if (!(fp=fopen(filename,"r"))) return(PL02x100_DEFWORD); fscanf(fp,"%s\n",aword); while (!feof(fp)) { if (i==cnt) { fclose(fp); return aword; } ++i; fscanf(fp,"%s\n",aword); } fclose(fp); /* if no word was found, just return a generic word */ return(PL02x100_DEFWORD); }
/* * returns a word from a list for hangman. * this will save loading words into memory, and the list could be updated as and when * you feel like it */ void get_hang_word(char *aword) { char filename[80]; FILE *fp; int cnt, f; /* if no word is found, just return a generic word */ strcpy(aword, "hangman"); sprintf(filename, "%s/%s", MISCFILES, HANGDICT); cnt = count_lines(filename); if (!cnt) { return; } fp = fopen(filename, "r"); if (!fp) { return; } cnt = rand() % cnt; for (f = fscanf(fp, "%s\n", aword); f == 1; f = fscanf(fp, "%s\n", aword)) { if (!cnt--) { break; } } fclose(fp); }
struct TrainCatalog* TrainCatalogRead(const char* filename) { wlog("Reading TrainCatalog, NDIM=%d, from file: '%s'\n", NDIM, filename); wlog(" Counting lines\n"); size_t nlines = count_lines(filename); struct TrainCatalog* cat = TrainCatalogAlloc(nlines); wlog(" Reading %ld lines\n", nlines); FILE* fptr=fopen(filename,"r"); double* pdata = cat->pts->data; for (size_t i=0; i<nlines; i++) { if (!fscanf(fptr, "%lf %lf %lf", &cat->zspec[i], &cat->extra[i], &cat->weights[i])) { perror("Error reading from file: "); exit(1); } // note odd memory layout of the data array for (int dim=0; dim<NDIM; dim++) { if (!fscanf(fptr, "%lf", &pdata[i + nlines*dim])) { perror("Error reading from file: "); exit(1); } } } fclose(fptr); return cat; }
depthfile_t *load_depthfile(rgbcolor_t *colors, char *filename, int Z, int A) { FILE *in; depthfile_t *depthfile; char *line; int linenumber=0; int n_depths=count_lines(filename); if(n_depths==0) { return NULL; } in=fopen(filename, "r"); double depth, b, c, conc, e, f; int counts; int depth_i=0; if(!in) { fprintf(stderr, "No such file (%s)!\n", filename); return NULL; } depthfile=malloc(sizeof(depthfile_t)); depthfile->filename=calloc(strlen(filename)+1, sizeof(char)); strncat(depthfile->filename, filename, strlen(filename)); depthfile->Z=Z; depthfile->A=A; depthfile->n_depths=n_depths; depthfile->use_in_scaling=1; depthfile->plot=1; depthfile->depths=calloc(n_depths, sizeof(double)); depthfile->concentrations=calloc(n_depths, sizeof(double)); depthfile->counts=calloc(n_depths, sizeof(int)); depthfile->color=colors; line=calloc(MAX_LINE_LEN, sizeof(char)); while(fgets(line, MAX_LINE_LEN, in)) { linenumber++; if(*line == '#') continue; #ifdef OLD_FORMAT if(sscanf(line, "%lf %lf %lf %lf %lf\n", &depth, &b, &c, &conc, &e)==5) { depthfile->depths[depth_i]=depth; depthfile->concentrations[depth_i]=conc; depthfile->counts[depth_i]=0; depth_i++; } #else if(sscanf(line, "%lf %lf %lf %lf %lf %lf %i\n", &depth, &b, &c, &conc, &e, &f, &counts)==7) { depthfile->depths[depth_i]=depth; depthfile->concentrations[depth_i]=conc; depthfile->counts[depth_i]=counts; depth_i++; } #endif if(depth_i > n_depths) { fprintf(stderr, "Something odd at the depthfile, too many depths (was expecting %i)!\n", n_depths); break; } } fclose(in); free(line); return depthfile; }
static void emit_rewrite_diff(const char *name_a, const char *name_b, struct diff_tempfile *temp) { /* Use temp[i].name as input, name_a and name_b as labels */ int lc_a, lc_b; lc_a = count_lines(temp[0].name); lc_b = count_lines(temp[1].name); printf("--- %s\n+++ %s\n@@ -", name_a, name_b); print_line_count(lc_a); printf(" +"); print_line_count(lc_b); printf(" @@\n"); if (lc_a) copy_file('-', temp[0].name); if (lc_b) copy_file('+', temp[1].name); }
void display_colle(char nb, t_lines *lines, char separator) { my_putstr("[colle1-"); my_putchar('0' + nb); my_putstr("] "); my_put_nbr(my_strlen(get_line(0, lines)->buffer)); my_putchar(' '); my_put_nbr(count_lines(lines)); }
bool pgsRecord::remove_line(const USHORT &line) { if (line < count_lines()) { m_record.RemoveAt(line); return true; } return false; }
char *get_pos(int line, int column, t_lines *lines) { if (line <= count_lines(lines)) { if (column <= my_strlen(get_line(line, lines)->buffer)) return (&(get_line(line, lines)->buffer[column])); } return (0); }
/* Abre el archivo `filename` y crea un grafo con la lista de adyacencia * inscrita en el. Cada linea del archivo debe cumplir la siguiente expresion * regular (con BL como el BUFFER_LEN): '\d{1,BL}:( \d{1,BL})*\n' * Retorno: Un grafo G (descrito en structures.h)*/ struct graph *file_to_graph(const char * filename) { char ch = '\0', buffer[BUFFER_LEN] = ""; // Se puede calcular por el nro de lineas. unsigned int i, id, size, *tmp; struct list *last_list; struct node *last_node; struct graph *G; FILE *fp = fopen(filename, "r"); if(fp == NULL){ fprintf(stderr,"No se puede leer el archivo \"%s\".\n", filename); return NULL; } size = count_lines(fp); G = new_graph(size); IDC = (int*) calloc(size, sizeof(int)); ODC = (int*) malloc(size * sizeof(int)); while (1) { i = 0; do { ch = getc(fp); if (ch == EOF) { fclose(fp); return G; } else if (ch == ':') { id = atoi(buffer); last_list = new_list(); ch = getc(fp); if (ch == '\n'){ break; } i = 0; memset(buffer, 0, BUFFER_LEN); } else if (ch == ' ' || ch == '\n') { tmp = (int *) malloc(sizeof(int)); buffer[i] = '\0'; *tmp = atoi(buffer); list_add(last_list, tmp); IDC[*tmp]++; i = 0; memset(buffer, 0, BUFFER_LEN); } else { buffer[i++] = ch; } } while (ch != '\n'); ODC[id] = last_list->size; last_node = new_node(id, last_list); graph_add_node(G, last_node); last_node = NULL; last_list = NULL; } }
/** * Skip through the text to a matching "#endif" or "#else" or * "#elif*def". We do this when we are skipping code due to a failed * "#if*def" test. */ static char * skip_to_else_end(char * start) { char * scan = start; char * res; for (;;) { scan = next_directive(scan); switch (find_directive(scan)) { case DIR_ELSE: /* * We found an "else" directive for an "ifdef"/"ifndef" * that we were skipping over. Start processing the text. * Let the @code{DIR_ENDIF} advance the scanning pointer. */ ifdef_lvl++; /* FALLTHROUGH */ case DIR_ENDIF: { /* * We reached the end of the "ifdef"/"ifndef" (or transitioned to * process-the-text mode via "else"). Start processing the text. */ char * pz = strchr(scan, NL); if (pz != NULL) res = pz+1; else res = scan + strlen(scan); goto leave_func; } case DIR_IF: skip_if_block = true; scan = skip_to_endif(scan); skip_if_block = false; break; case DIR_IFDEF: case DIR_IFNDEF: scan = skip_to_endif(scan); break; default: /* * We either don't know what it is or we do not care. */ break; } /* switch (find_directive(scan)) */ } leave_func: cctx->scx_line += count_lines(start, res); return res; }
/** * Skip through the text to a matching "#endif". We do this when we * have processed the allowable text (found an "#else" after * accepting the preceding text) or when encountering a "#if*def" * while skipping a block of text due to a failed test. */ static char * skip_to_endif(char * start) { bool skipping_if = skip_if_block; char * scan = start; char * dirv_end; for (;;) { scan = next_directive(scan); switch (find_directive(scan)) { case DIR_ENDIF: { /* * We found the endif we are interested in */ char * pz = strchr(scan, NL); if (pz != NULL) dirv_end = pz+1; else dirv_end = scan + strlen(scan); goto leave_func; } case DIR_ELIF: if (! skip_if_block) AG_ABEND(ELIF_CONTEXT_MSG); break; case DIR_IF: skip_if_block = true; /* FALLTHROUGH */ case DIR_IFDEF: case DIR_IFNDEF: /* * We found a nested conditional, so skip to endif nested, too. */ scan = skip_to_endif(scan); skip_if_block = skipping_if; break; default: /* * We do not care what we found */ break; /* ignore it */ } /* switch (find_directive(scan)) */ } leave_func: cctx->scx_line += count_lines(start, dirv_end); return dirv_end; }
/* count how many lines in current directory input: no, may require to change inside the function body output: number of lines */ int count_lines() { vector<string> files{ "Main.cpp", "Code_Backup.cpp", "Leetcode.h", "Leetcode.cpp", "Solver.h", "Puzzle.h", "Puzzle.cpp", "Random.cpp", "QuadraticProbing.h", "SeparateChaining.h", "Sort.h", "LinkedList.h", "BinarySearchTree.h", "QueueAr.h", "StackLi.h", "StackAr.h", "Matrix.h", "String.h", "String.cpp", "Vector.h", "dsexceptions.h", "Test.cpp", "Utility.cpp", "DesignPatterns.cpp", "MyMath.cpp", "EigenIO.cpp", "Finance.cpp", "Finance.h", "Processor.cpp", "Data.cpp" }; return count_lines(files); }