1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
/*
Copyright (C) 2003-2004 Douglas Thain and the University of Wisconsin
Copyright (C) 2005- The University of Notre Dame
This software is distributed under the GNU General Public License.
See the file COPYING for details.
*/
#ifndef STRINGTOOLS_H
#define STRINGTOOLS_H
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include <stdarg.h>
typedef char *(*string_subst_lookup_t) (const char *name, void *arg);
/** Comparison function for an array of strings.
* This is useful with qsort(3) and list_sort().
*/
int string_compare(const void *p1, const void *p2);
/** Takes a command string and escapes special characters in the Shell Command
language. Mallocs space for new string and does not modify original string.
Characters dollar-sign $, backtick `, backslash \, and double-quote " are
escaped.
@param str Command string presented to be escaped.
@return String with special characters escaped.
*/
char *string_escape_shell (const char *str);
/** Takes a command string and escapes quote characters.
Mallocs space for new string and does not modify original string.
The resulting string is wrapped in double quotes,
and will be subject to shell expansion.
@param str Command string presented to be quoted.
@return String with characters quoted.
*/
char *string_quote_shell (const char *str);
/** Takes a command string, escapes double quotes with another double
quote, and escapes single quotes with two additional single quotes.
This is a utilized when putting wrapped and nested commands in Condor.
@param str Command string presented to be escaped.
@return String with special characters escaped.
*/
char *string_escape_condor( const char *str);
void string_chomp(char *str);
int whole_string_match_regex(const char *text, const char *pattern);
int string_match_regex(const char *text, const char *pattern);
int string_match(const char *pattern, const char *text);
char *string_front(const char *str, int max);
const char *string_back(const char *str, int max);
char *string_metric(double value, int power_needed, char *buffer);
int64_t string_metric_parse(const char *str);
time_t string_time_parse(const char *str);
int string_split(char *str, int *argc, char ***argv);
int string_split_quotes(char *str, int *argc, char ***argv);
char *string_pad_right(char *str, unsigned int length);
char *string_pad_left(char *str, int length);
void string_cookie(char *str, int length);
char *string_subst(char *value, string_subst_lookup_t lookup, void *arg);
int string_prefix_is(const char *string, const char *prefix);
int string_suffix_is(const char *string, const char *suffix);
/** Appends second to first, both null terminated strings. Returns the new
formed string. First argument is reallocated with realloc.
@param first Null terminated string.
@param second Null terminated string.
@return Null terminated string concatenating second to first.
*/
char *string_combine(char *first, const char *second);
char *string_combine_multi(char *first, ...);
char *string_signal(int sig);
void string_tolower(char *str);
void string_toupper(char *str);
int string_isspace(const char *str);
int string_is_integer(const char *str, long long *integer_value );
int string_is_float(const char *str, double *double_value );
void string_replace_backslash_codes(const char *instr, char *outstr);
/** Replace instances of %% in a string with the string 'replace'.
To escape this behavior, %%%% becomes %%.
(Backslash it not used as the escape, as it would interfere with shell escapes.)
This function works like realloc: the string str must be created by malloc
and may be freed and reallocated. Therefore, always invoke it like this:
x = replace_percents(x,replace);
@param str Base string to have percents replaced within.
@param replace String used to replace %%.
@return The base string with replacements.
*/
char *string_replace_percents( const char *str, const char *replace );
int string_equal(const char *str1, const char *str2);
int strpos(const char *str, char c);
int strrpos(const char *str, char c);
int getDateString(char *str);
int string_null_or_empty(const char *str);
/** Returns a heap allocated freeable string formatted using sprintf.
@param fmt Format string passed to sprintf.
@param ... Variable arguments passed to sprintf.
@return The formatted string.
*/
char *string_format (const char *fmt, ...)
__attribute__ (( format(printf,1,2) ));
/** Writes a string formatted using snprintf. It is an error if the string is longer than the buffer provided.
@param str Output string buffer, passed as first argument of snprintf.
@param max Maximum number of characters to write to str, counting the final '\0'.
@param fmt Format string passed to snprintf.
@param ... Variable arguments passed to snprintf
@return The number of character written, not counting the final '\0'.
*/
int string_nformat(char *str, const size_t max, const char *fmt, ...);
char *string_trim(char *s, int(func)(int));
char *string_trim_spaces(char *s);
char *string_trim_quotes(char *s);
/** Converts a string to a boolean value. "true", "yes", and "N">0 are,
* case-insensitive, true. Everything else (including NULL) is false.
* @param str A boolean, possibly NULL, string.
* @return True or false.
*/
int string_istrue(const char *str);
/**
Apply a wrapper to a given command. If the wrapper_command contains {}, do the substitution there. Otherwise, just append the command to the wrapper with an extra space.
Example:
<pre>
string_wrap_command( "ls -l", "strace -o trace" ) -> "strace -o trace ls -l"
string_wrap_command( "ls -l", "strace {} > output" ) -> "strace ls -la > output"
string_wrap_command( "ls -l", 0 ) -> "ls -l"
</pre>
@param command The original command.
@param wrapper_command The command to wrap around it.
@result The combined command, returned as a newly allocated string.
*/
char * string_wrap_command( const char *command, const char *wrapper_command );
#ifndef CCTOOLS_OPSYS_LINUX
char *strsep(char **stringp, const char *delim);
#endif
char *strnchr (const char *s, int c);
#endif
|