示例#1
0
void utility_formatter::insert_escaped(const std::string& s,
    const quote_types qt, const spacing_types st) const {
    const space_scope scope(st, stream_);
    stream_ << escape_quote(s, qt);
}
示例#2
0
/**
 * Builds the line to execute by the handler.
 *
 * Takes the handler from the config file and the filename, and mates them
 * together, placing the filename where appropriate.
 *
 * It will replace any instance of %% with the filename.  If no %% exists in
 * the handler parameter, it will add the filename to the end of the handler
 * with a space.  The filename will always be surrounded by double quotes.
 *
 * This returns a newly allocated string that the caller is expected to 
 * free.
 */
static char* build_exec_line(char* handler, char* path, char* name)
{
    char* sanefilename;
    char* sanepath;
    char* sanename;
    char* handlerline;
    char* replacename;
    char* percentmod = NULL;
    char* postpercent = NULL;
    char* percentloc = NULL;
    int didreplace = 0;
    int percentoffset;
    int reallocsize;

    /* build filename with quotes */
    sanepath = escape_quote(path);
    sanename = escape_quote(name);
    sanefilename = malloc(strlen(sanepath) + strlen(sanename) + 4);
    sprintf(sanefilename, "\"%s/%s\"", sanepath, sanename);

    handlerline = strdup(handler); 

    /* now replace all %% that occur */
    while ((percentloc = strstr(handlerline, "%")))
    {
        percentmod = percentloc+1;

        /* parse the substitution string, and replace it as follows:
           %F is replaced with only the filename being acted upon
           %D is replaced with only the path to the above file
           %% is replaced with %D/%F */
        if (percentmod[0] == '%')
            replacename = sanefilename;
        else if (percentmod[0] == 'F' || percentmod[0] == 'f')
            replacename = sanename;
        else if (percentmod[0] == 'D' || percentmod[0] == 'd')
            replacename = sanepath;
        else
        {
            log_write("Error: %%%c is not a valid substitution string\n",percentmod[0]);
        }

        percentoffset = percentloc - handlerline;
        didreplace = 1;
        postpercent = strdup(percentloc+2);

        /* size is length of original string minus size of %% plus size of file plus \0 */
        reallocsize = strlen(handlerline) - 2 + strlen(replacename) + 1;
        handlerline = realloc(handlerline, reallocsize);

        /* make sure we get the whole filename including the \0 */
        strncpy(handlerline+percentoffset, replacename, strlen(replacename) + 1);
        strncat(handlerline, postpercent, strlen(postpercent));
        free(postpercent);
    }

    /* if no replacements, we must tack it on the end */
    if (!didreplace)
    {
        handlerline = realloc(handlerline, strlen(handlerline) + 1 + strlen(sanefilename) + 1);
        strcat(handlerline, " ");
        strcat(handlerline, sanefilename);
    }

    free(sanefilename);
    free(sanepath);
    free(sanename);

    return handlerline;
}