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
Arquivo: rdf_uri.c Projeto: njh/librdf
/**
 * librdf_new_uri2:
 * @world: redland world object
 * @uri_string: URI in string form
 * @length: length of string
 *
 * Constructor - create a new #librdf_uri object from a counted URI string.
 * 
 * A new URI is constructed from a copy of the string.  If the string
 * is a NULL pointer or 0 length or empty (first byte is 0) then the
 * result is NULL.
 *
 * Return value: a new #librdf_uri object or NULL on failure
 **/
librdf_uri*
librdf_new_uri2(librdf_world *world, 
                const unsigned char *uri_string,
                size_t length)
{
#ifdef LIBRDF_USE_RAPTOR_URI
  return raptor_new_uri_from_counted_string(world->raptor_world_ptr,
                                            uri_string, length);
#else
  librdf_uri* new_uri;
  unsigned char *new_string;
  librdf_hash_datum key, value; /* on stack - not allocated */
  librdf_hash_datum *old_value;

  /* just to be safe */
  memset(&key, 0, sizeof(key));
  memset(&value, 0, sizeof(value));

  librdf_world_open(world);

  if(!uri_string || !length || !*uri_string)
    return NULL;

#ifdef WITH_THREADS
  pthread_mutex_lock(world->mutex);
#endif
  
  key.data = (char*)uri_string;
  key.size = length;

  /* if existing URI found in hash, return it */
  if((old_value = librdf_hash_get_one(world->uris_hash, &key))) {
    new_uri = *(librdf_uri**)old_value->data;

#if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1
    LIBRDF_DEBUG3("Found existing URI %s in hash with current usage %d\n", uri_string, new_uri->usage);
#endif

    librdf_free_hash_datum(old_value);
    new_uri->usage++;

#if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1
    if(new_uri->usage > new_uri->max_usage)
      new_uri->max_usage = new_uri->usage;
#endif    

    goto unlock;
  }
  

  /* otherwise create a new one */

#if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1
  LIBRDF_DEBUG2("Creating new URI %s in hash\n", uri_string);
#endif

  new_uri = (librdf_uri*)LIBRDF_CALLOC(librdf_uri, 1, sizeof(librdf_uri));
  if(!new_uri)
    goto unlock;

  new_uri->world = world;
  new_uri->string_length = length;

  new_string = (unsigned char*)LIBRDF_MALLOC(cstring, length+1);
  if(!new_string) {
    LIBRDF_FREE(librdf_uri, new_uri);
    new_uri = NULL;
    goto unlock;
  }
  
  strcpy((char*)new_string, (const char*)uri_string);
  new_uri->string = new_string;

  new_uri->usage = 1;
#if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1
  new_uri->max_usage = 1;
#endif

  value.data = &new_uri; value.size = sizeof(librdf_uri*);
  
  /* store in hash: URI-string => (librdf_uri*) */
  if(librdf_hash_put(world->uris_hash, &key, &value)) {
    LIBRDF_FREE(cstring, new_string);
    LIBRDF_FREE(librdf_uri, new_uri);
    new_uri = NULL;
  }

 unlock:
#ifdef WITH_THREADS
  pthread_mutex_unlock(world->mutex);
#endif

  return new_uri;
#endif /* !LIBRDF_USE_RAPTOR_URI */
}
Exemplo n.º 3
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;
}