Exemplo n.º 1
0
/**
 * librdf_hash_bdb_clone - Clone the BerkeleyDB hash
 * @hash: new &librdf_hash that this implements
 * @context: new BerkeleyDB hash context
 * @new_identifier: new identifier for this hash
 * @old_context: old BerkeleyDB hash context
 * 
 * Clones the existing Berkeley DB hash into the new one with the
 * new identifier.
 * 
 * Return value: non 0 on failure
 **/
static int
librdf_hash_bdb_clone(librdf_hash *hash, void* context, char *new_identifier,
                      void *old_context) 
{
  librdf_hash_bdb_context* hcontext=(librdf_hash_bdb_context*)context;
  librdf_hash_bdb_context* old_hcontext=(librdf_hash_bdb_context*)old_context;
  librdf_hash_datum *key, *value;
  librdf_iterator *iterator;
  int status=0;
  
  /* copy data fields that might change */
  hcontext->hash=hash;

  /* Note: The options are not used at present, so no need to make a copy 
   */
  if(librdf_hash_bdb_open(context, new_identifier,
                          old_hcontext->mode, old_hcontext->is_writable,
                          old_hcontext->is_new, NULL))
    return 1;


  /* Use higher level functions to iterator this data
   * on the other hand, maybe this is a good idea since that
   * code is tested and works
   */

  key=librdf_new_hash_datum(hash->world, NULL, 0);
  value=librdf_new_hash_datum(hash->world, NULL, 0);

  iterator=librdf_hash_get_all(old_hcontext->hash, key, value);
  while(!librdf_iterator_end(iterator)) {
    librdf_hash_datum* k= (librdf_hash_datum*)librdf_iterator_get_key(iterator);
    librdf_hash_datum* v= (librdf_hash_datum*)librdf_iterator_get_value(iterator);

    if(librdf_hash_bdb_put(hcontext, k, v)) {
      status=1;
      break;
    }
    librdf_iterator_next(iterator);
  }
  if(iterator)
    librdf_free_iterator(iterator);

  librdf_free_hash_datum(value);
  librdf_free_hash_datum(key);

  return status;
}
Exemplo n.º 2
0
static int test_string_manipulation(librdf_world *world, librdf_hash *h) {

  const char *test_hash_array[]={"shape", "cube",
      "sides", "6", /* for testing get as long */
      "3d", "yes", /* testing bool */
      "colour", "red",
      "colour", "yellow",
      "creator", "rubik",
      NULL};

  const unsigned char* template_string=(const unsigned char*)"the shape is %{shape} and the sides are %{sides} created by %{creator}";
  const unsigned char* template_expected=(const unsigned char*)"the shape is cube and the sides are 6 created by rubik";
  const char * const test_hash_string="field1='value1', field2='\\'value2', field3='\\\\', field4='\\\\\\'', field5 = 'a' ";
  const char * filter_string[] = {"field1", NULL};

  librdf_iterator* iterator;
  librdf_hash_datum *key_hd;
  char *string_result;
  unsigned char *template_result;
  int b;
  long l;

  /*
   *
   * Test librdf_hash_from_array_of_strings
   *
   */
  fprintf(stdout, "Initializing hash from array of strings\n");
  if(librdf_hash_from_array_of_strings(h, test_hash_array)) {
    fprintf(stderr, "Failed to init hash from array of strings\n");
    return(1);
  }

  fprintf(stdout, "librdf_hash_from_array_of_strings success. total values: %d.", librdf_hash_values_count(h));
  fprintf(stdout, "\nresulting hash: ");
  librdf_hash_print(h, stdout);
  fputc('\n', stdout);

  fprintf(stdout, "\nresulting hash keys: ");
  librdf_hash_print_keys(h, stdout);
  fputc('\n', stdout);

  /*
   *
   * Test librdf_hash_get_as_boolean and librdf_hash_get_as_long
   *
   */
  key_hd=librdf_new_hash_datum(world, NULL, 0);

  iterator=librdf_hash_keys(h, key_hd);
  while(!librdf_iterator_end(iterator)) {
    librdf_hash_datum *k=(librdf_hash_datum*)librdf_iterator_get_key(iterator);
    char *key_string;

    key_string = LIBRDF_MALLOC(char*, k->size + 1);
    if(!key_string)
      break;
    strncpy(key_string, (char*)k->data, k->size);
    key_string[k->size]='\0';

    fprintf(stdout, "boolean value of key '%s' is ", key_string);
    b=librdf_hash_get_as_boolean(h, key_string);
    fprintf(stdout, "%d (0 F, -1 Bad, else T)\n", b);

    fprintf(stdout, "long value of key '%s' is ", key_string);
    l=librdf_hash_get_as_long(h, key_string);
    fprintf(stdout, "%ld (decimal, -1 Bad)\n", l);

    LIBRDF_FREE(char*, key_string);
    librdf_iterator_next(iterator);
  }
  if(iterator)
    librdf_free_iterator(iterator);
  librdf_free_hash_datum(key_hd);


  /*
   *
   * Test librdf_hash_from_string
   *
   */
  fprintf(stdout, "Initializing hash from string >>%s<<\n", test_hash_string);
  librdf_hash_from_string (h, test_hash_string);

  fprintf(stdout, "values count %d\n", librdf_hash_values_count(h));
  fprintf(stdout, "resulting: ");
  librdf_hash_print(h, stdout);
  fputc('\n', stdout);


  fprintf(stdout, "Converting hash back to a string");
  string_result=librdf_hash_to_string(h, NULL);

  /* Order is not guaranteed, so sadly we can't just do a full string comparison */
  if(!strstr(string_result, "field1='value1'")) {
    fprintf(stdout, "Did not see field1='value1' in the generated string >>%s<<\n",
        string_result);
    return 0;
  } else if(!strstr(string_result, "field2='\\'value2'")) {
    fprintf(stdout, "Did not see field2='\\'value2'' in the generated string >>%s<<\n",
        string_result);
    return 0;
  } else if(!strstr(string_result, "field3='\\\\'")) {
    fprintf(stdout, "Did not see field3='\\\\' in the generated string >>%s<<\n",
        string_result);
    return 0;
  } else if(!strstr(string_result, "field4='\\\\\\'")) {
    fprintf(stdout, "Did not see field4='\\\\\\' in the generated string >>%s<<\n",
        string_result);
    return 0;
  } else if(!strstr(string_result, "field5='a'")) {
    fprintf(stdout, "Did not see field5='a' in the generated string >>%s<<\n", string_result);
    return 0;
  } else {
    fprintf(stdout, "\nresulting in >>%s<<\n", string_result);
  }
  librdf_free_memory(string_result);

  fprintf(stdout, "Converting hash back to a string with filter");
  string_result=librdf_hash_to_string(h, filter_string);
  if(strstr(string_result, "field1")) {
    fprintf(stdout, "Was not expecting >>field1<< to be in the generated string >>%s<<\n",
        string_result);
    return 0;
  } else {
    fprintf(stdout, "\nresulting in >>%s<<\n", string_result);
  }
  librdf_free_memory(string_result);

  /*
   *
   * Test librdf_hash_interpret_template
   *
   */
  fprintf(stdout, "Substituting into template >>%s",
      template_string);
  template_result=librdf_hash_interpret_template(template_string, h,
      (const unsigned char*)"%{",
      (const unsigned char*)"}");
  if(strcmp((const char*)template_result, (const char*)template_expected)) {
    fprintf(stdout, "Templating failed. Result was >>%s<< but expected >>%s<<\n",
        template_result, template_expected);
    return 0;
  } else
    fprintf(stdout, "\nresulting in >>%s<<\n", template_result);

  LIBRDF_FREE(char*, template_result);

  return 0;
}