Author: miguel
Date: 2006-09-18 04:27:59 -0400 (Mon, 18 Sep 2006)
New Revision: 65563
Modified:
trunk/mono/eglib/TODO
trunk/mono/eglib/src/glib.h
trunk/mono/eglib/src/gstr.c
trunk/mono/eglib/test/string-util.c
Log:
Another method, but need to complete the tests
Modified: trunk/mono/eglib/TODO
===================================================================
--- trunk/mono/eglib/TODO 2006-09-18 08:24:44 UTC (rev 65562)
+++ trunk/mono/eglib/TODO 2006-09-18 08:27:59 UTC (rev 65563)
@@ -20,7 +20,6 @@
3 g_thread_init
* Path string manipulation and utilities.
- 2 g_filename_to_uri
2 g_filename_from_uri
* Information retrieval
Modified: trunk/mono/eglib/src/glib.h
===================================================================
--- trunk/mono/eglib/src/glib.h 2006-09-18 08:24:44 UTC (rev 65562)
+++ trunk/mono/eglib/src/glib.h 2006-09-18 08:27:59 UTC (rev 65563)
@@ -132,6 +132,20 @@
#define g_assert_not_reached() G_STMT_START { g_error ("* Assertion: should
not be reached at %s:%d\n", __FILE__, __LINE__); } G_STMT_END
/*
+ * Errors
+ */
+typedef struct {
+ /* In the real glib, this is a GQuark, but we dont use/need that */
+ gpointer domain;
+ gint code;
+ gchar *message;
+} GError;
+
+void g_error_free (GError *error);
+GError *g_error_new (gpointer domain, gint code, const char *format, ...);
+void g_set_error (GError **err, gpointer domain, gint code, const gchar
*format, ...);
+
+/*
* Strings utility
*/
gchar *g_strdup_printf (const gchar *format, ...);
@@ -149,6 +163,7 @@
gchar *g_strjoin (const gchar *separator, ...);
gchar *g_strchug (gchar *str);
gchar *g_strchomp (gchar *str);
+gchar *g_filename_to_uri (const gchar *filename, const gchar *hostname,
GError **error);
gint g_printf (gchar const *format, ...);
gint g_fprintf (FILE *file, gchar const *format, ...);
@@ -430,20 +445,6 @@
#define G_UNLIKELY(x) (x)
/*
- * Errors
- */
-typedef struct {
- /* In the real glib, this is a GQuark, but we dont use/need that */
- gpointer domain;
- gint code;
- gchar *message;
-} GError;
-
-void g_error_free (GError *error);
-GError *g_error_new (gpointer domain, gint code, const char *format, ...);
-void g_set_error (GError **err, gpointer domain, gint code, const gchar
*format, ...);
-
-/*
* Unicode conversion
*/
Modified: trunk/mono/eglib/src/gstr.c
===================================================================
--- trunk/mono/eglib/src/gstr.c 2006-09-18 08:24:44 UTC (rev 65562)
+++ trunk/mono/eglib/src/gstr.c 2006-09-18 08:27:59 UTC (rev 65563)
@@ -359,4 +359,59 @@
return ret;
}
+static const char const hx [] = { '0', '1', '2', '3', '4', '5', '6', '7',
+ '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
+static gboolean
+char_needs_encoding (char c)
+{
+ if (((unsigned char)c) >= 0x80)
+ return TRUE;
+
+ if ((c >= '@' && c <= 'Z') ||
+ (c >= 'a' && c <= 'z') ||
+ (c >= '&' && c < 0x3b) ||
+ (c == '!') || (c == '$') || (c == '_') || c == '=')
+ return FALSE;
+ return TRUE;
+}
+
+gchar *
+g_filename_to_uri (const gchar *filename, const gchar *hostname, GError
**error)
+{
+ int n;
+ char *ret, *rp;
+ const char *p;
+
+ g_return_val_if_fail (filename != NULL, NULL);
+
+ if (hostname != NULL)
+ g_warning ("g_filename_to_uri: hostname and error not handled");
+
+ if (*filename != '/'){
+ if (error != NULL)
+ *error = g_error_new (NULL, 2, "Not an absolute
filename");
+
+ return NULL;
+ }
+
+ n = strlen ("file://") + 1;
+ for (p = filename; *p; p++){
+ if (char_needs_encoding (*p))
+ n += 3;
+ else
+ n++;
+ }
+ ret = g_malloc (n);
+ strcpy (ret, "file://");
+ for (p = filename, rp = ret + strlen (ret); *p; p++){
+ if (char_needs_encoding (*p)){
+ *rp++ = '%';
+ *rp++ = hx [((unsigned char)(*p)) >> 4];
+ *rp++ = hx [((unsigned char)(*p)) & 0xf];
+ } else
+ *rp++ = *p;
+ }
+ *rp = 0;
+ return ret;
+}
Modified: trunk/mono/eglib/test/string-util.c
===================================================================
--- trunk/mono/eglib/test/string-util.c 2006-09-18 08:24:44 UTC (rev 65562)
+++ trunk/mono/eglib/test/string-util.c 2006-09-18 08:27:59 UTC (rev 65563)
@@ -101,27 +101,27 @@
s = g_strjoin (NULL, "a", "b", NULL);
if (strcmp (s, "ab") != 0)
- return "Join of two strings with no separator fails";
+ return FAILED ("Join of two strings with no separator fails");
g_free (s);
s = g_strjoin ("", "a", "b", NULL);
if (strcmp (s, "ab") != 0)
- return "Join of two strings with empty separator fails";
+ return FAILED ("Join of two strings with empty separator
fails");
g_free (s);
s = g_strjoin ("-", "a", "b", NULL);
if (strcmp (s, "a-b") != 0)
- return "Join of two strings with separator fails";
+ return FAILED ("Join of two strings with separator fails");
g_free (s);
s = g_strjoin ("-", "aaaa", "bbbb", "cccc", "dddd", NULL);
if (strcmp (s, "aaaa-bbbb-cccc-dddd") != 0)
- return "Join of multiple strings fails";
+ return FAILED ("Join of multiple strings fails");
g_free (s);
s = g_strjoin ("-", NULL);
if (s == NULL || (strcmp (s, "") != 0))
- return "Failed to join empty arguments";
+ return FAILED ("Failed to join empty arguments");
g_free (s);
return OK;
@@ -136,7 +136,7 @@
if (strcmp ("hola", str)) {
fprintf (stderr, "%s\n", str);
g_free (str);
- return "Failed.";
+ return FAILED ("Failed.");
}
g_free (str);
return OK;
@@ -151,7 +151,7 @@
if (strcmp ("hola", str)) {
fprintf (stderr, "%s\n", str);
g_free (str);
- return "Failed.";
+ return FAILED ("Failed.");
}
g_free (str);
return OK;
@@ -166,12 +166,35 @@
if (strcmp ("hola", str)) {
fprintf (stderr, "%s\n", str);
g_free (str);
- return "Failed.";
+ return FAILED ("Failed.");
}
g_free (str);
return OK;
}
+#define urit(so,j) do { s = g_filename_to_uri (so, NULL, NULL); if (strcmp (s,
j) != 0) return FAILED("Got %s expected %s", s, j); g_free (s); } while (0);
+
+#define errit(so) do { s = g_filename_to_uri (so, NULL, NULL); if (s != NULL)
return FAILED ("got %s, expected NULL", s); } while (0);
+
+RESULT
+test_filename_to_uri ()
+{
+ char *s;
+
+ urit ("/a", "file:///a");
+ urit ("/home/miguel", "file:///home/miguel");
+ urit ("/home/mig uel", "file:///home/mig%20uel");
+ urit ("/\303\241", "file:///%C3%A1");
+ urit ("/\303\241/octal", "file:///%C3%A1/octal");
+ urit ("/%", "file:///%25");
+ urit
("/\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032",
"file:///%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F%10%11%12%13%14%15%16%17%18%19%1A");
+
+ errit ("a");
+ errit ("./hola");
+
+ return OK;
+}
+
static Test strutil_tests [] = {
{"g_strfreev", test_strfreev},
{"g_strconcat", test_concat},
@@ -181,6 +204,7 @@
{"g_strchug", test_strchug},
{"g_strchomp", test_strchomp},
{"g_strstrip", test_strstrip},
+ {"g_filename_to_uri", test_filename_to_uri},
{NULL, NULL}
};
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches