Exemplo n.º 1
0
void everyconfig(char* folder, hash_t *config) {
  char * env = "default";
  if (getenv("CONFIG_ENV") != NULL) {
    env = getenv("CONFIG_ENV");
  }

  FILE *f;
  hash_t *default_config = hash_new();
  char path[1024];

  strcpy(path, folder);
  strcat(path, "/default.yaml");
  if (f = fopen(path, "r")) {
    fclose(f);
    yaml_read(path, default_config);
  }

  strcpy(path, folder);
  strcat(path, "/");
  strcat(path, env);
  strcat(path, ".yaml");
  yaml_read(path, config);

  hash_each(default_config, {
    if (!hash_has(config, (char *)key)) {
      hash_set(config, (char *)key, val);
    }
  });
Exemplo n.º 2
0
Arquivo: hash.c Projeto: abw/hemp
void test_hash() {
    HempHash table = hemp_hash_new();
    HempHash child = hemp_hash_new();
    

    hash_index_same(
        "foo",  3,
        "food", 3
    );
    hash_index_different(
        "foo",  3,
        "food", 4
    );
    hash_index_same(
        "Rachmaninov",  6,
        "Rachmaninoff", 6
    );
    hash_index_same(
        "Rachmaninov",  10,
        "Rachmaninoff", 10
    );
    hash_index_different(
        "Rachmaninov",  11,
        "Rachmaninoff", 11
    );
    hash_index_different(
        "Rachmaninoff", 11,
        "Rachmaninoff", 12
    );

    /* it's OK for a key in the hash (first arg) to match a prefix substring
     * of the search key (second arg), but not OK for a search key to match a
     * prefix of the hash key
     */
    hash_keys_do_match("foo", "food", 3);
    hash_keys_no_match("food", "foo", 3);

    hash_set(table, "msg", "hello world");
    hash_has(table, "msg", "hello world");

    hash_set(child, "foo", "The foo item");
    hash_has(child, "foo", "The foo item");

    hash_hasnt(child, "msg");
    
    hemp_hash_attach(child, table);
    
    hash_has(child, "foo", "The foo item");
    hash_has(child, "msg", "hello world");    

    hash_set(table, "bar", "The bar item");
    hash_has(child, "bar", "The bar item");    

    hash_has_keylen(child, "food", 3, "The foo item");
    hash_has_keylen(child, "msgism", 3, "hello world");    
    hash_hasnt_keylen(child, "msgism", 4);    

    hemp_hash_detach(child);

    hash_hasnt(child, "msg");
    hash_has_keylen(child, "food", 3, "The foo item");
    hash_hasnt_keylen(child, "msgism", 3);    

    hemp_hash_free(table);
    hemp_hash_free(child);
}