Exemplo n.º 1
0
static trans_node * make_trans_node( const char * chromosome, const char * translation )
{
    trans_node * res = calloc( 1, sizeof * res );
    if ( res != NULL )
    {
        StringInitCString( &(res->chromosome), chromosome );
        StringInitCString( &(res->translation), translation );
    }
    return res;
}
Exemplo n.º 2
0
LIB_EXPORT rc_t CC KCurlRequestAddField( struct KCurlRequest *self, const char * name, const char * value )
{
    String s_name, s_value;

    if ( self == NULL )
        return RC( rcNS, rcFile, rcReading, rcSelf, rcNull );
    if ( name == NULL || name[ 0 ] == 0 || value == NULL || value[ 0 ] == 0 )
        return RC( rcNS, rcFile, rcReading, rcParam, rcNull );

    StringInitCString( &s_name, name );
    StringInitCString( &s_value, value );
    return KCurlRequestAddSField( self, &s_name, &s_value );
}
Exemplo n.º 3
0
/* Make
 *  make an initialized structure
 *  NB - does NOT attach reference to dir, but steals it
 */
static
rc_t KDatabaseMake ( KDatabase **dbp, const KDirectory *dir,
    const char *path, KMD5SumFmt *md5, bool read_only )
{
    KDatabase *db;
    rc_t rc = 0;

    assert ( dbp != NULL );
    assert ( path != NULL );

    db = malloc ( sizeof * db + strlen ( path ) );
    if ( db == NULL )
    {
        * dbp = NULL;
        return RC ( rcDB, rcDatabase, rcConstructing, rcMemory, rcExhausted );
    }

    memset ( db, 0, sizeof * db );
    db -> dir = ( KDirectory* ) dir;
    db -> md5 = md5;
    rc = KMD5SumFmtAddRef ( md5 );
    db -> use_md5 = ( md5 == NULL ) ? false : true;
    KRefcountInit ( & db -> refcount, 1, "KDatabase", "make", path );
    db -> opencount = 1;
    db -> read_only = read_only;

    strcpy ( db -> path, path );

    db->sym.u.obj = db;
    StringInitCString (&db->sym.name, db->path);
    db->sym.type = kptDatabase;

    * dbp = db;
    return rc;
}
Exemplo n.º 4
0
static
rc_t KDyldVTryLoadLib ( KDyld *self, KDylib **lib,
    const KDirectory *dir, const char *path, va_list args )
{
    rc_t rc;

    const KSysDir *sdir = KDirectoryGetSysDir ( dir );
    if ( sdir == NULL )
        rc = RC ( rcFS, rcDylib, rcLoading, rcDirectory, rcIncorrect );
    else
    {
        char real [ PATH_MAX ];
        rc = KSysDirVRealPath ( sdir, real, sizeof real, path, args );
        if ( rc == 0 )
        {
            String pstr;
            StringInitCString ( & pstr, real );

            rc = KDylibMake ( lib, & pstr );
            if ( rc == 0 )
            {
                rc = KDyldLoad ( self, * lib, real );
                if ( rc == 0 )
                    return 0;

                free ( * lib );
            }
        }
    }

    * lib = NULL;

    return rc;
}
Exemplo n.º 5
0
/* Make
 */
static
rc_t KSymAddrMake ( KSymAddr **symp,
    const KDylib *lib, const char *name )
{
    void *addr = dlsym ( lib -> handle, name );
    const char *estr = dlerror();

    if ( addr == NULL  &&  builtins != NULL )
    {
        String   builtin_name;
        KSymbol *builtin_sym;
        StringInitCString ( & builtin_name, name );
        builtin_sym = KSymTableFind ( builtins, & builtin_name );
        if ( builtin_sym != NULL ) {
            addr = builtin_sym -> u . obj;
        }
    }
    
    if ( addr != NULL || estr == NULL )
    {
        KSymAddr *sym = malloc ( sizeof * sym );
        if ( sym == NULL )
            return RC ( rcFS, rcDylib, rcConstructing, rcMemory, rcExhausted );

        sym -> lib = KDylibAttach ( lib );
        sym -> addr = addr;
        KRefcountInit ( & sym -> refcount, 1, "KSymAddr", "make", name );
        * symp = sym;
        return 0;
    }

    * symp = NULL;
    return RC ( rcFS, rcDylib, rcSelecting, rcName, rcNotFound );
}
Exemplo n.º 6
0
static rc_t build_full_url( char ** s )
{
    rc_t rc;
    String prefix, accession;
    const String * full_url;

    StringInitCString( &prefix, ENTREZ_URL );
    StringInitCString( &accession, *s );
    rc = StringConcat ( &full_url, &prefix, &accession );
    if ( rc == 0 )
    {
        free( *s );
        *s = string_dup_measure ( full_url->addr, NULL );
        StringWhack ( full_url );
    }
    return rc;
}
Exemplo n.º 7
0
LIB_EXPORT rc_t CC KCurlRequestAddFields( struct KCurlRequest *self, const char * fields )
{
    String s;

    if ( self == NULL )
        return RC( rcNS, rcFile, rcReading, rcSelf, rcNull );
    if ( fields == NULL || fields[ 0 ] == 0 )
        return RC( rcNS, rcFile, rcReading, rcParam, rcNull );

    StringInitCString( &s, fields );
    return KCurlRequestAddSFields( self, &s );
}
Exemplo n.º 8
0
rc_t get_slice( const Args * args, const char *option, slice ** slice )
{
	const char * value;
	rc_t rc = get_charptr( args, option, &value );
	if ( rc == 0 && value != NULL )
	{
		String S;
		StringInitCString( &S, value );
		*slice = make_slice_from_str( &S );
	}
    else
        *slice = NULL;
	return rc;
}
Exemplo n.º 9
0
rc_t make_column_typelist ( const BSTree *columns,
    const char *col, uint32_t *dflt_idx, KNamelist **typedecls )
{
    VNamelist *list;
    rc_t rc = VNamelistMake ( & list, 8 );
    if ( rc == 0 )
    {
        uint32_t idx;
        const VColumnRef *first;

        String col_name;
        StringInitCString ( & col_name, col );

        first = ( const VColumnRef* )
            BSTreeFind ( columns, & col_name, VColumnRefCmpString );
        if ( first != NULL )
        {
            const VColumnRef *cref = ( const VColumnRef* ) BSTNodePrev ( & first -> n );
            while ( cref != NULL && StringEqual ( & first -> name, & cref -> name ) )
            {
                first = cref;
                cref = ( const VColumnRef* ) BSTNodePrev ( & cref -> n );
            }

            for ( cref = first, idx = 0; ; ++ idx )
            {
                rc = VNamelistAppend ( list, cref -> typedecl );
                if ( rc != 0 )
                    break;

                if ( cref -> dflt )
                    * dflt_idx = idx;

                cref = ( const VColumnRef* ) BSTNodeNext ( & cref -> n );
                if ( cref == NULL || ! StringEqual ( & first -> name, & cref -> name ) )
                    break;
            }
        }

        if ( rc == 0 )
            rc = VNamelistToNamelist ( list, typedecls );

        VNamelistRelease ( list );
    }

    return rc;
}
Exemplo n.º 10
0
void TrieRead(bool swap)
{
  struct stat sbuf;
  FILE *fp;
  PTrie *pt;
  size_t siz;
  char *fbuf;
  char buf[1024];
  String key;
  int len;
  rc_t rc;
  PTNode ptn;
  uint32_t id;
  size_t count;

  if (stat("/tmp/foo", &sbuf)) {
    return;
  }
  siz = sbuf.st_size;
  fbuf = malloc(siz+1);
  fp = fopen("/tmp/foo", "r");
  count = fread(fbuf, 1, siz, fp);
  if (count != siz) {
    fprintf(stderr, "Only read %ld of %ld\n", count, siz);
    return;
  }
  fbuf[siz] = '\0';
  rc = PTrieMake(&pt, fbuf, siz, swap);
  if (rc != 0 || pt == NULL) {
    fprintf(stderr, "PTrieMake failed.\n");
    return;
  }
  
  while (NULL != fgets(buf, 1024, stdin)) {
    len = strlen(buf);
    buf[--len] = '\0';
    StringInitCString( &key, buf );

    id = PTrieFind(pt, &key, &ptn, NULL, NULL);
    if ( id == 0 )
      fprintf(stderr, "not found\n");
    else
      fprintf(stderr, "id found: %d - '%.*s'\n", id, (int) ptn.data.size, ptn.data.addr);
  }
}  
Exemplo n.º 11
0
rc_t get_slices( const Args * args, const char *option, Vector * slices )
{
    uint32_t count, i;
    rc_t rc = ArgsOptionCount( args, option, &count );
    for ( i = 0; i < count && rc == 0; ++i )
    {
        const char * value;
        rc = ArgsOptionValue( args, option, i, ( const void ** )&value );
        if ( rc == 0 && value != NULL )
        {
            String S;
            StringInitCString( &S, value );
            {
                slice * s = make_slice_from_str( &S );
                rc = VectorAppend( slices, NULL, s );
                if ( rc != 0 )
                    release_slice( s );
            }
        }
    }
    return rc;
}
Exemplo n.º 12
0
LIB_EXPORT rc_t CC KDyldRegisterBuiltin ( const char *c_name, void *addr )
{
    String name;

    if (builtins == NULL) {
        rc_t    rc = 0;
        BSTree *scope;

        builtins = malloc ( sizeof * builtins );
        if ( builtins == NULL ) {
            return RC ( rcFS, rcDylib, rcRegistering, rcMemory, rcExhausted );
        }
        rc = KSymTableInit ( builtins, NULL );
        if (rc == 0) {
            scope = malloc ( sizeof * scope );
            if ( scope == NULL ) {
                rc = RC ( rcFS, rcDylib, rcRegistering, rcMemory, rcExhausted );
            } else {
                BSTreeInit ( scope );
                rc = KSymTablePushScope ( builtins, scope );
                if (rc != 0) {
                    free ( scope );
                }
            }
        }
        
        if ( rc != 0 ) {
            free ( builtins );
            builtins = NULL;
            return rc;
        }
    }

    StringInitCString ( & name, c_name );
    return KSymTableCreateSymbol ( builtins, NULL, & name, 0, addr );
}
Exemplo n.º 13
0
/* Make
 */
static
rc_t KRepositoryMake ( KRepository **rp, const KConfigNode *node,
    const char *name, KRepCategory category, KRepSubCategory subcategory )
{
    rc_t rc;
    KRepository *r;
    String name_str;

    /* measure string */
    StringInitCString ( & name_str, name );

    /* create object */
    r = malloc ( sizeof * r + name_str . size + 1 );
    if ( r == NULL )
        return RC ( rcKFG, rcNode, rcConstructing, rcMemory, rcExhausted );

    rc = KConfigNodeAddRef ( node );
    if ( rc != 0 )
    {
        free ( r );
        return rc;
    }

    r -> node = node;
    r -> name = name_str;
    r -> name . addr = ( char* ) ( r + 1 );
    KRefcountInit ( & r -> refcount, 1, "KRepository", "make", name );
    r -> category = category;
    r -> subcategory = subcategory;
    memcpy ( r + 1, name, name_str . size );
    ( ( char* ) ( r + 1 ) ) [ name_str . size ] = 0;

    * rp = r;

    return 0;
}
Exemplo n.º 14
0
rc_t KMain ( int argc, char *argv [] )
{
  rc_t rc;
  MyKVPair *pair;
  char buf[1024];
  int counter = 0;
  String key;
  int len;

  if (argc > 1 && !strcmp(argv[1], "-read")) {
    TrieRead(false);
    return 0;
  }

  if (argc > 1 && !strcmp(argv[1], "-swapread")) {
    TrieRead(true);
    return 0;
  }

  /* the documentation claims to return Unix status codes
     EINVAL or ENOMEM. however, it's more likely we just
     ran out of energy to update the comments...

     this creates a Trie with an initial character set of
     0-9, but which expands to accept new characters as seen
     thanks to the "true" value for "cs_expand"

     a standard Trie would encode the entire string into
     the prefix-tree, while this version only encodes as much
     of the strings initial characters as necessary before
     depositing the nodes into a BSTree at the leaves. you have
     told the code that it can accept up to 512 nodes in the
     BSTree until it has to split them up. to get more standard
     Trie behavior, choose a lower number, e.g. 1 */
  rc = TrieInit(&t, "a-z", 1, true);
  if (rc != 0) 
    LOGERR ( klogInt, rc, "triecreate" );

  /* you might want to get out after such an error... */

  /* another comment for you - using "buf, sizeof buf, stdin"
     will be safer ( I know this is just for experimentation,
     but since I'm reviewing it, I want you to get your money's worth */
  while (NULL != fgets(buf, 1024, stdin)) {
    len = strlen(buf);
    buf[--len] = '\0';
    /* printf("%d\n", len); */
    if (len <= 0)
      break;

    /* in C++, the cast to MyKVPair* is required, whereas
       in C it's not - which is part of its beauty. I prefer
       to avoid the cast unless we're planning on porting to C++,
       because the cast prevents you from changing the type of "pair"
       up above. same goes for "sizeof" - I prefer to let the compiler
       choose the right size for the allocation as "sizeof *pair"
       which will always be correct, even if I were to retype "pair" */
    pair = (MyKVPair *)malloc(sizeof(MyKVPair) + len + 1);
    if (pair == NULL) {
      fprintf(stderr, "Error in malloc\n");
      return -1;
    }
    strcpy(pair->key, buf);
    StringInitCString( &pair->tnode.key, (const char *)&pair->key );
    pair->value = counter++;
    /* fprintf(stderr, "At trieinsert\n"); */

    /* here I'd prefer "&pair->tnode" rather than the typecast
       ( do you get the idea I worry about typecasts? ) */
    rc = TrieInsert( &t, (TNode *)pair );
    if (rc != 0) 
      LOGERR ( klogInt, rc, "triecreate" );    
  }
  while (NULL != fgets(buf, 1024, stdin)) {
    len = strlen(buf);
    buf[--len] = '\0';
    StringInitCString( &key, buf );

    /* the first cast is required, although would normally
       want to be a "const MyKVPair*" so that you are sure
       not to modify it. the second cast is unnecessary */
    pair = (MyKVPair *)TrieFind(&t, (const String *)&key);
    if (pair != NULL) {
      printf("%s %d\n", buf, pair->value);
    }
  }
  Persist("/tmp/foo");
  return 0;
}
Exemplo n.º 15
0
static bool StringCmp ( const String * self, const char * val ) {
    String v;
    StringInitCString ( & v, val );

    return StringEqual ( self, & v );
}