📄 ofstd.h
字号:
/* * * Copyright (C) 2000-2005, OFFIS * * This software and supporting documentation were developed by * * Kuratorium OFFIS e.V. * Healthcare Information and Communication Systems * Escherweg 2 * D-26121 Oldenburg, Germany * * THIS SOFTWARE IS MADE AVAILABLE, AS IS, AND OFFIS MAKES NO WARRANTY * REGARDING THE SOFTWARE, ITS PERFORMANCE, ITS MERCHANTABILITY OR * FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES OR * ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND * PERFORMANCE OF THE SOFTWARE IS WITH THE USER. * * Module: ofstd * * Author: Joerg Riesmeier, Marco Eichelberg * * Purpose: Class for various helper functions * * Last Update: $Author: meichel $ * Update Date: $Date: 2005/12/08 16:06:04 $ * CVS/RCS Revision: $Revision: 1.23 $ * Status: $State: Exp $ * * CVS/RCS Log at end of file * */#ifndef OFSTD_H#define OFSTD_H#include "dcmtk/config/osconfig.h"#include "dcmtk/ofstd/oflist.h" /* for class OFList */#include "dcmtk/ofstd/ofstring.h" /* for class OFString */#include "dcmtk/ofstd/oftypes.h" /* for OFBool */#define INCLUDE_CSTDLIB#define INCLUDE_CSTDIO#define INCLUDE_CSTRING#define INCLUDE_UNISTD#include "dcmtk/ofstd/ofstdinc.h"BEGIN_EXTERN_C#ifdef HAVE_SYS_TYPES_H#include <sys/types.h> /* for size_t */#endifEND_EXTERN_C/*---------------------* * class declaration * *---------------------*//** A class for various helper functions. * This class is used to comprise a number of "global" helper functions. */class OFStandard{ public: // --- string functions --- /** This function copies up to size - 1 characters from the NUL- * terminated string src to dst, NUL-terminating the result. It is * designed to be a safer, more consistent, and less error-prone * replacement for strncpy(3). strlcpy takes the full size of the * buffer (not just the length) and guarantees to NUL-terminate the * result (as long as size is larger than 0). Note that you should * include a byte for the NUL in size. Also note that strlcpy only * operates on true C strings, i. e. src must be NUL-terminated. * @param dst destination buffer of size siz, must not be NULL * @param src source string, must not be NULL * @param siz size of destination buffer * @return the total length of the string the function tried to * create, i.e. strlen(src). While this may seem somewhat * confusing it was done to make truncation detection simple. */ static inline size_t strlcpy(char *dst, const char *src, size_t siz) {#ifdef HAVE_STRLCPY return ::strlcpy(dst, src, siz);#else return my_strlcpy(dst, src, siz);#endif } /** This function appends the NUL-terminated string src to the end of * dst. It will append at most size - strlen(dst) - 1 bytes, NUL- * terminating the result. It is designed to be a safer, more * consistent, and less error-prone replacement for strncat(3). * strlcat takes the full size of the buffer (not just the length) and * guarantees to NUL-terminate the result (as long as size is larger * than 0). Note that you should include a byte for the NUL in size. * Also note that strlcat only operates on true C strings, i. e. dst * and src must be NUL-terminated. * @param dst destination buffer of size siz, must not be NULL * @param src source string, must not be NULL * @param siz size of destination buffer * @return the total length of the string the function tried to * create, i.e. the initial length of dst plus the length of src. * While this may seem somewhat confusing it was done to make * truncation detection simple. */ static inline size_t strlcat(char *dst, const char *src, size_t siz) {#ifdef HAVE_STRLCAT return ::strlcat(dst, src, siz);#else return my_strlcat(dst, src, siz);#endif } // --- file system functions --- /** check whether the given path exists. * This function does not distinguish files from directories (use 'fileExists()' * or 'directoryExists()' if required). * @param pathName name of the path to be checked * @return OFTrue if path exists, OFFalse otherwise */ static OFBool pathExists(const OFString &pathName); /** check whether the given file exists. * This function also checks that the specified path points to file and not to * a directory (or the like). * @param fileName name of the file to be checked * @return OFTrue if file exists, OFFalse otherwise */ static OFBool fileExists(const OFString &fileName); /** check whether the given directory exists. * This function also checks that the specified path points to directory and * not to a file (or the like). * @param dirName name of the directory to be checked * @return OFTrue if directory exists, OFFalse otherwise */ static OFBool dirExists(const OFString &dirName); /** check whether the given path is readable. * This function works for both files and directories. * @param pathName name of the path to be checked * @return OFTrue if path is readable, OFFalse otherwise */ static OFBool isReadable(const OFString &pathName); /** check whether the given path is writeable. * This function works for both files and directories. * @param pathName name of the path to be checked * @return OFTrue if path is writeable, OFFalse otherwise */ static OFBool isWriteable(const OFString &pathName); /** normalize the given directory name. * Removes trailing path separators from the directory name. If the resulting * directory name is an empty string and the flag 'allowEmptyDirName' is OFFalse * the directory name is set to "." (current directory). If the resulting directory * name is "." and the flag 'allowEmptyDirName' is OFTrue the directory name is set * to an empty string. * @param result string variable in which the resulting directory name is stored * @param dirName directory name to be normalized * @param allowEmptyDirName flag indicating whether an empty directory name is allowed * @return reference to the resulting directory name (same as 'result') */ static OFString &normalizeDirName(OFString &result, const OFString &dirName, const OFBool allowEmptyDirName = OFFalse); /** combine the given directory and file name. * Normalizes the directory name and appends the file name (with a path separator) * if not empty. If both 'dirName' and 'fileName' are empty strings and the flag * 'allowEmptyDirName' is OFFalse the resulting path name is set to "." (current * directory). If 'dirName' is "." and the flag 'allowEmptyDirName' is OFTrue an * empty directory name is used. * NB: This function neither checks whether the given 'dirName' exists nor whether * the resulting path name points to a valid or existing file name. * @param result string variable in which the resulting path name is stored * @param dirName directory name to be combined with the file name * @param fileName file name to be combined with the directory name * @param allowEmptyDirName flag indicating whether an empty directory name is allowed * @return reference to the resulting path name (same as 'result') */ static OFString &combineDirAndFilename(OFString &result, const OFString &dirName, const OFString &fileName, const OFBool allowEmptyDirName = OFFalse); /** scan a given directory recursively and add all filenames found to a list * @param directory name of the directory to be scanned * @param fileList list to which the filenames are added. * Please note that the list is not not cleared automatically. * @param pattern optional wildcard pattern used to match the filenames against. * By default all files match. In order to work under Unix the system function * fnmatch() is required. * @param dirPrefix optional prefix added to the directory name. * This prefix will, however, not be part of the filenames added to the list. * @return number of new files added to the list */ static size_t searchDirectoryRecursively(const OFString &directory, OFList<OFString> &fileList, const OFString &pattern /*= ""*/, // default parameter value not const OFString &dirPrefix /*= ""*/); // supported by Sun CC 2.0.1 :-/ // --- other functions --- /** convert character string to HTML/XML mnenonic string. * Characters with special meaning for HTML/XML (e.g. '<' and '&') are replaced by the * corresponding mnenonics (e.g. "<" and "&"). If flag 'convertNonASCII' is OFTrue * all characters > #127 are also converted (useful if only HTML 3.2 is supported which does * not allow to specify the character set). ** @param sourceString source string to be converted * @param markupString reference to character string where the result should be stored * @param convertNonASCII convert non-ASCII characters (> #127) to numeric value (&#nnn;) * if OFTrue * @param xmlMode convert to XML markup string if OFTrue, HTML string otherwise. * LF and CR are encoded as " " and " " in XML mode, the flag 'newlineAllowed' * has no meaning in this case. * @param newlineAllowed optional flag indicating whether newlines are allowed or not. * If they are allowed the text "<br>" is used, "¶" otherwise. The following * combinations are accepted: LF, CR, LF CR, CF LF. ** @return reference to resulting 'markupString' (might be empty if 'sourceString' was empty) */ static const OFString &convertToMarkupString(const OFString &sourceString, OFString &markupString, const OFBool convertNonASCII = OFFalse, const OFBool xmlMode = OFTrue, const OFBool newlineAllowed = OFFalse); /** encode binary data according to "Base64" as described in RFC 2045 (MIME). * Basic algorithm: groups of 3 bytes from the binary input are coded as groups of 4 bytes in * the textual output. The input data is 'padded' with zeros to create a length that is an * even multiple of 3. A special character ('=') is used to denote padding so that the output * can be decoded back to its exact size. * If the input data is NULL an empty string is returned. ** @param data buffer with binary data to be encoded (big endian required!) * @param length length of the input data buffer (in bytes) * @param result reference to resulting string variable (Base64 encoded) * @param width maximum number of characters per line in the output string * (default: 0 = no line breaks, typical for MIME = 72) ** @return reference to the resulting string */ static const OFString &encodeBase64(const unsigned char *data, const size_t length, OFString &result, const size_t width = 0); /** decode "Base64" encoded string. * Any character that does not belong to the Base64 alphabet (0..9, A..Z, a..z, + and /) is * ignored when decoding the input string. This is especially true for line breaks which are * usually contained in MIME (RFC 2045) encoded streams (see above). The first occurence of * a '=' character is taken as evidence that the end of the data has been reached. * NB: The memory buffer in which the binary output is stored is allocated inside this function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -