oop string_join(oop strlist, oop sepstr) { size_t total_length = 0; size_t sep_length; int needsep = 0; oop pos; binary *result; char *c; if (!isbinary(sepstr)) die("string_join: sepstr is not a binary"); sep_length = oop_len(sepstr); for (pos = strlist; ispair(pos); pos = ((pair *) pos)->cdr) { oop s = ((pair *) pos)->car; if (!isbinary(s)) die("string_join: element of strlist is not a binary"); if (needsep) total_length += sep_length; total_length += oop_len(s); needsep = 1; } if (!isnil(pos)) die("string_join: strlist is not a list"); result = raw_alloc(sizeof(binary) + total_length); init_binary_header(result->header, TYPE_BINARY, total_length); c = result->data; needsep = 0; for (pos = strlist; ispair(pos); pos = ((pair *) pos)->cdr) { oop s = ((pair *) pos)->car; if (needsep) { memcpy(c, ((binary *) sepstr)->data, sep_length); c += sep_length; } memcpy(c, ((binary *) s)->data, oop_len(s)); c += oop_len(s); needsep = 1; } return result; }
/* copy a pattern string from the command buffer; return pointer to the copy */ static char *extract_pattern( const char **ibufpp, const int delimiter ) { static char *buf = 0; static int bufsz = 0; const char *nd = *ibufpp; int len; while( *nd != delimiter && *nd != '\n' ) { if( *nd == '[' ) { nd = parse_char_class( ++nd ); if( !nd ) { set_error_msg( "Unbalanced brackets ([])" ); return 0; } } else if( *nd == '\\' && *++nd == '\n' ) { set_error_msg( "Trailing backslash (\\)" ); return 0; } ++nd; } len = nd - *ibufpp; if( !resize_buffer( &buf, &bufsz, len + 1 ) ) return 0; memcpy( buf, *ibufpp, len ); buf[len] = 0; *ibufpp = nd; if( isbinary() ) nul_to_newline( buf, len ); return buf; }
int main(void) { int mode; unsigned char old[MINTEXT - 1]; int c; mode = 0; while ((c = getchar()) != EOF) { if (isbinary(c)) { if (mode == 0) putchar('\n'); mode = MINTEXT; } else { if (mode > 0) { if (istext(c)) { mode--; if (mode == 0) { int j; for (j = 0; j < MINTEXT-1; j++) putchar(old[j]); putchar(c); } else old[MINTEXT - mode - 1] = c; } else mode = MINTEXT; } else putchar(c); } } if (mode == 0) putchar('\n'); return 0; }
void R_load_XGAPheader(char **filename,int* num_rows,int* num_cols,int* isNumeric,int* pos){ // Rprintf("File to load: %s\n",filename[0]); rawfilestruct binfile; if(parsefile(filename[0],&binfile)){ char *matrixname,*investigationname,*colname,*rowname; uint newpos,nrows,ncols; uint deciortext; newpos = getname(binfile,&matrixname,1); newpos = getname(binfile,&investigationname,newpos); newpos = getname(binfile,&colname,newpos); newpos = getname(binfile,&rowname,newpos); deciortext = isbinary(binfile,newpos); // Rprintf("Matrix= %s\n",matrixname); // Rprintf("Numeric= %i\n",deciortext); newpos++; ncols = getncols(binfile,newpos); // Rprintf("Cols= %i\n",ncols); newpos = newpos+4; nrows = getnrows(binfile,newpos); // Rprintf("Rows= %i\n",nrows); *num_rows = nrows; *num_cols = ncols; *isNumeric = deciortext; newpos = newpos+4; (*pos) = newpos; freememory(binfile); }else{ Rprintf("Error opening file: %s",filename[0]); } } //R_load_XGAPheader
/* replace text matched by a pattern according to a substitution template; return pointer to the modified text */ static int replace_matching_text( const line_t *lp, const int gflags, const int snum ) { const int se_max = 30; /* max subexpressions in a regular expression */ regmatch_t rm[se_max]; char *txt = get_sbuf_line( lp ); char *eot; int i = 0, off = 0; char changed = 0; if( !txt ) return -1; if( isbinary() ) nul_to_newline( txt, lp->len ); eot = txt + lp->len; if( !regexec( global_pat, txt, se_max, rm, 0 ) ) { int matchno = 0; do { if( !snum || snum == ++matchno ) { changed = 1; i = rm[0].rm_so; if( !resize_buffer( &rbuf, &rbufsz, off + i ) ) return -1; if( isbinary() ) newline_to_nul( txt, rm[0].rm_eo ); memcpy( rbuf + off, txt, i ); off += i; off = apply_subst_template( txt, rm, off, global_pat->re_nsub ); if( off < 0 ) return -1; } else { i = rm[0].rm_eo; if( !resize_buffer( &rbuf, &rbufsz, off + i ) ) return -1; if( isbinary() ) newline_to_nul( txt, i ); memcpy( rbuf + off, txt, i ); off += i; } txt += rm[0].rm_eo; } while( *txt && ( !changed || ( ( gflags & GSG ) && rm[0].rm_eo ) ) && !regexec( global_pat, txt, se_max, rm, REG_NOTBOL ) ); i = eot - txt; if( !resize_buffer( &rbuf, &rbufsz, off + i + 2 ) ) return -1; if( i > 0 && !rm[0].rm_eo && ( gflags & GSG ) ) { set_error_msg( "Infinite substitution loop" ); return -1; } if( isbinary() ) newline_to_nul( txt, i ); memcpy( rbuf + off, txt, i ); memcpy( rbuf + off + i, "\n", 2 ); } return ( changed ? off + i + 1 : 0 ); }
static int isbinarystream(fz_buffer *buf) { int i; for (i = 0; i < buf->len; i++) if (isbinary(buf->data[i])) return 1; return 0; }
oop string_split_by_chars(oop s, oop charstr) { oop result = mknull(); size_t limit; pair *prev = NULL; char *separators; size_t separators_len; size_t tokstart; size_t pos; char *sdata; if (!isbinary(s)) die("string_split_by_chars: string is not a binary"); if (!isbinary(charstr)) die("string_split_by_chars: separators is not a binary"); separators = ((binary *) charstr)->data; separators_len = oop_len(charstr); sdata = ((binary *) s)->data; tokstart = pos = 0; limit = oop_len(s); while (pos < limit) { /* Find the first separator */ while (pos < limit && memchr(separators, sdata[pos], separators_len) == NULL) pos++; if (tokstart != pos) { binary *tok = raw_alloc(sizeof(binary) + (pos - tokstart)); pair *p = raw_cons(tok, mknull()); init_binary_header(tok->header, TYPE_BINARY, (pos - tokstart)); memcpy(tok->data, &sdata[tokstart], pos - tokstart); if (prev == NULL) { result = p; } else { prev->cdr = p; } prev = p; } /* Find the first nonseparator */ while (pos < limit && memchr(separators, sdata[pos], separators_len) != NULL) pos++; tokstart = pos; } return result; }
int bin_file(FILE *f) { char buf[BUFSIZ]; size_t m; int ret = 0; if (fseek(f, 0L, SEEK_SET) == -1) return 0; if ((m = fread(buf, 1, BUFSIZ, f)) == 0) return 0; if (isbinary(buf, m)) ret = 1; rewind(f); return ret; }
int gzbin_file(gzFile *f) { char buf[BUFSIZ]; int m; int ret = 0; if (gzseek(f, (z_off_t)0, SEEK_SET) == -1) return 0; if ((m = gzread(f, buf, BUFSIZ)) <= 0) return 0; if (isbinary(buf, m)) ret = 1; if (gzrewind(f) != 0) err(1, "gzbin_file"); return ret; }
/* return the address of the next line matching a pattern in a given direction. wrap around begin/end of editor buffer if necessary */ int get_matching_node_addr( const char **ibufpp, const char forward ) { regex_t *pat = get_compiled_pattern( ibufpp ); int addr = current_addr(); if( !pat ) return -1; do { addr = ( forward ? inc_addr( addr ) : dec_addr( addr ) ); if( addr ) { line_t *lp = search_line_node( addr ); char *s = get_sbuf_line( lp ); if( !s ) return -1; if( isbinary() ) nul_to_newline( s, lp->len ); if( !regexec( pat, s, 0, 0, 0 ) ) return addr; } } while( addr != current_addr() ); set_error_msg( "No match" ); return -1; }
/* add line matching a pattern to the global-active list */ char build_active_list( const char **ibufpp, const int first_addr, const int second_addr, const char match ) { regex_t *pat; line_t *lp; int addr; const char delimiter = **ibufpp; if( delimiter == ' ' || delimiter == '\n' ) { set_error_msg( "Invalid pattern delimiter" ); return 0; } if( !( pat = get_compiled_pattern( ibufpp ) ) ) return 0; if( **ibufpp == delimiter ) ++(*ibufpp); clear_active_list(); lp = search_line_node( first_addr ); for( addr = first_addr; addr <= second_addr; ++addr, lp = lp->q_forw ) { char *s = get_sbuf_line( lp ); if( !s ) return 0; if( isbinary() ) nul_to_newline( s, lp->len ); if( !regexec( pat, s, 0, 0, 0 ) == match && !set_active_node( lp ) ) return 0; } return 1; }
int mmbin_file(mmf_t *f) { /* XXX knows too much about mmf internals */ return isbinary(f->base, f->len < BUFSIZ ? f->len : BUFSIZ); }
oop basic_library_load_module(oop name) { if (!isbinary(name)) die("basic_library_load_module: not a string"); return load_module(((binary *) name)->data); }