Author: dajobe
Date: Thu Jan 17 04:54:34 2008
New Revision: 13609

URL: http://svn.librdf.org/view?rev=3D13609&view=3Drev
Log:
(raptor_iostream_write_string_python): Added renamed from raptor_iostream_w=
rite_string_turtle and replacing raptor_iostream_write_string_ntriples. Han=
dles writing to an iostream with escapes in N-Triples, Turtle and JSON with=
 appropriate escapes which are all python like. (raptor_iostream_write_stri=
ng_turtle): Changed to use raptor_iostream_write_string_python.  (raptor_tu=
rtle_writer_quoted_counted_string): Renamed from raptor_turtle_writer_quote=
d and taking a length

Modified:
    raptor/trunk/src/raptor.h
    raptor/trunk/src/raptor_serialize_ntriples.c
    raptor/trunk/src/raptor_turtle_writer.c

Modified: raptor/trunk/src/raptor.h
URL: http://svn.librdf.org/view/raptor/trunk/src/raptor.h?rev=3D13609&r1=3D=
13608&r2=3D13609&view=3Ddiff
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- raptor/trunk/src/raptor.h (original)
+++ raptor/trunk/src/raptor.h Thu Jan 17 04:54:34 2008
@@ -984,6 +984,8 @@
 RAPTOR_API
 int raptor_iostream_write_string_ntriples(raptor_iostream *iostr, const un=
signed char *string, size_t len, const char delim);
 RAPTOR_API
+int raptor_iostream_write_string_python(raptor_iostream *iostr, const unsi=
gned char *string, size_t len, const char delim, int flags);
+RAPTOR_API RAPTOR_DEPRECATED
 void raptor_iostream_write_string_turtle(raptor_iostream *iostr, const uns=
igned char *string, size_t len);
 RAPTOR_API
 void raptor_iostream_write_statement_ntriples(raptor_iostream* iostr, cons=
t raptor_statement *statement);

Modified: raptor/trunk/src/raptor_serialize_ntriples.c
URL: http://svn.librdf.org/view/raptor/trunk/src/raptor_serialize_ntriples.=
c?rev=3D13609&r1=3D13608&r2=3D13609&view=3Ddiff
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- raptor/trunk/src/raptor_serialize_ntriples.c (original)
+++ raptor/trunk/src/raptor_serialize_ntriples.c Thu Jan 17 04:54:34 2008
@@ -2,7 +2,7 @@
  *
  * raptor_serialize_rdfxml.c - N-Triples serializer
  *
- * Copyright (C) 2004-2006, David Beckett http://purl.org/net/dajobe/
+ * Copyright (C) 2004-2008, David Beckett http://purl.org/net/dajobe/
  * Copyright (C) 2004-2005, University of Bristol, UK http://www.bristol.a=
c.uk/
  *
  * This package is Free Software and part of Redland http://librdf.org/
@@ -111,65 +111,7 @@
                                       size_t len,
                                       const char delim)
 {
-  unsigned char c;
-  int unichar_len;
-  raptor_unichar unichar;
-  =

-  for(; (c=3D*string); string++, len--) {
-    if((delim && c =3D=3D delim && (delim=3D=3D'\'' || delim =3D=3D '"')) =
||
-       c =3D=3D '\\') {
-      raptor_iostream_write_byte(iostr, '\\');
-      raptor_iostream_write_byte(iostr, c);
-      continue;
-    }
-    if(delim && c =3D=3D delim) {
-      raptor_iostream_write_counted_string(iostr, "\\u", 2);
-      raptor_iostream_format_hexadecimal(iostr, c, 4);
-      continue;
-    }
-    =

-    /* Note: NTriples is ASCII */
-    if(c =3D=3D 0x09) {
-      raptor_iostream_write_counted_string(iostr, "\\t", 2);
-      continue;
-    } else if(c =3D=3D 0x0a) {
-      raptor_iostream_write_counted_string(iostr, "\\n", 2);
-      continue;
-    } else if(c =3D=3D 0x0d) {
-      raptor_iostream_write_counted_string(iostr, "\\r", 2);
-      continue;
-    } else if(c < 0x20|| c =3D=3D 0x7f) {
-      raptor_iostream_write_counted_string(iostr, "\\u", 2);
-      raptor_iostream_format_hexadecimal(iostr, c, 4);
-      continue;
-    } else if(c < 0x80) {
-      raptor_iostream_write_byte(iostr, c);
-      continue;
-    }
-    =

-    /* It is unicode */
-    =

-    unichar_len=3Draptor_utf8_to_unicode_char(NULL, string, len);
-    if(unichar_len < 0 || unichar_len > (int)len)
-      /* UTF-8 encoding had an error or ended in the middle of a string */
-      return 1;
-
-    unichar_len=3Draptor_utf8_to_unicode_char(&unichar, string, len);
-    =

-    if(unichar < 0x10000) {
-      raptor_iostream_write_counted_string(iostr, "\\u", 2);
-      raptor_iostream_format_hexadecimal(iostr, unichar, 4);
-    } else {
-      raptor_iostream_write_counted_string(iostr, "\\U", 2);
-      raptor_iostream_format_hexadecimal(iostr, unichar, 8);
-    }
-    =

-    unichar_len--; /* since loop does len-- */
-    string +=3D unichar_len; len -=3D unichar_len;
-
-  }
-
-  return 0;
+  return raptor_iostream_write_string_python(iostr, string, len, delim, 0);
 }
 =

 =


Modified: raptor/trunk/src/raptor_turtle_writer.c
URL: http://svn.librdf.org/view/raptor/trunk/src/raptor_turtle_writer.c?rev=
=3D13609&r1=3D13608&r2=3D13609&view=3Ddiff
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- raptor/trunk/src/raptor_turtle_writer.c (original)
+++ raptor/trunk/src/raptor_turtle_writer.c Thu Jan 17 04:54:34 2008
@@ -3,7 +3,7 @@
  * raptor_turtle_writer.c - Raptor Turtle Writer
  *
  * Copyright (C) 2006, Dave Robillard
- * Copyright (C) 2003-2007, David Beckett http://purl.org/net/dajobe/
+ * Copyright (C) 2003-2008, David Beckett http://purl.org/net/dajobe/
  * Copyright (C) 2003-2005, University of Bristol, UK http://www.bristol.a=
c.uk/
  * =

  * This package is Free Software and part of Redland http://librdf.org/
@@ -378,76 +378,150 @@
 =

 =

 /**
+ * raptor_iostream_write_string_python:
+ * @iostr: #raptor_iostream to write to
+ * @string: UTF-8 string to write
+ * @len: length of UTF-8 string
+ * @delim: Terminating delimiter character for string (such as " or >)
+ * or \0 for no escaping.
+ * @flags: flags 0=3DN-Triples mode, 1=3DTurtle (allow raw UTF-8), 2=3DJSON
+ *
+ * Write a UTF-8 string using Python-style escapes (N-Triples, Turtle, JSO=
N) to an iostream.
+ * =

+ * Return value: non-0 on failure such as bad UTF-8 encoding.
+ **/
+int
+raptor_iostream_write_string_python(raptor_iostream *iostr,
+                                    const unsigned char *string,
+                                    size_t len,
+                                    const char delim,
+                                    int flags)
+{
+  unsigned char c;
+  int unichar_len;
+  raptor_unichar unichar;
+
+  if(flags < 0 || flags > 2)
+    return 1;
+  =

+  for(; (c=3D*string); string++, len--) {
+    if((delim && c =3D=3D delim && (delim=3D=3D'\'' || delim =3D=3D '"')) =
||
+       c =3D=3D '\\') {
+      raptor_iostream_write_byte(iostr, '\\');
+      raptor_iostream_write_byte(iostr, c);
+      continue;
+    }
+    if(delim && c =3D=3D delim) {
+      raptor_iostream_write_counted_string(iostr, "\\u", 2);
+      raptor_iostream_format_hexadecimal(iostr, c, 4);
+      continue;
+    }
+    =

+    /* Note: NTriples is ASCII */
+    if(c =3D=3D 0x09) {
+      raptor_iostream_write_counted_string(iostr, "\\t", 2);
+      continue;
+    } else if((flags =3D=3D 2) && c =3D=3D 0x08) {
+      /* JSON has \b for backspace */
+      raptor_iostream_write_counted_string(iostr, "\\b", 2);
+      continue;
+    } else if(c =3D=3D 0x0a) {
+      raptor_iostream_write_counted_string(iostr, "\\n", 2);
+      continue;
+    } else if((flags =3D=3D 2) && c =3D=3D 0x0b) {
+      /* JSON has \f for formfeed */
+      raptor_iostream_write_counted_string(iostr, "\\f", 2);
+      continue;
+    } else if(c =3D=3D 0x0d) {
+      raptor_iostream_write_counted_string(iostr, "\\r", 2);
+      continue;
+    } else if(c < 0x20|| c =3D=3D 0x7f) {
+      raptor_iostream_write_counted_string(iostr, "\\u", 2);
+      raptor_iostream_format_hexadecimal(iostr, c, 4);
+      continue;
+    } else if(c < 0x80) {
+      raptor_iostream_write_byte(iostr, c);
+      continue;
+    }
+    =

+    /* It is unicode */
+    =

+    unichar_len=3Draptor_utf8_to_unicode_char(NULL, string, len);
+    if(unichar_len < 0 || unichar_len > (int)len)
+      /* UTF-8 encoding had an error or ended in the middle of a string */
+      return 1;
+
+    unichar_len=3Draptor_utf8_to_unicode_char(&unichar, string, len);
+
+    if(flags =3D=3D 1 || flags =3D=3D 2) {
+      /* Turtle and JSON are UTF-8 - no need to escape */
+      raptor_iostream_write_counted_string(iostr, string, len);
+    } else {
+      if(unichar < 0x10000) {
+        raptor_iostream_write_counted_string(iostr, "\\u", 2);
+        raptor_iostream_format_hexadecimal(iostr, unichar, 4);
+      } else {
+        raptor_iostream_write_counted_string(iostr, "\\U", 2);
+        raptor_iostream_format_hexadecimal(iostr, unichar, 8);
+      }
+    }
+    =

+    unichar_len--; /* since loop does len-- */
+    string +=3D unichar_len; len -=3D unichar_len;
+
+  }
+
+  return 0;
+}
+
+
+/**
  * raptor_iostream_write_string_turtle:
  * @iostr: #raptor_iostream to write to
  * @string: UTF-8 string to write
  * @len: length of UTF-8 string
  *
- * Write an UTF-8 string using Turtle "longString" triple quoting to an io=
stream.
+ * Write an UTF-8 string using Turtle "longString" triple quoting to
+ * an iostream.
+ *
  **/
 void
 raptor_iostream_write_string_turtle(raptor_iostream *iostr,
-                                    const unsigned char *string,
-                                    size_t len)
-{
-  unsigned char c;
-  =

-  for(; (c=3D*string); string++, len--) {
-    if(c =3D=3D '\"') {
-      raptor_iostream_write_byte(iostr, '\\');
-      raptor_iostream_write_byte(iostr, c);
-    } else if(c =3D=3D '\\') {
-      raptor_iostream_write_byte(iostr, '\\');
-      raptor_iostream_write_byte(iostr, '\\');
-    } else {
-      raptor_iostream_write_byte(iostr, c);
-    }
-  }
-}
-
-
-/**
- * raptor_turtle_writer_quoted:
+                                    const unsigned char *string, size_t le=
n)
+{
+  raptor_iostream_write_string_python(iostr, string, len, '"', 1);
+}
+
+
+
+/**
+ * raptor_turtle_writer_quoted_counted_string:
  * @turtle_writer: Turtle writer object
- * @s: string to write (SHARED)
- *
- * Write a string raw to the Turtle writer.
+ * @s: string to write
+ * @len: string length
+ *
+ * Write a Turtle escaped-string inside double quotes to the writer.
  *
  * Return value: non-0 on failure
  **/
 int
-raptor_turtle_writer_quoted(raptor_turtle_writer* turtle_writer,
-                            const unsigned char *s)
-{
-  raptor_stringbuffer* sb;
-
+raptor_turtle_writer_quoted_counted_string(raptor_turtle_writer* turtle_wr=
iter,
+                                           const unsigned char *s, size_t =
len)
+{
+  const unsigned char *quotes=3D(const unsigned char *)"\"\"\"\"";
+  const unsigned char *q;
+  size_t q_len;
+  =

   if(!s)
     return 1;
-
-  sb =3D raptor_new_stringbuffer();
-  if(!sb)
-    return 1;
-
-  if(raptor_turtle_writer_contains_newline(s)) {
-    raptor_iostream_write_string(turtle_writer->iostr, "\"\"\"");
-    raptor_iostream_write_string_turtle(turtle_writer->iostr, s, =

-                                        strlen((const char*)s));
-    raptor_iostream_write_string(turtle_writer->iostr, "\"\"\"");
-  } else {
-    raptor_stringbuffer_append_turtle_string(sb, s, strlen((const char*)s),
-                                             (int)'"',
-                                             turtle_writer->error_handler,
-                                             turtle_writer->error_data);
-
-    raptor_iostream_write_byte(turtle_writer->iostr, '\"');
-
-    /* FIXME: over-escapes things because ntriples is ASCII */
-    raptor_iostream_write_string_ntriples(turtle_writer->iostr, s,
-                                          strlen((const char*)s), '"');
-    raptor_iostream_write_byte(turtle_writer->iostr, '\"');
-  }
-
-  raptor_free_stringbuffer(sb);
+  =

+  q=3Draptor_turtle_writer_contains_newline(s) ? quotes : quotes+2;
+  q_len=3D(q =3D=3D quotes) ? 3 : 1;
+  raptor_iostream_write_counted_string(turtle_writer->iostr, q, q_len);
+  raptor_iostream_write_string_python(turtle_writer->iostr,
+                                      s, strlen((const char*)s), '"', 1);
+  raptor_iostream_write_counted_string(turtle_writer->iostr, q, q_len);
+
   return 0;
 }
 =

@@ -503,7 +577,8 @@
   if(written)
     return 0;
     =

-  if(raptor_turtle_writer_quoted(turtle_writer, s))
+  if(raptor_turtle_writer_quoted_counted_string(turtle_writer, s,
+                                                strlen((const char*)s)))
     return 1;
 =

   /* typed literal, not a special case */
@@ -856,7 +931,8 @@
   =

   raptor_turtle_writer_raw(turtle_writer, (const unsigned char*)"ex:foo ");
 =

-  raptor_turtle_writer_quoted(turtle_writer, longstr);
+  raptor_turtle_writer_quoted_counted_string(turtle_writer, longstr,
+                                             strlen(longstr));
   raptor_turtle_writer_raw_counted(turtle_writer,
                                    (const unsigned char*)" ;", 2);
   raptor_turtle_writer_newline(turtle_writer);


_______________________________________________
redland-commits mailing list
[email protected]
http://lists.librdf.org/mailman/listinfo/redland-commits

Reply via email to