Example #1
0
static int test_put(librdf_world *world, librdf_hash *h, const char **test_put_array, int test_put_array_len) {

  librdf_hash_datum hd_key, hd_value;

  int j;
  for(j=0; j<test_put_array_len; j+=2) {
    const char *key = (char*)test_put_array[j];
    const char *value = (char*)test_put_array[j+1];

    int ret = -1;
    if (key && value) {
      hd_key.data=(char*)key;
      hd_value.data=(char*)value;
      hd_key.size=strlen((char*)hd_key.data);
      hd_value.size=strlen((char*)hd_value.data);
      ret = librdf_hash_put(h, &hd_key, &hd_value);

      if (ret)
        fprintf(stdout, "put failed: %d", ret);
    }
  }

  return 0;
}
Example #2
0
File: rdf_uri.c Project: 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 */
}