00001 /* 00002 * 00003 * Copyright (C) 2000-2010, OFFIS e.V. 00004 * All rights reserved. See COPYRIGHT file for details. 00005 * 00006 * This software and supporting documentation were developed by 00007 * 00008 * OFFIS e.V. 00009 * R&D Division Health 00010 * Escherweg 2 00011 * D-26121 Oldenburg, Germany 00012 * 00013 * 00014 * Module: ofstd 00015 * 00016 * Author: Joerg Riesmeier, Marco Eichelberg 00017 * 00018 * Purpose: Class for various helper functions 00019 * 00020 * Last Update: $Author: joergr $ 00021 * Update Date: $Date: 2010-10-14 13:15:50 $ 00022 * CVS/RCS Revision: $Revision: 1.42 $ 00023 * Status: $State: Exp $ 00024 * 00025 * CVS/RCS Log at end of file 00026 * 00027 */ 00028 00029 00030 #ifndef OFSTD_H 00031 #define OFSTD_H 00032 00033 #include "dcmtk/config/osconfig.h" 00034 #include "dcmtk/ofstd/oflist.h" /* for class OFList */ 00035 #include "dcmtk/ofstd/ofstring.h" /* for class OFString */ 00036 #include "dcmtk/ofstd/oftypes.h" /* for OFBool */ 00037 #include "dcmtk/ofstd/ofcond.h" /* for OFCondition */ 00038 00039 #define INCLUDE_CSTDLIB 00040 #define INCLUDE_CSTDIO 00041 #define INCLUDE_CSTRING 00042 #define INCLUDE_UNISTD 00043 #include "dcmtk/ofstd/ofstdinc.h" 00044 00045 BEGIN_EXTERN_C 00046 #ifdef HAVE_SYS_TYPES_H 00047 #include <sys/types.h> /* for size_t */ 00048 #endif 00049 END_EXTERN_C 00050 00051 00052 /*---------------------* 00053 * class declaration * 00054 *---------------------*/ 00055 00059 class OFStandard 00060 { 00061 00062 public: 00063 00064 // --- type definitions --- 00065 00068 enum E_MarkupMode 00069 { 00071 MM_HTML, 00073 MM_HTML32, 00075 MM_XHTML, 00077 MM_XML 00078 }; 00079 00080 // --- string functions --- 00081 00097 static inline size_t strlcpy(char *dst, const char *src, size_t siz) 00098 { 00099 #ifdef HAVE_STRLCPY 00100 return ::strlcpy(dst, src, siz); 00101 #else 00102 return my_strlcpy(dst, src, siz); 00103 #endif 00104 } 00105 00123 static inline size_t strlcat(char *dst, const char *src, size_t siz) 00124 { 00125 #ifdef HAVE_STRLCAT 00126 return ::strlcat(dst, src, siz); 00127 #else 00128 return my_strlcat(dst, src, siz); 00129 #endif 00130 } 00131 00142 static const char *strerror(const int errnum, 00143 char *buf, 00144 const size_t buflen); 00145 00151 static OFString &toUpper(OFString &result, 00152 const OFString &value); 00153 00159 static OFString &toUpper(OFString &value); 00160 00166 static OFString &toLower(OFString &result, 00167 const OFString &value); 00168 00174 static OFString &toLower(OFString &value); 00175 00176 // --- file system functions --- 00177 00184 static OFBool pathExists(const OFString &pathName); 00185 00192 static OFBool fileExists(const OFString &fileName); 00193 00200 static OFBool dirExists(const OFString &dirName); 00201 00207 static OFBool isReadable(const OFString &pathName); 00208 00214 static OFBool isWriteable(const OFString &pathName); 00215 00227 static OFString &getDirNameFromPath(OFString &result, 00228 const OFString &pathName, 00229 const OFBool assumeDirName = OFTrue); 00230 00242 static OFString &getFilenameFromPath(OFString &result, 00243 const OFString &pathName, 00244 const OFBool assumeFilename = OFTrue); 00245 00257 static OFString &normalizeDirName(OFString &result, 00258 const OFString &dirName, 00259 const OFBool allowEmptyDirName = OFFalse); 00260 00276 static OFString &combineDirAndFilename(OFString &result, 00277 const OFString &dirName, 00278 const OFString &fileName, 00279 const OFBool allowEmptyDirName = OFFalse); 00280 00291 static OFCondition removeRootDirFromPathname(OFString &result, 00292 const OFString &rootDir, 00293 const OFString &pathName, 00294 const OFBool allowLeadingPathSeparator = OFTrue); 00295 00308 static size_t searchDirectoryRecursively(const OFString &directory, 00309 OFList<OFString> &fileList, 00310 const OFString &pattern /*= ""*/, // default parameter value not 00311 const OFString &dirPrefix /*= ""*/, // supported by Sun CC 2.0.1 :-/ 00312 const OFBool recurse = OFTrue); 00313 00318 static OFBool deleteFile(const OFString &filename); 00319 00324 static size_t getFileSize(const OFString &filename); 00325 00326 // --- other functions --- 00327 00336 static OFBool checkForMarkupConversion(const OFString &sourceString, 00337 const OFBool convertNonASCII = OFFalse); 00338 00359 static OFCondition convertToMarkupStream(STD_NAMESPACE ostream &out, 00360 const OFString &sourceString, 00361 const OFBool convertNonASCII = OFFalse, 00362 const E_MarkupMode markupMode = MM_XML, 00363 const OFBool newlineAllowed = OFFalse); 00364 00385 static const OFString &convertToMarkupString(const OFString &sourceString, 00386 OFString &markupString, 00387 const OFBool convertNonASCII = OFFalse, 00388 const E_MarkupMode markupMode = MM_XML, 00389 const OFBool newlineAllowed = OFFalse); 00390 00404 static OFCondition encodeBase64(STD_NAMESPACE ostream &out, 00405 const unsigned char *data, 00406 const size_t length, 00407 const size_t width = 0); 00408 00422 static const OFString &encodeBase64(const unsigned char *data, 00423 const size_t length, 00424 OFString &result, 00425 const size_t width = 0); 00426 00440 static size_t decodeBase64(const OFString &data, 00441 unsigned char *&result); 00442 00477 static double atof(const char *s, 00478 OFBool *success = NULL); 00479 00503 static void ftoa(char *target, 00504 size_t targetSize, 00505 double value, 00506 unsigned int flags = 0, 00507 int width = 0, 00508 int precision = -1); 00509 00514 00516 static const unsigned int ftoa_format_e; 00517 00519 static const unsigned int ftoa_format_f; 00520 00522 static const unsigned int ftoa_uppercase; 00523 00528 static const unsigned int ftoa_alternate; 00529 00531 static const unsigned int ftoa_leftadj; 00532 00534 static const unsigned int ftoa_zeropad; 00535 00537 00543 static inline unsigned int sleep(unsigned int seconds) 00544 { 00545 #if defined(HAVE_SLEEP) && !defined(HAVE_WINDOWS_H) 00546 // we only use this call if HAVE_WINDOWS_H is undefined because 00547 // MinGW has sleep() but no prototype 00548 return ::sleep(seconds); 00549 #else 00550 return my_sleep(seconds); 00551 #endif 00552 } 00553 00557 static long getProcessID(); 00558 00564 static inline OFBool check32BitAddOverflow(const Uint32 summand1, 00565 const Uint32 summand2) 00566 { 00567 return (0xffffffff - summand1 < summand2); 00568 } 00569 00570 private: 00571 00580 static size_t my_strlcpy(char *dst, const char *src, size_t siz); 00581 00590 static size_t my_strlcat(char *dst, const char *src, size_t siz); 00591 00597 static unsigned int my_sleep(unsigned int seconds); 00598 }; 00599 00600 00601 #endif 00602 00603 00604 /* 00605 * 00606 * CVS/RCS Log: 00607 * $Log: ofstd.h,v $ 00608 * Revision 1.42 2010-10-14 13:15:50 joergr 00609 * Updated copyright header. Added reference to COPYRIGHT file. 00610 * 00611 * Revision 1.41 2010-06-02 12:54:28 joergr 00612 * Introduced new helper function strerror() which is used as a wrapper to the 00613 * various approaches found on different systems. 00614 * 00615 * Revision 1.40 2010-05-20 09:20:13 joergr 00616 * Added new method for determining the size of a given file (in bytes). 00617 * 00618 * Revision 1.39 2010-04-26 12:22:30 uli 00619 * Fixed a some minor doxygen warnings. 00620 * 00621 * Revision 1.38 2010-01-21 14:43:27 joergr 00622 * Added stream variant of method convertToMarkupString(). 00623 * 00624 * Revision 1.37 2010-01-20 13:49:47 uli 00625 * Added OFStandard::getProcessID(). 00626 * 00627 * Revision 1.36 2010-01-04 16:02:23 joergr 00628 * Added new method getDirNameFromPath() and enhanced existing method 00629 * getFilenameFromPath(). 00630 * 00631 * Revision 1.35 2009-08-19 10:43:37 joergr 00632 * Added new string helper functions toUpper() and toLower(). 00633 * 00634 * Revision 1.34 2009-04-27 14:26:00 joergr 00635 * Added comment on absolute path names e.g. in UNC syntax. 00636 * 00637 * Revision 1.33 2009-03-13 09:47:20 joergr 00638 * Added new helper function getFilenameFromPath(). 00639 * 00640 * Revision 1.32 2009-03-05 13:33:12 onken 00641 * Added helper function that checks whether a given Uint32 addition would 00642 * result in an overflow. 00643 * 00644 * Revision 1.31 2008-08-28 10:44:36 onken 00645 * Introduced deleteFile() method. 00646 * 00647 * Revision 1.30 2008-07-15 09:49:33 joergr 00648 * Removed unused function OFStandard::stringMatchesCharacterSet(). 00649 * 00650 * Revision 1.29 2008-04-28 12:03:24 joergr 00651 * Adapted OFStandard::checkForMarkupConversion() to the new behavior of 00652 * parameter "convertNonASCII" of OFStandard::convertToMarkupString(). 00653 * Fixed API documentation of OFStandard::convertToMarkupString(). 00654 * 00655 * Revision 1.28 2007/11/15 16:11:43 joergr 00656 * Introduced new markup mode for convertToMarkupString() that is used to 00657 * distinguish between HTML, HTML 3.2, XHTML and XML. 00658 * 00659 * Revision 1.27 2007/06/26 16:21:14 joergr 00660 * Added new variant of encodeBase64() method that outputs directly to a stream 00661 * (avoids using a memory buffer for large binary data). 00662 * 00663 * Revision 1.26 2007/03/09 14:54:59 joergr 00664 * Added optional parameter "recurse" to searchDirectoryRecursively(). 00665 * 00666 * Revision 1.25 2007/02/20 13:12:27 joergr 00667 * Added function that removes a given prefix from a pathname (e.g. root dir). 00668 * 00669 * Revision 1.24 2006/10/13 10:04:03 joergr 00670 * Added new helper function that allows to check whether the conversion to an 00671 * HTML/XML markup string is required. 00672 * 00673 * Revision 1.23 2005/12/08 16:06:04 meichel 00674 * Changed include path schema for all DCMTK header files 00675 * 00676 * Revision 1.22 2004/08/03 11:45:42 meichel 00677 * Headers libc.h and unistd.h are now included via ofstdinc.h 00678 * 00679 * Revision 1.21 2004/04/16 12:43:26 joergr 00680 * Restructured code to avoid default parameter values for "complex types" like 00681 * OFString. Required for Sun CC 2.0.1. 00682 * 00683 * Revision 1.20 2003/12/05 10:37:41 joergr 00684 * Removed leading underscore characters from preprocessor symbols (reserved 00685 * symbols). Updated copyright date where appropriate. 00686 * 00687 * Revision 1.19 2003/08/12 13:10:10 joergr 00688 * Improved implementation of normalizeDirName(). 00689 * 00690 * Revision 1.18 2003/07/17 14:53:24 joergr 00691 * Added new function searchDirectoryRecursively(). 00692 * Updated documentation to get rid of doxygen warnings. 00693 * 00694 * Revision 1.17 2003/07/04 13:31:51 meichel 00695 * Fixed issues with compiling with HAVE_STD_STRING 00696 * 00697 * Revision 1.16 2003/07/03 14:23:50 meichel 00698 * Minor changes to make OFStandard::sleep compile on MinGW 00699 * 00700 * Revision 1.15 2003/06/06 09:43:54 meichel 00701 * Added static sleep function in class OFStandard. This replaces the various 00702 * calls to sleep(), Sleep() and usleep() throughout the toolkit. 00703 * 00704 * Revision 1.14 2003/04/17 15:50:51 joergr 00705 * Replace LF and CR by and in XML mode instead of ¶ (para). 00706 * 00707 * Revision 1.13 2003/03/12 14:57:47 joergr 00708 * Added apostrophe (') to the list of characters to be replaced by the 00709 * corresponding HTML/XML mnenonic. 00710 * 00711 * Revision 1.12 2002/12/13 13:45:33 meichel 00712 * Removed const from decodeBase64() return code, needed on MIPSpro 00713 * 00714 * Revision 1.11 2002/12/05 13:49:36 joergr 00715 * Moved definition of ftoa() processing flags to implementation file to avoid 00716 * compiler errors (e.g. on Sun CC 2.0.1). 00717 * 00718 * Revision 1.10 2002/12/04 09:13:00 meichel 00719 * Implemented a locale independent function OFStandard::ftoa() that 00720 * converts double to string and offers all the flexibility of the 00721 * sprintf family of functions. 00722 * 00723 * Revision 1.9 2002/11/27 11:23:06 meichel 00724 * Adapted module ofstd to use of new header file ofstdinc.h 00725 * 00726 * Revision 1.8 2002/07/02 15:17:57 wilkens 00727 * Added function OFStandard::stringMatchesCharacterSet(...). 00728 * 00729 * Revision 1.7 2002/06/20 12:02:38 meichel 00730 * Implemented a locale independent function OFStandard::atof() that 00731 * converts strings to double and optionally returns a status code 00732 * 00733 * Revision 1.6 2002/05/14 08:12:51 joergr 00734 * Added support for Base64 (MIME) encoding and decoding. 00735 * 00736 * Revision 1.5 2002/04/25 09:13:52 joergr 00737 * Moved helper function which converts a conventional character string to an 00738 * HTML/XML mnenonic string (e.g. using "<" instead of "<") from module 00739 * dcmsr to ofstd. 00740 * 00741 * Revision 1.4 2002/04/11 12:06:42 joergr 00742 * Added general purpose routines to check whether a file exists, a path points 00743 * to a directory or a file, etc. 00744 * 00745 * Revision 1.3 2001/12/04 16:57:15 meichel 00746 * Implemented strlcpy and strlcat routines compatible with the 00747 * corresponding BSD libc routines in class OFStandard 00748 * 00749 * Revision 1.2 2001/06/01 15:51:35 meichel 00750 * Updated copyright header 00751 * 00752 * Revision 1.1 2000/03/02 12:42:57 joergr 00753 * Added new class comprising all general purpose helper functions (first 00754 * entry: strlcpy - a mixture of strcpy and strncpy). 00755 * 00756 * 00757 * 00758 */