// the main function takes a single argument from the command line: the CSV file name int main(int argc, char **argv) { char line[MAX_LINE_SIZE]; struct vec3 data[MAX_VECTORS]; double vert[MAX_VECTORS]; double filtered[MAX_VECTORS]; int data_len = 0; if(argc < 2) { puts("No file provided."); return -1; } int fd = open(argv[1], O_RDONLY); // throw away the header line _getline(fd, line, sizeof(line)); // parse and load the vector data struct vec3 *ptr = data; while(_getline(fd, line, sizeof(line)) && data_len < MAX_VECTORS) { struct vec3 v = parse_vec3(line); //printf("x = %f, y = %f, z = %f\n", v.x, v.y, v.z); *ptr++ = v; data_len++; } printf("vectors read: %d\n", data_len); // calculate the gravity vector struct vec3 s = sum(data, data_len); double m = mag(s); //printf("mag = %f\n", m); struct vec3 g = scale(s, 1/m); printf("normalized gravity vector: %f %f %f\n", g.x, g.y, g.z); // reduce 3D data to 1D vertical acceleration for(int i = 0; i < data_len; i++) { vert[i] = dot(data[i], g); //printf("v = %f\n", vert[i]); } // bandpass filter 1-3 Hz filter(vert, data_len, filtered); //for(int i = 0; i < data_len; i++) printf("%f\n", filtered[i]); // calculate the thresholds double rms_val = rms(filtered, data_len); printf("rms: %f\n", rms_val); double threshold = rms_val * 0.5; // count the steps in the cleaned up signal int cnt = count_steps(filtered, data_len, threshold, -threshold); printf("cnt: %d\n", cnt); return 0; }
void start(void) { char * line = NULL; //init destructor if(atexit(end) != 0) { fprintf(stderr, "atexit: %s\n", strerror(errno)); exit(EXIT_FAILURE); } //read line, init width height line = _getline(stdin); if(line == NULL) { fprintf(stderr, "_getline: NULL pointer\n"); exit(EXIT_FAILURE); } if(sscanf(line ,"%d %d", &width, &height) != 2) { fprintf(stderr, "sscanf: unexpected number of items\n"); exit(EXIT_FAILURE); } free(line); line = NULL; //read line, init n_jump line = _getline(stdin); if(line == NULL) { fprintf(stderr, "_getline: NULL pointer\n"); exit(EXIT_FAILURE); } if(sscanf(line ,"%d", &n_jump) != 1) { fprintf(stderr, "sscanf: unexpected number of items\n"); exit(EXIT_FAILURE); } free(line); line = NULL; //read line, init x_bat y_bat line = _getline(stdin); if(line == NULL) { fprintf(stderr, "_getline: NULL pointer\n"); exit(EXIT_FAILURE); } if(sscanf(line ,"%d %d", &x_bat, &y_bat) != 2) { fprintf(stderr, "sscanf: unexpected number of items\n"); exit(EXIT_FAILURE); } free(line); line = NULL; }
main() { int i, j, len, offset; char line[MAXLINE]; while ((len = _getline(line, MAXLINE)) > 0) { for (i = 0; i < len; ++i) { if (i % MAXLENGTH == 0 && i != 0) { j = 0; offset = i; while (offset > 0 && line[offset] != ' ' && line[offset] != '\t') { ++j; offset = i - j; } if (offset != 0) line[offset] = '\n'; } } printf("%s", line); } }
int main () { char line[MAXLINE]; int i, j; while (_getline(line, MAXLINE) > 0){ for (i = 0; line[i] != '\0'; i++){ if (line[i] != ' ') putchar(line[i]); else { j = i + 1; while (line[j] == ' ') j++; i = tab(line, i, j); while (i < j){ putchar(line[i]); i++; } i--; } } } return 0; }
/* read string */ void read_str(char *s) { char ss[255]; size_t limit = 80; MESSAGE msg; int st; if (has_network_socket()) { msg.msg_type = MSG_GRAPH; msg.param.pword[1] = GraphRes; msg.param.pword[0] = GRAPH_READSTR; send_to_graph(&msg); while (TRUE) { st = receive_message(network_socket, &msg); if (st == 0) { if ((msg.msg_type == MSG_GRAPH) && (msg.param.pword[0] == GRAPH_READSTR_RESPONSE)) { strcpy(ss, msg.param.pstr); break; } } } strcpy(s, ss); } else { char *empty_str_pointer =NULL; _getline(empty_str_pointer, &limit , stdin); strcpy(s,empty_str_pointer); } }
int main(int argc, char *argv[]) { char line[MAXLEN]; getopt(argc, argv); while (_getline(line, MAXLEN) > 0) printline(line); return 0; }
int main(int argc, char **argv) { int len; char line[MAXLINE]; while ( (len = _getline(line, MAXLINE)) != 0) { if (len > 80) { printstr(line); } } }
main() { int len; char line[MAXLINE]; while ((len = _getline(line, MAXLINE)) > 0) { if (len > 80) printf("%s", line); } return 0; }
int main() { char current_line[MAX_LINE] = { '\0' }; uint32_t length = 0; while (_getline(current_line, &length) > 0) { char reversed_line[MAX_LINE] = { '\0' }; _reverse(current_line, reversed_line); printf("%s\n", reversed_line); } return 0; }
int main() { int len; // current line length char line[MAXLINE]; // line while ((len = _getline(line, MAXLINE)) > 0) { if (len > MINPRINTLEN) { printf("%s\n", line); } } return 0; }
int main () { char line[MAXLINE]; int pos, i; while (_getline(line, MAXLINE) > 0) { for (i = 0, pos= 0; line[i] != '\0'; i++, pos++) { if (line[i] == '\t') pos = tab(pos); else putchar(line[i]); } } }
char *readline(void) { char buf[MAXLEN]; int len; char *line; len = _getline(buf, MAXLEN); if (len == 0) return NULL; line = malloc((len + 1) * sizeof(char)); strcpy(line, buf); return line; }
int main(void) { int len; /* current line length */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ max = 0; while ((len = _getline(line, MAXLINE)) > 0) if (len > max) { max = len; copy(longest, line); } if (max > 0) printf("%d: %s\n", max, longest); return 0; }
/* readlines: read input lines */ int readlines(char *lineptr[], int maxlines) { int len, nlines; char *p, line[MAXLEN]; nlines = 0; while ((len = _getline(line, MAXLEN)) > 0) if (nlines >= maxlines || (p = alloc(len)) == NULL) return -1; else { line[len-1] = '\0'; /* delete newline */ strcpy(p, line); lineptr[nlines++] = p; } return nlines; }
main() { int i, j, k, w_len; char line[MAXLINE]; while (_getline(line, MAXLINE) > 0) for (i = w_len = 0; line[i] != '\0'; ++i) if (line[i] == '\t') { k = TABSIZE - (w_len % TABSIZE); for (j = 0; j < k; ++j) { putchar(' '); ++w_len; } } else { putchar(line[i]); ++w_len; }; }
/** * @brief [brief description] * @details [long description] * * @param lineptr [description] * @param maxlines [description] * * @return [description] */ int readlines(char *lineptr[], int maxlines, char *buffer){ int len, nlines; char line[MAXLEN]; char *p = buffer; nlines = 0; while ((len = _getline(line, MAXLEN)) > 0){ if (nlines >= maxlines){ return -1; } else { line[len-1] = '\0'; // remove string end strcpy(p, line); lineptr[nlines++] = p; p += len; } } return nlines; }
bool Document::read(std::istream& in) { if (mProcessed) return false; token::Container *tokens=dynamic_cast<token::Container*>(mTokenContainer.get()); assert(tokens!=0); std::string line; TokenGroup tgt; while (_getline(in, line)) { if (isBlankLine(line)) { tgt.push_back(TokenPtr(new token::BlankLine(line))); } else { tgt.push_back(TokenPtr(new token::RawText(line))); } } tokens->appendSubtokens(tgt); return true; }
int main() { int len; int max; char line[MAXLINE]; char longest[MAXLINE]; max = 0; while ((len = _getline(line, MAXLINE)) > 0) { if (len > max) { max = len; copy(longest, line); } } if (max > 0) { printf("\n\n%d chars: %s\n", max, longest); } }
/* read line */ void read_line() { MESSAGE msg; int st; if (has_network_socket()) { msg.msg_type = MSG_GRAPH; msg.param.pword[1] = GraphRes; msg.param.pword[0] = GRAPH_READLN; send_to_graph(&msg); while (TRUE) { st = receive_message(network_socket, &msg); if (st == 0) if ((msg.msg_type == MSG_GRAPH) && (msg.param.pword[0] == GRAPH_READLN_RESPONSE)) break; } } else { size_t limit = 255; char s[limit]; _getline((char**)&s, &limit , stdin); } }
int readlines(char *lineptr[], int maxlines) { int len, nlines; char *p, line[MAXLEN]; OPEN_FD(); nlines = 0; while ((len = _getline(line, MAXLEN)) > 1) if (nlines >= MAXLINES ) { CLOSE_FD(); return nlines*(-1) - 1; } else if(!(p=malloc(len))) { CLOSE_FD(); return nlines*(-1) - 1; } else { line[len - 1] = '\0'; strcpy(p, line); int ans; int ff=myAtoi(p,&ans); if(numeric) { myItoa(ans,p); } if(numeric && ff==-1) { drop_error_string_format(); CLOSE_FD(); free(p); strcpy(line,""); return nlines*(-1) - 1; } lineptr[nlines++] = p; } CLOSE_FD(); return nlines; }
int main(int argc, char *argv[]) { int i, c; char w[MAXLEN], name[MAXLEN]; char defn[MAXLEN * 15]; for (c = getword(w, MAXLEN); c != EOF; c = getword(w, MAXLEN)) { if (c == DEFINE){ c = getword(w, MAXLEN); if (c != NAME) printf ("error: name spected\n"); strcpy(name, w); _getline(defn, MAXLEN * 15); install(name, defn); } } for (i = 0; i < HASHSIZE; ++i) display_entry(i); printf ("finished\n"); return 0; }
/** * reverse polish calculator\ * 1 2 - 4 5 + * == (1 - 2) * (4 + 5) */ int main (void) { int type; double op2; char s[MAXOP]; /** signal that we`ve printed last variable and don`t want to show pop() */ int last_var_printed = 0; clear_variables(); while (_getline(line, MAXLINE) != 0) { line_index = 0; // while ((type = getop(s)) != EOF) { while ((type = getop(s)) != '\0') { switch(type) { case NUMBER: push(atof(s)); break; case FUNCTION_OR_VARIABLE: process_function_or_variable(s); break; case ENDSTRING: break; case '+': push(pop() + pop()); break; case '*': push(pop() * pop()); break; case '-': /** first pop second operand*/ op2 = pop(); push(pop() - op2); break; case '/': op2 = pop(); if (op2 != 0.0) push(pop() / op2); else printf("error: division by zero\n"); break; case '%': op2 = pop(); if (op2 != 0.0) push(fmod(pop(), op2)); else printf("error: division by zero\n"); break; case '?': stack_show(); break; case '#': stack_duplicate(); break; case '~': stack_swap(); break; case '!': stack_clear(); break; case '=': /** get '=' from stack - it`s not used */ pop(); /** next get real value */ variables[var_pos].value = pop(); last_var.value = variables[var_pos].value; push(last_var.value); break; case '$': printf("last added value: %s = %f\n", last_var.name, last_var.value); last_var_printed = 1; break; case '\n': if (last_var_printed == 0) printf("\t%.8f\n", pop()); else last_var_printed = 0; break; default: printf("error1: unknown command \'%s\'\n", s); break; } } } return 0; }
int EstompageImage::getline ( uint8_t* buffer, int line ) { if ( !estompage ) { generate(); } return _getline ( buffer, line ); }
int main(void) { int x_min = 0, y_min = 0, x_max = 0, y_max = 0; char * direction = NULL; start(); x_max = width; y_max = height; while(true) { //read direction direction = _getline(stdin); if(direction == NULL) { fprintf(stderr, "_getline: NULL pointer\n"); exit(EXIT_FAILURE); } if(strlen(direction)>1) { //switch case 0 switch(direction[0]) { case 'U': y_max = y_bat; y_bat = y_min+(y_bat-y_min)/2; break; case 'D': y_min = y_bat; y_bat = 1+y_bat+(y_max-y_bat)/2; if(y_bat == y_max) y_bat--; break; case 'L': x_max = x_bat; x_bat = x_min+(x_bat-x_min)/2; break; case 'R': x_min = x_bat; x_bat = 1+x_bat+(x_max-x_bat)/2; if(x_bat == x_max) x_bat--; break; default: fprintf(stderr,"main: unknow direction\n"); return EXIT_FAILURE; } if(strlen(direction)>2) { //switch case 1 switch(direction[1]) { case 'U': y_max = y_bat; y_bat = y_min+(y_bat-y_min)/2; break; case 'D': y_min = y_bat; y_bat = 1+y_bat+(y_max-y_bat)/2; if(y_bat == y_max) y_bat--; break; case 'L': x_max = x_bat; x_bat = x_min+(x_bat-x_min)/2; break; case 'R': x_min = x_bat; x_bat = 1+x_bat+(x_max-x_bat)/2; if(x_bat == x_max) x_bat--; break; default: fprintf(stderr,"main: unknow direction\n"); return EXIT_FAILURE; } } } free(direction); direction = NULL; fprintf(stdout, "%d %d\n", x_bat, y_bat); } return EXIT_SUCCESS; }
static const char * any2ppm_daemon (void) { int timeout = TIMEOUT; struct pollfd pfd; int sk, fd; long flags; struct sockaddr_un addr; char *line = NULL; size_t len = 0; #ifdef SIGPIPE signal (SIGPIPE, SIG_IGN); #endif /* XXX racy! */ if (getenv ("ANY2PPM_FORCE") == NULL && any2ppm_daemon_exists ()) return "any2ppm daemon already running"; unlink (SOCKET_PATH); sk = socket (PF_UNIX, SOCK_STREAM, 0); if (sk == -1) return "unable to create socket"; memset (&addr, 0, sizeof (addr)); addr.sun_family = AF_UNIX; strcpy (addr.sun_path, SOCKET_PATH); if (bind (sk, (struct sockaddr *) &addr, sizeof (addr)) == -1) { close (sk); return "unable to bind socket"; } flags = fcntl (sk, F_GETFL); if (flags == -1 || fcntl (sk, F_SETFL, flags | O_NONBLOCK) == -1) { close (sk); return "unable to set socket to non-blocking"; } if (listen (sk, 5) == -1) { close (sk); return "unable to listen on socket"; } /* ready for client connection - detach from parent/terminal */ if (getenv ("ANY2PPM_NODAEMON") == NULL && daemonize () == -1) { close (sk); return "unable to detach from parent"; } if (! write_pid_file ()) { close (sk); return "unable to write pid file"; } if (getenv ("ANY2PPM_TIMEOUT") != NULL) { timeout = atoi (getenv ("ANY2PPM_TIMEOUT")); if (timeout == 0) timeout = -1; if (timeout > 0) timeout *= 1000; /* convert env (in seconds) to milliseconds */ } pfd.fd = sk; pfd.events = POLLIN; pfd.revents = 0; /* valgrind */ while (poll (&pfd, 1, timeout) > 0) { while ((fd = accept (sk, NULL, NULL)) != -1) { if (_getline (fd, &line, &len) != -1) { char *argv[10]; if (split_line (line, argv, ARRAY_LENGTH (argv)) > 0) { const char *err; err = convert (argv, fd); if (err != NULL) { FILE *file = fopen (".any2ppm.errors", "a"); if (file != NULL) { fprintf (file, "Failed to convert '%s': %s\n", argv[0], err); fclose (file); } } } } close (fd); } } close (sk); unlink (SOCKET_PATH); unlink (SOCKET_PATH ".pid"); free (line); return NULL; }
ini_t *ini_load(const char *path) { FILE *fp = fopen(path, "r"); if (fp == NULL) return NULL; struct ini_section *head = NULL; struct ini_section *prev = NULL; struct ini_section *curr = NULL; struct ini_arg *arg_curr = NULL; struct ini_arg *arg_prev = NULL; char *line = NULL; size_t n = 0; ssize_t len = 0; while ((len = _getline(&line, &n, fp)) != -1) { char *s = line; if (is_comment(&s)) continue; len = strlen(s); if (len >= 3 && s[0] == '[' && s[len - 1] == ']') { char *name = s + 1; while (isspace(*name)) ++name; char *name_end = s + len - 1; *name_end-- = '\0'; while (isspace(*name_end)) *name_end-- = '\0'; if ((curr = find_section(head, name)) == NULL) { if ((curr = create_section(head, name)) == NULL) { free(line); return NULL; } if (head == NULL) head = curr; if (prev != NULL) prev->next = curr; prev = curr; arg_prev = NULL; } else { arg_prev = curr->args; while (arg_prev->next != NULL) arg_prev = arg_prev->next; } continue; } char *delimiter = strchr(s, '='); if (delimiter == NULL) continue; *delimiter = '\0'; char *name = s; char *name_end = delimiter - 1; while (isspace(*name_end)) *name_end-- = '\0'; char *value = delimiter + 1; while (isspace(*value)) value++; if (curr == NULL) { if ((curr = create_section(head, "global")) == NULL) { free(line); return NULL; } if (head == NULL) head = curr; prev = curr; arg_prev = NULL; } if ((arg_curr = find_arg(curr, name)) == NULL) { arg_curr = create_arg(head, name, value); if (arg_curr == NULL) { free(line); return NULL; } if (arg_prev) arg_prev->next = arg_curr; if (curr->args == NULL) curr->args = arg_curr; arg_prev = arg_curr; } else { char *old_value = arg_curr->value; if ((arg_curr->value = strdup(value)) == NULL) { ini_free(head); free(line); return NULL; } free(old_value); } } free(line); fclose(fp); if (head == NULL) { if ((head = calloc(1, sizeof(struct ini_section))) == NULL) return NULL; } ini_print(head); return head; }
/* * _get_command - get a command from the user * OUT argc - location to store count of arguments * OUT argv - location to store the argument list */ static int _get_command (int *argc, char **argv) { char *in_line; static char *last_in_line = NULL; int i, in_line_size; static int last_in_line_size = 0; *argc = 0; #if HAVE_READLINE in_line = readline ("sacctmgr: "); #else in_line = _getline("sacctmgr: "); #endif if (in_line == NULL) { exit_flag = 2; return 0; } else if (xstrncmp (in_line, "#", 1) == 0) { free (in_line); return 0; } else if (xstrcmp (in_line, "!!") == 0) { free (in_line); in_line = last_in_line; in_line_size = last_in_line_size; } else { if (last_in_line) free (last_in_line); last_in_line = in_line; last_in_line_size = in_line_size = strlen (in_line); } #if HAVE_READLINE add_history(in_line); #endif /* break in_line into tokens */ for (i = 0; i < in_line_size; i++) { bool double_quote = false, single_quote = false; if (in_line[i] == '\0') break; if (isspace ((int) in_line[i])) continue; if (((*argc) + 1) > MAX_INPUT_FIELDS) { /* bogus input line */ exit_code = 1; fprintf (stderr, "%s: can not process over %d words\n", command_name, input_words); return E2BIG; } argv[(*argc)++] = &in_line[i]; for (i++; i < in_line_size; i++) { if (in_line[i] == '\042') { double_quote = !double_quote; continue; } if (in_line[i] == '\047') { single_quote = !single_quote; continue; } if (in_line[i] == '\0') break; if (double_quote || single_quote) continue; if (isspace ((int) in_line[i])) { in_line[i] = '\0'; break; } } } return 0; }
int parse_form_encoded(llist* entries) { long content_length; entrytype entry; node* window; FILE *uploadfile = NULL; char *uploadfname, *tempstr, *boundary; char *buffer = (char *)malloc(sizeof(char) * BUFSIZ + 1); char *prevbuf = (char *)malloc(sizeof(char) + BUFSIZ + 1); short isfile,done,start; int i,j; int bytesread,prevbytesread=0; int buffersize; int numentries = 0; #ifdef WINDOWS setmode(fileno(stdin), O_BINARY); /* define stdin as binary */ _fmode = BINARY; /* default all file I/O as binary */ #endif if (CONTENT_LENGTH != NULL) content_length = atol(CONTENT_LENGTH); else return 0; /* get boundary */ tempstr = newstr(CONTENT_TYPE); boundary = strstr(tempstr,"boundary="); boundary += (sizeof(char) * 9); /* create list */ list_create(entries); window = entries->head; /* ignore first boundary; this isn't so robust; improve it later */ _getline(buffer,BUFSIZ); /* now start parsing */ while ((bytesread=_getline(buffer,BUFSIZ)) != 0) { start = 1; /* this assumes that buffer contains no binary characters. if the buffer contains the first valid header, then this is a safe assumption. however, it should be improved for robustness sake. */ buffer[bytesread] = '\0'; tempstr = newstr(buffer); tempstr += (sizeof(char) * 38); /* 38 is header up to name */ entry.name = tempstr; entry.value = (char *)malloc(sizeof(char) * BUFSIZ + 1); buffersize = BUFSIZ; strcpy(entry.value,""); while (*tempstr != '"') tempstr++; *tempstr = '\0'; if (strstr(buffer,"filename=\"") != NULL) { isfile = 1; tempstr = newstr(buffer); tempstr = strstr(tempstr,"filename=\""); tempstr += (sizeof(char) * 10); if (strlen(tempstr) >= BUFSIZ) entry.value = (char *) realloc(entry.value, sizeof(char) * strlen(tempstr)+1); entry.value = tempstr; while (*tempstr != '"') tempstr++; *tempstr = '\0'; /* Netscape's Windows browsers handle paths differently from its UNIX and Mac browsers. It delivers a full path for the uploaded file (which it shouldn't do), and it uses backslashes rather than forward slashes. No need to worry about Internet Explorer, since it doesn't support HTTP File Upload at all. */ if (strstr(lower_case(HTTP_USER_AGENT),"win") != 0) { tempstr = strrchr(entry.value, '\\'); if (tempstr) { tempstr++; entry.value = tempstr; } } window = list_insafter(entries,window,entry); numentries++; uploadfname = (char *)malloc(strlen(UPLOADDIR)+strlen(entry.value)+2); #ifdef WINDOWS sprintf(uploadfname,"%s\\%s",UPLOADDIR,entry.value); #else sprintf(uploadfname,"%s/%s",UPLOADDIR,entry.value); #endif if ( (uploadfile = fopen(uploadfname,"w")) == NULL) { /* null filename; for now, just don't save info. later, save to default file */ isfile = 0; } } else isfile = 0; /* ignore rest of headers and first blank line */ while (_getline(buffer, BUFSIZ) > 1) { /* DOS style blank line? */ if ((buffer[0] == '\r') && (buffer[1] == '\n')) break; } done = 0; j = 0; while (!done) { bytesread = _getline(buffer,BUFSIZ); buffer[bytesread] = '\0'; if (bytesread && strstr(buffer,boundary) == NULL) { if (start) { i = 0; while (i < bytesread) { prevbuf[i] = buffer[i]; i++; } prevbytesread = bytesread; start = 0; } else { /* flush buffer */ i = 0; while (i < prevbytesread) { if (isfile) fputc(prevbuf[i],uploadfile); else { if (j > buffersize) { buffersize += BUFSIZ; entry.value = (char *) realloc(entry.value, sizeof(char) * buffersize+1); } entry.value[j] = prevbuf[i]; j++; } i++; } /* buffer new input */ i = 0; while (i < bytesread) { prevbuf[i] = buffer[i]; i++; } prevbytesread = bytesread; } } else { done = 1; /* flush buffer except last two characters */ i = 0; while (i < prevbytesread - 2) { if (isfile) fputc(prevbuf[i],uploadfile); else { if (j > buffersize) { buffersize += BUFSIZ; entry.value = (char *) realloc(entry.value, sizeof(char) * buffersize+1); } entry.value[j] = prevbuf[i]; j++; } i++; } } } if (isfile) fclose(uploadfile); else { entry.value[j] = '\0'; window = list_insafter(entries,window,entry); numentries++; j = 0; } } return numentries; }