char *token_to_string(Token *tok) { switch (tok->type) { case TTYPE_IDENT: return tok->sval; case TTYPE_PUNCT: case TTYPE_CHAR: { String *s = make_string(); string_append(s, tok->c); return get_cstring(s); } case TTYPE_INT: { String *s = make_string(); string_appendf(s, "%d", tok->ival); return get_cstring(s); } case TTYPE_STRING: { String *s = make_string(); string_appendf(s, "\"%s\"", tok->sval); return get_cstring(s); } default: error("internal error: unknown token type: %d", tok->type); } }
static void test_string(void) { String *s = make_string(); string_append(s, 'a'); assert_string("a", get_cstring(s)); string_append(s, 'b'); assert_string("ab", get_cstring(s)); string_appendf(s, "."); assert_string("ab.", get_cstring(s)); string_appendf(s, "%s", "0123456789"); assert_string("ab.0123456789", get_cstring(s)); }
/* special handling: two string arguments */ JNIEXPORT void JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_setClientCert( JNIEnv *jenv, jobject jobj, jstring jcert, jstring jsslkey) { struct libctx *ctx = getctx(jenv, jobj); const char *cert = NULL, *sslkey = NULL; if (ctx && !get_cstring(ctx->jenv, jcert, &cert) && !get_cstring(ctx->jenv, jsslkey, &sslkey)) openconnect_set_client_cert(ctx->vpninfo, cert, sslkey); release_cstring(ctx->jenv, jcert, cert); release_cstring(ctx->jenv, jsslkey, sslkey); return; }
/* special handling: multiple string arguments */ JNIEXPORT jint JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_setupTunDevice( JNIEnv *jenv, jobject jobj, jstring jarg0, jstring jarg1) { struct libctx *ctx = getctx(jenv, jobj); const char *arg0 = NULL, *arg1 = NULL; int ret = -ENOMEM; if (ctx && !get_cstring(ctx->jenv, jarg0, &arg0) && !get_cstring(ctx->jenv, jarg1, &arg1)) ret = openconnect_setup_tun_device(ctx->vpninfo, arg0, arg1); release_cstring(ctx->jenv, jarg0, arg0); release_cstring(ctx->jenv, jarg1, arg1); return ret; }
JNIEXPORT void JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_setMobileInfo( JNIEnv *jenv, jobject jobj, jstring jarg0, jstring jarg1, jstring jarg2) { struct libctx *ctx = getctx(jenv, jobj); const char *arg0 = NULL, *arg1 = NULL, *arg2 = NULL; if (ctx && !get_cstring(ctx->jenv, jarg0, &arg0) && !get_cstring(ctx->jenv, jarg1, &arg1) && !get_cstring(ctx->jenv, jarg2, &arg2)) openconnect_set_mobile_info(ctx->vpninfo, arg0, arg1, arg2); release_cstring(ctx->jenv, jarg0, arg0); release_cstring(ctx->jenv, jarg1, arg1); release_cstring(ctx->jenv, jarg2, arg2); }
static Token *make_ident(String *s) { Token *r = malloc(sizeof(Token)); r->type = TTYPE_IDENT; r->sval = get_cstring(s); return r; }
static Token *make_strtok(String *s) { Token *r = malloc(sizeof(Token)); r->type = TTYPE_STRING; r->sval = get_cstring(s); return r; }
bool forecast_from_bridge_dict(DictionaryIterator* dict, Forecast* forecast) { // TBD: Test results - J. Speicher (8/9/13) get_int16(dict, DUMMY_INTEGER_KEY, &forecast->dummy_integer); get_int16(dict, ANOTHER_DUMMY_INTEGER_KEY, &forecast->another_dummy_integer); get_cstring(dict, DUMMY_STRING_KEY, &forecast->dummy_string); return true; };
static char *get_caller_list(void) { String *s = make_string(); for (Iter *i = list_iter(functions); !iter_end(i);) { string_appendf(s, "%s", iter_next(i)); if (!iter_end(i)) string_appendf(s, " -> "); } return get_cstring(s); }
JNIEXPORT void JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_setCSDWrapper( JNIEnv *jenv, jobject jobj, jstring jarg0, jstring jarg1, jstring jarg2) { struct libctx *ctx = getctx(jenv, jobj); const char *arg0 = NULL, *arg1 = NULL, *arg2 = NULL; if (ctx && !get_cstring(ctx->jenv, jarg0, &arg0) && !get_cstring(ctx->jenv, jarg1, &arg1) && !get_cstring(ctx->jenv, jarg2, &arg2)) { openconnect_setup_csd(ctx->vpninfo, getuid(), 1, arg0); openconnect_set_csd_environ(ctx->vpninfo, "TMPDIR", arg1); openconnect_set_csd_environ(ctx->vpninfo, "PATH", arg2); } release_cstring(ctx->jenv, jarg0, arg0); release_cstring(ctx->jenv, jarg1, arg1); release_cstring(ctx->jenv, jarg2, arg2); }
static Token *read_number(char c) { String *s = make_string(); string_append(s, c); for (;;) { int c = getc(stdin); if (!isdigit(c) && c != '.') { ungetc(c, stdin); return make_number(get_cstring(s)); } string_append(s, c); } }
// Adds one MyString to another MyString MyString::operator+(MyString& lhs_string){ return MyString(get_cstring(), lhs_string.get_cstring()); }
static void *read_det_cov(void *vd) { int i,err; FILE *fptr; string *s; tokens *tok; void *tbuf; char *p,*p1,cc,*fname; struct contig *c,*ctg; struct tdc_par *tp; count *ct,ct1,ct2; struct lk_compress *lkc; u_int32_t x; tp=vd; ctg=tp->ctg; lkc=tp->lkc; while((fname=get_input_file())) { s=0; tok=0; tbuf=0; err=0; fptr=open_readfile(fname,&i,lkc); if(!fptr) return 0; printf("Reading detailed coverage file '%s'\n",fname); c=0; ct=0; x=0; while(!err) { s=fget_string(fptr,s,&tbuf); if(!s->len) break; tok=tokenize(get_cstring(s),'\t',tok); if(tok->n_tok==2) { p=tok->toks[0]; if(*p=='>') { if(!c || strcmp(c->name,p+1)) { HASH_FIND_STR(ctg,p+1,c); } if(c) { p=tok->toks[1]; x=strtoul(p,&p1,10)-1; if(p==p1 || *p1) { err=10; fprintf(stderr,"(a) Bad file format\n"); } ct=c->counts; } } } else if(c) { if(tok->n_tok!=1) { fprintf(stderr,"(b) Bad file format\n"); err=-1; } p=tok->toks[0]; while(x<c->size && (cc=*p++)) { if(cc>=33 && cc<=125) { cc-=33; ct1=(count)cc; } else if(cc==126) { ct1=(count)strtoul(p,&p1,16); p=p1+1; } else { fprintf(stderr,"(c) Bad file format (%d) '%s'\n",cc,tok->toks[0]); err=-1; } if(!err) { pthread_mutex_lock(&c->mut); ct2=ct[x]; if(ct2<MAX_COUNT) { if(MAX_COUNT-ct2<ct1) ct[x]=MAX_COUNT; else ct[x]=ct2+ct1; } pthread_mutex_unlock(&c->mut); x++; } } } } fclose(fptr); if(s) free_string(s); if(tok) free_tokens(tok); if(tbuf) free_fget_buffer(&tbuf); signal(SIGCHLD,SIG_DFL); while(waitpid(-1,&i,WNOHANG)>0); } return 0; }
static int read_ranges_file(char *fname,struct contig **contig_ptr,struct lk_compress *lkc) { int i,err; u_int32_t x1,x1a,x2,x3,x; struct contig *ctg,*c; struct range_blk *r,*r1; char *estr; FILE *fptr; string *s; tokens *tok; void *tbuf; s=0; tok=0; tbuf=0; err=0; fptr=open_readfile_and_check(fname,&i,lkc); printf("Reading ranges file '%s'\n",fname); ctg=0; while(!err) { s=fget_string(fptr,s,&tbuf); if(!s->len) break; tok=tokenize(get_cstring(s),'\t',tok); if(tok->n_tok>=3) { HASH_FIND_STR(ctg,tok->toks[0],c); if(!c) { c=lk_malloc(sizeof(struct contig)); c->name=strdup(tok->toks[0]); c->size=0; c->ranges=lk_malloc(sizeof(struct range_blk)); c->ranges->idx=0; c->ranges->next=0; c->tranges=0; c->counts=0; c->tcounts=0; c->tot_count=0; c->tot_tcount=0; c->tot_bases=0; c->tot_tbases=0; HASH_ADD_KEYPTR(hh,ctg,c->name,strlen(c->name),c); } r=c->ranges; if(r->idx==RANGE_BLK_SIZE) { r=lk_malloc(sizeof(struct range_blk)); r->idx=0; r->next=c->ranges; c->ranges=r; } x1=(u_int32_t)getlnumcolumn(tok,1,0,&estr); if(!estr) x2=(u_int32_t)getlnumcolumn(tok,2,0,&estr); if(estr || x1<1 || x2<x1) { err=1; fprintf(stderr,"Error in coordinate format: %s\n",estr); } else { r->name[r->idx]=getstrcolumn(tok,3,0,0); r->x1[r->idx]=x1; r->x2[r->idx++]=x2; } } } fclose(fptr); if(s) free_string(s); if(tok) free_tokens(tok); if(tbuf) free_fget_buffer(&tbuf); signal(SIGCHLD,SIG_DFL); x2=x3=0; for(c=ctg; c && !err; c=c->hh.next) { x1=0; r=c->ranges; c->ranges=0; while(r) { for(i=0; i<r->idx; i++) { if(r->x2[i]>x1) x1=r->x2[i]; } r1=r->next; r->next=c->ranges; c->ranges=r; r=r1; } c->size=x1; x1a=0; if(!x1) err=2; else { x2+=x1; c->counts=lk_malloc(sizeof(count)*c->size); for(i=0; i<(int)c->size; i++) c->counts[i]=NO_COUNT; for(r=c->ranges; r; r=r->next) { for(i=0; i<r->idx; i++) { for(x=r->x1[i]-1; x<r->x2[i]; x++) c->counts[x]=0; } } for(i=0; i<(int)c->size; i++) if(!c->counts[i]) x1a++; } c->tot_bases=x1a; x3+=x1a; } printf("total=%u, in ranges=%u\n",x2,x3); while(waitpid(-1,&i,WNOHANG)>0); *contig_ptr=err?0:ctg; return err; }
static int process_file(char *fname,struct contig *ctg,int block_size,int fmt,u_int64_t *number,struct lk_compress *lkc) { int i,k,err,sz,nn; FILE *fptr; string *s; tokens *tok; void *tbuf; char *p,*p1; struct match m; struct contig *c; count *ct; u_int32_t x,x1; s=0; tok=0; tbuf=0; err=0; nn=(fmt==GEM_FMT?1:0); fptr=open_readfile(fname,&i,lkc); if(!fptr) return -1; printf("Reading matches file '%s'\n",fname); while(!err) { s=fget_string(fptr,s,&tbuf); if(!s->len) break; tok=tokenize(get_cstring(s),'\t',tok); if(tok->n_tok>=4+nn) { p=tok->toks[2+nn]; i=0; if(!isdigit(*p)) continue; while(*p && !err) { k=(int)strtol(p,&p1,10); i+=k; if(i>1) break; if(*p1) { if(*p1!=':' && *p1!='+') err=5; else p1++; } p=p1; if(!k && i) break; } if(err) { fprintf(stderr,"Bad format for match number column '%s'\n",tok->toks[3]); err=0; continue; } else if(i==1) { p=parse_match(tok->toks[3+nn],fmt,&m); if(!p) { /* err=10;*/ fprintf(stderr,"Bad format for match\n"); continue; } else { if(*number) { (*number)--; if(!(*number)) { err=1; break; } } HASH_FIND_STR(ctg,m.ctg,c); if(c) { sz=strlen(tok->toks[1]); ct=c->counts; x=m.pos-1; if(block_size>1) { if(m.orientation) { for(i=0; i<sz; i++) { if(x<c->size) { x1=x/block_size; x1*=block_size; if(ct[x1]<MAX_COUNT) ct[x1]++; } if(!x) break; x--; } } else { for(i=0; i<sz && x<c->size; i++) { x1=x/block_size; x1*=block_size; if(ct[x1]<MAX_COUNT) ct[x1]++; x++; } } } else { if(m.orientation) { for(i=0; i<sz; i++) { if(x<c->size) { if(ct[x]<MAX_COUNT) ct[x]++; } if(!x) break; x--; } } else { for(i=0; i<sz && x<c->size; i++) { if(ct[x]<MAX_COUNT) ct[x]++; x++; } } } if((ct=c->tcounts)) { x=m.pos-1; if(block_size>1) { if(m.orientation) { for(i=0; i<sz; i++) { if(x<c->tsize) { x1=x/block_size; x1*=block_size; if(ct[x1]<MAX_COUNT) ct[x1]++; } if(!x) break; x--; } } else { for(i=0; i<sz && x<c->tsize; i++) { x1=x/block_size; x1*=block_size; if(ct[x1]<MAX_COUNT) ct[x1]++; x++; } } } else { if(m.orientation) { for(i=0; i<sz; i++) { if(x<c->tsize) { if(ct[x]<MAX_COUNT) ct[x]++; } if(!x) break; x--; } } else { for(i=0; i<sz && x<c->tsize; i++) { if(ct[x]<MAX_COUNT) ct[x]++; x++; } } } } } } } } } fclose(fptr); if(s) free_string(s); if(tok) free_tokens(tok); if(tbuf) free_fget_buffer(&tbuf); signal(SIGCHLD,SIG_DFL); while(waitpid(-1,&i,WNOHANG)>0); return err; }
static int read_target_file(char *fname,struct contig *ctg,int extend,struct lk_compress *lkc) { int i,err; u_int32_t x1,x2,x3,x,cs,x1a; struct contig *c; struct range_blk *r; char *estr; FILE *fptr; string *s; tokens *tok; void *tbuf; s=0; tok=0; tbuf=0; err=0; fptr=open_readfile_and_check(fname,&i,lkc); printf("Reading target file '%s'\n",fname); while(!err) { s=fget_string(fptr,s,&tbuf); if(!s->len) break; tok=tokenize(get_cstring(s),'\t',tok); if(tok->n_tok>=3) { HASH_FIND_STR(ctg,tok->toks[0],c); if(c) { r=c->tranges; if(!r) { r=c->tranges=lk_malloc(sizeof(struct range_blk)); r->idx=0; } else { if(r->idx==RANGE_BLK_SIZE) { r=lk_malloc(sizeof(struct range_blk)); r->idx=0; r->next=c->tranges; c->tranges=r; } } x1=(u_int32_t)getlnumcolumn(tok,1,0,&estr); if(!estr) x2=(u_int32_t)getlnumcolumn(tok,2,0,&estr); if(estr || x1<1 || x2<x1) { err=1; fprintf(stderr,"Error in coordinate format: %s\n",estr); } else { if(x1<=(unsigned int)extend) x1=1; else x1-=extend; x2+=extend; if(x1<1) x1=1; r->name[r->idx]=getstrcolumn(tok,3,0,0); r->x1[r->idx]=x1; r->x2[r->idx++]=x2; } } else { printf("Not found %s\n",tok->toks[0]); } } } fclose(fptr); if(s) free_string(s); if(tok) free_tokens(tok); if(tbuf) free_fget_buffer(&tbuf); signal(SIGCHLD,SIG_DFL); x2=x3=0; for(c=ctg; c && !err; c=c->hh.next) { if(!c->tranges) continue; x1=0; for(r=c->tranges; r; r=r->next) { for(i=0; i<r->idx; i++) { if(r->x2[i]>x1) x1=r->x2[i]; } } if(x1>c->size) x1=c->size; c->tsize=x1; x1a=0; if(!x1) { fprintf(stderr,"Illegal target range for %s\n",c->name); err=2; continue; } else { x2+=x1; /* printf("%s\t%d\n",c->name,c->tsize);*/ c->tcounts=lk_malloc(sizeof(count)*c->tsize); cs=c->tsize; for(i=0; i<(int)cs; i++) c->tcounts[i]=NO_COUNT; for(r=c->tranges; r; r=r->next) { for(i=0; i<r->idx; i++) { for(x=r->x1[i]-1; x<r->x2[i]; x++) { if(x>=cs) break; if(!c->counts[x]) c->tcounts[x]=0; } } } for(i=0; i<(int)cs; i++) if(!c->tcounts[i]) x1a++; } c->tot_tbases=x1a; x3+=x1a; } printf("total=%u, on target=%u\n",x2,x3); while(waitpid(-1,&i,WNOHANG)>0); return err; }