Back to index

tetex-bin  3.0
string-utils.h
Go to the documentation of this file.
00001 #ifndef STRING_UTILS_H_
00002 #define STRING_UTILS_H_
00003 
00004 #include "xdvi-config.h"
00005 #include "kpathsea/c-vararg.h"
00006 #include "my-vsnprintf.h"
00007 
00008 extern Boolean str_is_prefix(const char *, const char *, Boolean case_sensitive);
00009 extern Boolean str_is_suffix(const char *, const char *, Boolean case_sensitive);
00010 extern Boolean is_spaces_only(const char *);
00011 
00012 extern char *my_stristr(const char *haystack, const char *needle);
00013 
00014 typedef enum { USE_DVI_PATH, USE_CWD_PATH } expandPathTypeT;
00015 
00016 /* remove `file:[//localhost/]' prefix from filename, and append `.dvi'
00017    extension if no extension is present */
00018 extern char *filename_append_dvi(const char *fname);
00019 /* prepend a path of type `type' (either CVW, or directory of dvi file)
00020    to `fname' */
00021 extern char *expand_filename(const char *fname, expandPathTypeT type);
00022 
00023 /* do both of the above */
00024 extern char *expand_filename_append_dvi(const char *fname, expandPathTypeT type, Boolean must_exist);
00025 
00026 /* normalize `../' `./' `//' from path */
00027 extern char *canonicalize_path(const char *path);
00028 
00029 extern char *format_arg(const char *fmt, const char *arg, int *match);
00030 extern char *escape_format_arg(const char *arg);
00031 extern char *unquote_arg(const char *fmt, const char *arg, int *match, int *len);
00032 extern char *get_dir_component(const char *path);
00033 extern char **get_separated_list(const char *source, const char *sep, Boolean do_unquote);
00034 extern const char *find_format_str(const char *input, const char *fmt);
00035 extern Boolean src_compare(const char *src, int src_len, const char *target, const char *path, size_t path_len);
00036 extern char *shell_escape_string(const char *str);
00037 extern void replace_extension(const char *fname, const char *extension, char *buf, size_t name_len);
00038 extern int length_of_int(int n);
00039 
00040 extern char *get_string_va(const char *fmt, ...);
00041 extern double my_atof(const char *str);
00042 
00043 /*
00044  * Print `fmt' to a dynamically allocated string `buf'. Uses VSNPRINTF
00045  * in a way that should work with all of glibc <= 2.0.6, >= 2.1, and
00046  * the implementation in my-vsnprintf.c. We cannot invoke va_start()/va_end()
00047  * in a wrapper function since we may need to invoke vsnprintf() several
00048  * times, and each call invalidates argp; that's why this is a macro ...
00049  */
00050 #define XDVI_GET_STRING_ARGP(buf, fmt)                                                     \
00051     do {                                                                            \
00052         va_list argp;                                                                      \
00053        int CHUNK_LEN = 128;                                                         \
00054        int curr_len = CHUNK_LEN, need_len;                                                 \
00055                                                                                     \
00056        buf = NULL;                                                                  \
00057                                                                                     \
00058        for (;;) {                                                                   \
00059             /* see man page for vsnprintf: for glibc 2.0, <need_len> is */                 \
00060             /* -1 if <buf> had been trucated to <curr_len>; for glibc 2.1, */                     \
00061             /* it's the total length of <fmt>; so the following works */                   \
00062             /* with both versions: */                                                      \
00063            buf = xrealloc(buf, curr_len);                                           \
00064                                                                                     \
00065            va_start(argp, fmt);                                                     \
00066            need_len = VSNPRINTF(buf, curr_len, fmt, argp);                                 \
00067             va_end(argp);                                                           \
00068                                                                                     \
00069            if (need_len > -1 && need_len < curr_len) {  /* <curr_len> was large enough */  \
00070               break;                                                                \
00071            }                                                                        \
00072            if (need_len > -1) {    /* now need_len + 1 is the exact space needed */ \
00073               curr_len = need_len + 1;                                              \
00074            }                                                                        \
00075            else {    /* try again with larger <len> */                              \
00076               curr_len += CHUNK_LEN;                                                       \
00077            }                                                                        \
00078        }                                                                            \
00079     } while (0)                                                                            \
00080 
00081 #endif /* STRING_UTILS_H_ */