Example #1
0
static int
librdf_storage_trees_node_compare(librdf_node* n1, librdf_node* n2)
{
  if (n1 == n2) {
    return 0;
  } else if (n1->type != n2->type) {
    return n2->type - n1->type;
  } else {
    switch (n1->type) {
      case LIBRDF_NODE_TYPE_RESOURCE:
        return librdf_uri_compare(librdf_node_get_uri(n1), librdf_node_get_uri(n2));
      case LIBRDF_NODE_TYPE_LITERAL:
        if (1) {
          const char *s;
          size_t l1;
          size_t l2;
          size_t l;
          int r;

          s = librdf_node_get_literal_value_language(n1);
          l1 = s ? strlen(s) : 0;
          s = librdf_node_get_literal_value_language(n2);
          l2 = s ? strlen(s) : 0;

          l = (l1 < l2) ? l1 : l2;

          /* compare first by data type */
          r = librdf_uri_compare(librdf_node_get_literal_value_datatype_uri(n1),
                                 librdf_node_get_literal_value_datatype_uri(n2));
          if (r)
            return r;

          /* if data type is equal, compare by value */
          r = strcmp((const char*)librdf_node_get_literal_value(n1),
                     (const char*)librdf_node_get_literal_value(n2));
          if (r)
            return r;

          /* if both data type and value are equal, compare by language */
          if (l) {
            return strncmp(librdf_node_get_literal_value_language(n1),
                           librdf_node_get_literal_value_language(n2),
                           (size_t)l);
          } else {
            /* if l == 0 strncmp will always return 0; in that case
             * consider the node with no language to be lesser. */
            return l1 - l2;
          }
        }
      case LIBRDF_NODE_TYPE_BLANK:
        return strcmp((char*)n1->value.blank.identifier,
                      (char*)n2->value.blank.identifier);
      case LIBRDF_NODE_TYPE_UNKNOWN:
      default:
        return (char*)n2-(char*)n1; /* ? */
    }
  }
}
Example #2
0
rasqal_literal*
redland_node_to_rasqal_literal(librdf_world* world, librdf_node *node)
{
  rasqal_literal* l;
  
  if(librdf_node_is_resource(node)) {
    raptor_uri* uri=(raptor_uri*)librdf_new_uri_from_uri(librdf_node_get_uri(node));
    l=rasqal_new_uri_literal(world->rasqal_world_ptr, uri); /* transfer uri ownership to literal */
  } else if(librdf_node_is_literal(node)) {
    unsigned char *string;
    librdf_uri *uri;
    unsigned char *new_string;
    char *new_language=NULL;
    raptor_uri *new_datatype=NULL;
    size_t len;

    string=librdf_node_get_literal_value_as_counted_string(node, &len);
    new_string=(unsigned char*)rasqal_alloc_memory(len+1);
    if(!new_string)
      return NULL;
    strcpy((char*)new_string, (const char*)string);

    string=(unsigned char*)librdf_node_get_literal_value_language(node);
    if(string) {
      new_language=(char*)rasqal_alloc_memory(strlen((const char*)string)+1);
      if(!new_language) {
        rasqal_free_memory((void*)new_string);
        return NULL;
      }
      strcpy((char*)new_language, (const char*)string);
    }
    uri=librdf_node_get_literal_value_datatype_uri(node);
    if(uri)
      new_datatype=(raptor_uri*)librdf_new_uri_from_uri(uri);
    /* transfer new_string,new_language,new_datatype ownership to literal */
    l = rasqal_new_string_literal(world->rasqal_world_ptr, (const unsigned char*)new_string, new_language, new_datatype, NULL);
  } else {
    unsigned char *blank=librdf_node_get_blank_identifier(node);
    unsigned char *new_blank;
    if(!blank)
      return NULL;
    new_blank=(unsigned char*)rasqal_alloc_memory(strlen((const char*)blank)+1);
    if(!new_blank)
      return NULL;
    strcpy((char*)new_blank, (const char*)blank);
    /* transfer new_blank ownership to literal */
    l = rasqal_new_simple_literal(world->rasqal_world_ptr, RASQAL_LITERAL_BLANK, (const unsigned char*)new_blank);
  }

  return l;
}
static void
librdf_serializer_print_statement_as_ntriple(librdf_statement * statement,
                                             FILE *stream) 
{
  librdf_node *subject=librdf_statement_get_subject(statement);
  librdf_node *predicate=librdf_statement_get_predicate(statement);
  librdf_node *object=librdf_statement_get_object(statement);
  char *lang=NULL;
  librdf_uri *dt_uri=NULL;
  
  if(librdf_node_is_blank(subject))
    fprintf(stream, "_:%s", librdf_node_get_blank_identifier(subject));
  else if(librdf_node_is_resource(subject)) {
    /* Must be a URI */
    fputc('<', stream);
    raptor_print_ntriples_string(stream, librdf_uri_as_string(librdf_node_get_uri(subject)), '\0');
    fputc('>', stream);
  } else {
    LIBRDF_ERROR2(statement->world, "Do not know how to print triple subject type %d\n", librdf_node_get_type(subject));
    return;
  }

  if(!librdf_node_is_resource(predicate)) {
    LIBRDF_ERROR2(statement->world, "Do not know how to print triple predicate type %d\n", librdf_node_get_type(predicate));
    return;
  }

  fputc(' ', stream);
  fputc('<', stream);
  raptor_print_ntriples_string(stream, librdf_uri_as_string(librdf_node_get_uri(predicate)), '\0');
  fputc('>', stream);
  fputc(' ', stream);

  switch(librdf_node_get_type(object)) {
    case LIBRDF_NODE_TYPE_LITERAL:
      fputc('"', stream);
      raptor_print_ntriples_string(stream, librdf_node_get_literal_value(object), '"');
      fputc('"', stream);
      lang=librdf_node_get_literal_value_language(object);
      dt_uri=librdf_node_get_literal_value_datatype_uri(object);
      if(lang) {
        fputc('@', stream);
        fputs(lang, stream);
      }
      if(dt_uri) {
        fputs("^^<", stream); 
        raptor_print_ntriples_string(stream, librdf_uri_as_string(dt_uri), '\0');
        fputc('>', stream);
      }
      break;
    case LIBRDF_NODE_TYPE_BLANK:
      fputs("_:", stream);
      fputs((const char*)librdf_node_get_blank_identifier(object), stream);
      break;
    case LIBRDF_NODE_TYPE_RESOURCE:
      fputc('<', stream);
      raptor_print_ntriples_string(stream, librdf_uri_as_string(librdf_node_get_uri(object)), '\0');
      fputc('>', stream);
      break;
    default:
      LIBRDF_ERROR2(statement->world, "Do not know how to print triple object type %d\n", librdf_node_get_type(object));
      return;
  }
  fputs(" .", stream);
}