1 /* 2 * util.h 3 * Provides miscellaneous utility functions - any idiom which crops up more 4 * than a few times, but doesn't belong within another class or library ends up here. 5 * 6 * Copyright (c) 2004 by Alastair M. Robinson 7 * Distributed under the terms of the GNU General Public License - 8 * see the file named "COPYING" for more details. 9 * 10 */ 11 12 #ifndef UTIL_H 13 #define UTIL_H 14 15 #include <string> 16 17 // Filesystem Utilities 18 19 // Function to creates a given directory if it doesn't already exist 20 bool CreateDirIfNeeded(const char *dirname); 21 22 // Functions to check whether a given file or directory exist 23 bool CheckFileExists(const char *filename); 24 #define CheckDirExists(x) CheckFileExists(x) 25 26 // CheckSettingsDir prepends the current home directory, attempts to create 27 // the given directory if it doesn't already exist. Returns false only if 28 // the home directory isn't availble. 29 bool CheckSettingsDir(const char *dirname); 30 31 // Compares two files for binary equality 32 bool CompareFiles(const char *fn1,const char *fn2); 33 34 35 // Open a file from a utf-8-encoded filename. 36 // On Unix, this is a straight passthrough to fopen(). 37 // On Win32 the filename is will converted to wchar_t, then opened with _wfopen 38 // if FOpenUTF8EnableTransation() has been called. 39 // This allows client code to cope with filenames from GTK+ (in UTF-8) and also 40 // from the command line. 41 FILE *FOpenUTF8(const char *name,const char *mode); 42 void FOpenUTF8EnableTranslation(); 43 void FOpenUTF8DisableTranslation(); 44 45 46 // Filename handling utilities 47 48 // Attempts to construct a filename given a root, a suffix and an extension. 49 // Any extension on the root is removed if present. Useful for creating 50 // output filenames of the form <root><channel>.tif or suchlike. 51 // Result must be free()ed when no longer needed. 52 char *BuildFilename(const char *root,const char *suffix,const char *fileext); 53 54 // Prepends the current working directory to a relative filename. 55 // Result should be free()ed when no longer needed. 56 char *BuildAbsoluteFilename(const char *fname); 57 58 // Given an input filename such as "out.tif", constructs a filename 59 // of the form out_<serialno>.tif. 60 // The max parameter, if provided, is used to set the number of digits which 61 // will be printed. 62 // The resulting string should be free()ed when no longer needed. 63 char *SerialiseFilename(const char *fname,int serialno,int max=0); 64 65 // Quote a string to make it safe to pass as a command line argument 66 std::string ShellQuote(std::string &in); 67 std::string ShellQuote(const char *in); 68 69 70 // Given a top-level directory, scans recursively looking for an executable, 71 // and returns its path, minus the executable. 72 std::string FindParent(std::string initialpath, std::string program); 73 74 75 76 // String handling utilities 77 78 // A variant of strdup() which is safe to use on a NULL string, in which case it returns 79 // a valid pointer to an empty string. In all cases, the result must be free()ed when no 80 // longer needed. In some cases this reduces the need for special-case handling. 81 char *SafeStrdup(const char *src); 82 83 // A "safe" version of strcat which returns a valid pointer to an empty string 84 // if both parameters are null, a copy of the non-null parameter if the other is 85 // null, or a newly-allocated string containing the concatenated parameters if 86 // both are valid. 87 // In all cases the result is in newly-allocated storage and may (and must!) be 88 // free()ed when no longer needed. 89 char *SafeStrcat(const char *str1,const char *str2); 90 91 // A variant of strcasecmp() which ignores spaces as well as case - such that 92 // "MyString" matches "Mystring", "myString" and "my string". 93 int StrcasecmpIgnoreSpaces(const char *str1,const char *str2); 94 95 96 // Miscellaneous utilities 97 98 // Tests a given string to determine whether it could plausibly be a hostname:port combination. 99 // If it is, stores the hostname part to the string pointer pointed to by hostname 100 // (which must later be free()ed), and the port portion to the int pointed to by port. 101 bool TestHostName(char *str,char **hostname,int *port); 102 103 104 // A random function which returns rand() % max, and is automatically seeded with the 105 // system time when the program starts. 106 107 int RandomSeeded(int max); 108 109 #endif 110