Changeset 2bc6ad35 for util.c


Ignore:
Timestamp:
Jan 2, 2011, 3:35:15 PM (10 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
master, release-1.8, release-1.9
Children:
d222c44
Parents:
d275eb2
git-author:
David Benjamin <davidben@mit.edu> (12/14/10 23:52:42)
git-committer:
David Benjamin <davidben@mit.edu> (01/02/11 15:35:15)
Message:
Add owl_quote_arg and owl_string_append_quoted_arg

Also add unit tests. We don't appear to have an equivalent of
BarnOwl::quote in C that actually works.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • util.c

    rd275eb2 r2bc6ad35  
    194194}
    195195
     196/* Appends a quoted version of arg suitable for placing in a
     197 * command-line to a GString. Does not append a space. */
     198void owl_string_append_quoted_arg(GString *buf, const char *arg)
     199{
     200  const char *argp;
     201  if (arg[0] == '\0') {
     202    /* Quote the empty string. */
     203    g_string_append(buf, "''");
     204  } else if (arg[strcspn(arg, "'\" \n\t")] == '\0') {
     205    /* If there are no nasty characters, return as-is. */
     206    g_string_append(buf, arg);
     207  } else if (!strchr(arg, '\'')) {
     208    /* Single-quote if possible. */
     209    g_string_append_c(buf, '\'');
     210    g_string_append(buf, arg);
     211    g_string_append_c(buf, '\'');
     212  } else {
     213    /* Nasty case: double-quote, but change all internal "s to "'"'"
     214     * so that they are single-quoted because we're too cool for
     215     * backslashes.
     216     */
     217    g_string_append_c(buf, '"');
     218    for (argp = arg; *argp; argp++) {
     219      if (*argp == '"')
     220        g_string_append(buf, "\"'\"'\"");
     221      else
     222        g_string_append_c(buf, *argp);
     223    }
     224    g_string_append_c(buf, '"');
     225  }
     226}
     227
     228/* Returns a quoted version of arg suitable for placing in a
     229 * command-line. Result should be freed with owl_free. */
     230char *owl_arg_quote(const char *arg)
     231{
     232  GString *buf = g_string_new("");;
     233  owl_string_append_quoted_arg(buf, arg);
     234  return g_string_free(buf, false);
     235}
     236
    196237/* caller must free the return */
    197238char *owl_util_minutes_to_timestr(int in)
Note: See TracChangeset for help on using the changeset viewer.