Changeset cb1759e for util.c


Ignore:
Timestamp:
Jan 5, 2011, 1:34:51 PM (13 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
release-1.7
Children:
b4f7576
Parents:
a359456
git-author:
David Benjamin <davidben@mit.edu> (12/14/10 23:52:42)
git-committer:
David Benjamin <davidben@mit.edu> (01/05/11 13:34:51)
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

    ra359456 rcb1759e  
    234234}
    235235
     236/* Appends a quoted version of arg suitable for placing in a
     237 * command-line to a GString. Does not append a space. */
     238void owl_string_append_quoted_arg(GString *buf, const char *arg)
     239{
     240  const char *argp;
     241  if (arg[0] == '\0') {
     242    /* Quote the empty string. */
     243    g_string_append(buf, "''");
     244  } else if (arg[strcspn(arg, "'\" \n\t")] == '\0') {
     245    /* If there are no nasty characters, return as-is. */
     246    g_string_append(buf, arg);
     247  } else if (!strchr(arg, '\'')) {
     248    /* Single-quote if possible. */
     249    g_string_append_c(buf, '\'');
     250    g_string_append(buf, arg);
     251    g_string_append_c(buf, '\'');
     252  } else {
     253    /* Nasty case: double-quote, but change all internal "s to "'"'"
     254     * so that they are single-quoted because we're too cool for
     255     * backslashes.
     256     */
     257    g_string_append_c(buf, '"');
     258    for (argp = arg; *argp; argp++) {
     259      if (*argp == '"')
     260        g_string_append(buf, "\"'\"'\"");
     261      else
     262        g_string_append_c(buf, *argp);
     263    }
     264    g_string_append_c(buf, '"');
     265  }
     266}
     267
     268/* Returns a quoted version of arg suitable for placing in a
     269 * command-line. Result should be freed with owl_free. */
     270char *owl_arg_quote(const char *arg)
     271{
     272  GString *buf = g_string_new("");;
     273  owl_string_append_quoted_arg(buf, arg);
     274  return g_string_free(buf, false);
     275}
     276
    236277/* caller must free the return */
    237278char *owl_util_minutes_to_timestr(int in)
Note: See TracChangeset for help on using the changeset viewer.