OFFIS DCMTK  Version 3.6.0
ofstd.h
1 /*
2  *
3  * Copyright (C) 2000-2010, OFFIS e.V.
4  * All rights reserved. See COPYRIGHT file for details.
5  *
6  * This software and supporting documentation were developed by
7  *
8  * OFFIS e.V.
9  * R&D Division Health
10  * Escherweg 2
11  * D-26121 Oldenburg, Germany
12  *
13  *
14  * Module: ofstd
15  *
16  * Author: Joerg Riesmeier, Marco Eichelberg
17  *
18  * Purpose: Class for various helper functions
19  *
20  * Last Update: $Author: joergr $
21  * Update Date: $Date: 2010-10-14 13:15:50 $
22  * CVS/RCS Revision: $Revision: 1.42 $
23  * Status: $State: Exp $
24  *
25  * CVS/RCS Log at end of file
26  *
27  */
28 
29 
30 #ifndef OFSTD_H
31 #define OFSTD_H
32 
33 #include "dcmtk/config/osconfig.h"
34 #include "dcmtk/ofstd/oflist.h" /* for class OFList */
35 #include "dcmtk/ofstd/ofstring.h" /* for class OFString */
36 #include "dcmtk/ofstd/oftypes.h" /* for OFBool */
37 #include "dcmtk/ofstd/ofcond.h" /* for OFCondition */
38 
39 #define INCLUDE_CSTDLIB
40 #define INCLUDE_CSTDIO
41 #define INCLUDE_CSTRING
42 #define INCLUDE_UNISTD
43 #include "dcmtk/ofstd/ofstdinc.h"
44 
45 BEGIN_EXTERN_C
46 #ifdef HAVE_SYS_TYPES_H
47 #include <sys/types.h> /* for size_t */
48 #endif
49 END_EXTERN_C
50 
51 
52 /*---------------------*
53  * class declaration *
54  *---------------------*/
55 
60 {
61 
62  public:
63 
64  // --- type definitions ---
65 
69  {
78  };
79 
80  // --- string functions ---
81 
97  static inline size_t strlcpy(char *dst, const char *src, size_t siz)
98  {
99 #ifdef HAVE_STRLCPY
100  return ::strlcpy(dst, src, siz);
101 #else
102  return my_strlcpy(dst, src, siz);
103 #endif
104  }
105 
123  static inline size_t strlcat(char *dst, const char *src, size_t siz)
124  {
125 #ifdef HAVE_STRLCAT
126  return ::strlcat(dst, src, siz);
127 #else
128  return my_strlcat(dst, src, siz);
129 #endif
130  }
131 
142  static const char *strerror(const int errnum,
143  char *buf,
144  const size_t buflen);
145 
151  static OFString &toUpper(OFString &result,
152  const OFString &value);
153 
159  static OFString &toUpper(OFString &value);
160 
166  static OFString &toLower(OFString &result,
167  const OFString &value);
168 
174  static OFString &toLower(OFString &value);
175 
176  // --- file system functions ---
177 
184  static OFBool pathExists(const OFString &pathName);
185 
192  static OFBool fileExists(const OFString &fileName);
193 
200  static OFBool dirExists(const OFString &dirName);
201 
207  static OFBool isReadable(const OFString &pathName);
208 
214  static OFBool isWriteable(const OFString &pathName);
215 
227  static OFString &getDirNameFromPath(OFString &result,
228  const OFString &pathName,
229  const OFBool assumeDirName = OFTrue);
230 
242  static OFString &getFilenameFromPath(OFString &result,
243  const OFString &pathName,
244  const OFBool assumeFilename = OFTrue);
245 
257  static OFString &normalizeDirName(OFString &result,
258  const OFString &dirName,
259  const OFBool allowEmptyDirName = OFFalse);
260 
276  static OFString &combineDirAndFilename(OFString &result,
277  const OFString &dirName,
278  const OFString &fileName,
279  const OFBool allowEmptyDirName = OFFalse);
280 
292  const OFString &rootDir,
293  const OFString &pathName,
294  const OFBool allowLeadingPathSeparator = OFTrue);
295 
308  static size_t searchDirectoryRecursively(const OFString &directory,
309  OFList<OFString> &fileList,
310  const OFString &pattern /*= ""*/, // default parameter value not
311  const OFString &dirPrefix /*= ""*/, // supported by Sun CC 2.0.1 :-/
312  const OFBool recurse = OFTrue);
313 
318  static OFBool deleteFile(const OFString &filename);
319 
324  static size_t getFileSize(const OFString &filename);
325 
326  // --- other functions ---
327 
336  static OFBool checkForMarkupConversion(const OFString &sourceString,
337  const OFBool convertNonASCII = OFFalse);
338 
359  static OFCondition convertToMarkupStream(STD_NAMESPACE ostream &out,
360  const OFString &sourceString,
361  const OFBool convertNonASCII = OFFalse,
362  const E_MarkupMode markupMode = MM_XML,
363  const OFBool newlineAllowed = OFFalse);
364 
385  static const OFString &convertToMarkupString(const OFString &sourceString,
386  OFString &markupString,
387  const OFBool convertNonASCII = OFFalse,
388  const E_MarkupMode markupMode = MM_XML,
389  const OFBool newlineAllowed = OFFalse);
390 
404  static OFCondition encodeBase64(STD_NAMESPACE ostream &out,
405  const unsigned char *data,
406  const size_t length,
407  const size_t width = 0);
408 
422  static const OFString &encodeBase64(const unsigned char *data,
423  const size_t length,
424  OFString &result,
425  const size_t width = 0);
426 
440  static size_t decodeBase64(const OFString &data,
441  unsigned char *&result);
442 
477  static double atof(const char *s,
478  OFBool *success = NULL);
479 
503  static void ftoa(char *target,
504  size_t targetSize,
505  double value,
506  unsigned int flags = 0,
507  int width = 0,
508  int precision = -1);
509 
514 
516  static const unsigned int ftoa_format_e;
517 
519  static const unsigned int ftoa_format_f;
520 
522  static const unsigned int ftoa_uppercase;
523 
528  static const unsigned int ftoa_alternate;
529 
531  static const unsigned int ftoa_leftadj;
532 
534  static const unsigned int ftoa_zeropad;
535 
537 
543  static inline unsigned int sleep(unsigned int seconds)
544  {
545 #if defined(HAVE_SLEEP) && !defined(HAVE_WINDOWS_H)
546  // we only use this call if HAVE_WINDOWS_H is undefined because
547  // MinGW has sleep() but no prototype
548  return ::sleep(seconds);
549 #else
550  return my_sleep(seconds);
551 #endif
552  }
553 
557  static long getProcessID();
558 
564  static inline OFBool check32BitAddOverflow(const Uint32 summand1,
565  const Uint32 summand2)
566  {
567  return (0xffffffff - summand1 < summand2);
568  }
569 
580  template <typename T>
581  static OFBool __attribute__ ((visibility("default")))
582  safeSubtract(T minuend, T subtrahend, T& difference)
583  {
584  assert ((minuend >= 0) && (subtrahend >= 0)); // assert(!OFnumeric_limits<T>::is_signed);
585  if (minuend < subtrahend) {
586  return OFFalse;
587  } else {
588  difference = minuend - subtrahend;
589  return OFTrue;
590  }
591  }
592 
593  private:
594 
603  static size_t my_strlcpy(char *dst, const char *src, size_t siz);
604 
613  static size_t my_strlcat(char *dst, const char *src, size_t siz);
614 
620  static unsigned int my_sleep(unsigned int seconds);
621 };
622 
623 
624 #endif
625 
626 
627 /*
628  *
629  * CVS/RCS Log:
630  * $Log: ofstd.h,v $
631  * Revision 1.42 2010-10-14 13:15:50 joergr
632  * Updated copyright header. Added reference to COPYRIGHT file.
633  *
634  * Revision 1.41 2010-06-02 12:54:28 joergr
635  * Introduced new helper function strerror() which is used as a wrapper to the
636  * various approaches found on different systems.
637  *
638  * Revision 1.40 2010-05-20 09:20:13 joergr
639  * Added new method for determining the size of a given file (in bytes).
640  *
641  * Revision 1.39 2010-04-26 12:22:30 uli
642  * Fixed a some minor doxygen warnings.
643  *
644  * Revision 1.38 2010-01-21 14:43:27 joergr
645  * Added stream variant of method convertToMarkupString().
646  *
647  * Revision 1.37 2010-01-20 13:49:47 uli
648  * Added OFStandard::getProcessID().
649  *
650  * Revision 1.36 2010-01-04 16:02:23 joergr
651  * Added new method getDirNameFromPath() and enhanced existing method
652  * getFilenameFromPath().
653  *
654  * Revision 1.35 2009-08-19 10:43:37 joergr
655  * Added new string helper functions toUpper() and toLower().
656  *
657  * Revision 1.34 2009-04-27 14:26:00 joergr
658  * Added comment on absolute path names e.g. in UNC syntax.
659  *
660  * Revision 1.33 2009-03-13 09:47:20 joergr
661  * Added new helper function getFilenameFromPath().
662  *
663  * Revision 1.32 2009-03-05 13:33:12 onken
664  * Added helper function that checks whether a given Uint32 addition would
665  * result in an overflow.
666  *
667  * Revision 1.31 2008-08-28 10:44:36 onken
668  * Introduced deleteFile() method.
669  *
670  * Revision 1.30 2008-07-15 09:49:33 joergr
671  * Removed unused function OFStandard::stringMatchesCharacterSet().
672  *
673  * Revision 1.29 2008-04-28 12:03:24 joergr
674  * Adapted OFStandard::checkForMarkupConversion() to the new behavior of
675  * parameter "convertNonASCII" of OFStandard::convertToMarkupString().
676  * Fixed API documentation of OFStandard::convertToMarkupString().
677  *
678  * Revision 1.28 2007/11/15 16:11:43 joergr
679  * Introduced new markup mode for convertToMarkupString() that is used to
680  * distinguish between HTML, HTML 3.2, XHTML and XML.
681  *
682  * Revision 1.27 2007/06/26 16:21:14 joergr
683  * Added new variant of encodeBase64() method that outputs directly to a stream
684  * (avoids using a memory buffer for large binary data).
685  *
686  * Revision 1.26 2007/03/09 14:54:59 joergr
687  * Added optional parameter "recurse" to searchDirectoryRecursively().
688  *
689  * Revision 1.25 2007/02/20 13:12:27 joergr
690  * Added function that removes a given prefix from a pathname (e.g. root dir).
691  *
692  * Revision 1.24 2006/10/13 10:04:03 joergr
693  * Added new helper function that allows to check whether the conversion to an
694  * HTML/XML markup string is required.
695  *
696  * Revision 1.23 2005/12/08 16:06:04 meichel
697  * Changed include path schema for all DCMTK header files
698  *
699  * Revision 1.22 2004/08/03 11:45:42 meichel
700  * Headers libc.h and unistd.h are now included via ofstdinc.h
701  *
702  * Revision 1.21 2004/04/16 12:43:26 joergr
703  * Restructured code to avoid default parameter values for "complex types" like
704  * OFString. Required for Sun CC 2.0.1.
705  *
706  * Revision 1.20 2003/12/05 10:37:41 joergr
707  * Removed leading underscore characters from preprocessor symbols (reserved
708  * symbols). Updated copyright date where appropriate.
709  *
710  * Revision 1.19 2003/08/12 13:10:10 joergr
711  * Improved implementation of normalizeDirName().
712  *
713  * Revision 1.18 2003/07/17 14:53:24 joergr
714  * Added new function searchDirectoryRecursively().
715  * Updated documentation to get rid of doxygen warnings.
716  *
717  * Revision 1.17 2003/07/04 13:31:51 meichel
718  * Fixed issues with compiling with HAVE_STD_STRING
719  *
720  * Revision 1.16 2003/07/03 14:23:50 meichel
721  * Minor changes to make OFStandard::sleep compile on MinGW
722  *
723  * Revision 1.15 2003/06/06 09:43:54 meichel
724  * Added static sleep function in class OFStandard. This replaces the various
725  * calls to sleep(), Sleep() and usleep() throughout the toolkit.
726  *
727  * Revision 1.14 2003/04/17 15:50:51 joergr
728  * Replace LF and CR by &#10; and &#13; in XML mode instead of &#182; (para).
729  *
730  * Revision 1.13 2003/03/12 14:57:47 joergr
731  * Added apostrophe (') to the list of characters to be replaced by the
732  * corresponding HTML/XML mnenonic.
733  *
734  * Revision 1.12 2002/12/13 13:45:33 meichel
735  * Removed const from decodeBase64() return code, needed on MIPSpro
736  *
737  * Revision 1.11 2002/12/05 13:49:36 joergr
738  * Moved definition of ftoa() processing flags to implementation file to avoid
739  * compiler errors (e.g. on Sun CC 2.0.1).
740  *
741  * Revision 1.10 2002/12/04 09:13:00 meichel
742  * Implemented a locale independent function OFStandard::ftoa() that
743  * converts double to string and offers all the flexibility of the
744  * sprintf family of functions.
745  *
746  * Revision 1.9 2002/11/27 11:23:06 meichel
747  * Adapted module ofstd to use of new header file ofstdinc.h
748  *
749  * Revision 1.8 2002/07/02 15:17:57 wilkens
750  * Added function OFStandard::stringMatchesCharacterSet(...).
751  *
752  * Revision 1.7 2002/06/20 12:02:38 meichel
753  * Implemented a locale independent function OFStandard::atof() that
754  * converts strings to double and optionally returns a status code
755  *
756  * Revision 1.6 2002/05/14 08:12:51 joergr
757  * Added support for Base64 (MIME) encoding and decoding.
758  *
759  * Revision 1.5 2002/04/25 09:13:52 joergr
760  * Moved helper function which converts a conventional character string to an
761  * HTML/XML mnenonic string (e.g. using "&lt;" instead of "<") from module
762  * dcmsr to ofstd.
763  *
764  * Revision 1.4 2002/04/11 12:06:42 joergr
765  * Added general purpose routines to check whether a file exists, a path points
766  * to a directory or a file, etc.
767  *
768  * Revision 1.3 2001/12/04 16:57:15 meichel
769  * Implemented strlcpy and strlcat routines compatible with the
770  * corresponding BSD libc routines in class OFStandard
771  *
772  * Revision 1.2 2001/06/01 15:51:35 meichel
773  * Updated copyright header
774  *
775  * Revision 1.1 2000/03/02 12:42:57 joergr
776  * Added new class comprising all general purpose helper functions (first
777  * entry: strlcpy - a mixture of strcpy and strncpy).
778  *
779  *
780  *
781  */
HTML (Hyper Text Markup Language)
Definition: ofstd.h:71
XML (Extensible Markup Language)
Definition: ofstd.h:77
static const unsigned int ftoa_zeropad
pad with zeroes instead of blanks
Definition: ofstd.h:534
static const OFString & convertToMarkupString(const OFString &sourceString, OFString &markupString, const OFBool convertNonASCII=OFFalse, const E_MarkupMode markupMode=MM_XML, const OFBool newlineAllowed=OFFalse)
convert character string to HTML/XHTML/XML mnenonic string.
static OFBool checkForMarkupConversion(const OFString &sourceString, const OFBool convertNonASCII=OFFalse)
check whether conversion to HTML/XML mnenonic string is required.
static void ftoa(char *target, size_t targetSize, double value, unsigned int flags=0, int width=0, int precision=-1)
formats a floating-point number into an ASCII string.
static unsigned int sleep(unsigned int seconds)
makes the current process sleep until seconds seconds have elapsed or a signal arrives which is not i...
Definition: ofstd.h:543
static unsigned int my_sleep(unsigned int seconds)
makes the current process sleep until seconds seconds have elapsed or a signal arrives which is not i...
static OFBool isReadable(const OFString &pathName)
check whether the given path is readable.
static OFBool pathExists(const OFString &pathName)
check whether the given path exists.
static OFString & getDirNameFromPath(OFString &result, const OFString &pathName, const OFBool assumeDirName=OFTrue)
get directory name component from given path name.
static OFString & normalizeDirName(OFString &result, const OFString &dirName, const OFBool allowEmptyDirName=OFFalse)
normalize the given directory name.
static OFString & toUpper(OFString &result, const OFString &value)
returns the upper-case version of a given string
static const char * strerror(const int errnum, char *buf, const size_t buflen)
convert a given error code to a string.
static const unsigned int ftoa_uppercase
Use E, F or G conversion format instead of e, f or g.
Definition: ofstd.h:522
static OFCondition encodeBase64(STD_NAMESPACE ostream &out, const unsigned char *data, const size_t length, const size_t width=0)
encode binary data according to "Base64" as described in RFC 2045 (MIME).
static OFString & toLower(OFString &result, const OFString &value)
returns the lower-case version of a given string
static long getProcessID()
Determines the identification of the running process.
HTML 3.2 (Hyper Text Markup Language)
Definition: ofstd.h:73
static const unsigned int ftoa_format_f
Use f or F conversion format instead of g or G.
Definition: ofstd.h:519
static OFBool dirExists(const OFString &dirName)
check whether the given directory exists.
XHTML (Extensible Hyper Text Markup Language)
Definition: ofstd.h:75
static OFString & getFilenameFromPath(OFString &result, const OFString &pathName, const OFBool assumeFilename=OFTrue)
get file name component from given path name.
static size_t strlcpy(char *dst, const char *src, size_t siz)
This function copies up to size - 1 characters from the NUL- terminated string src to dst...
Definition: ofstd.h:97
static OFBool __attribute__((visibility("default"))) safeSubtract(T minuend
check whether subtraction is safe (i.e.
static size_t strlcat(char *dst, const char *src, size_t siz)
This function appends the NUL-terminated string src to the end of dst.
Definition: ofstd.h:123
static OFBool isWriteable(const OFString &pathName)
check whether the given path is writeable.
E_MarkupMode
Markup language mode.
Definition: ofstd.h:68
static const unsigned int ftoa_alternate
convert value to alternate form.
Definition: ofstd.h:528
static OFCondition removeRootDirFromPathname(OFString &result, const OFString &rootDir, const OFString &pathName, const OFBool allowLeadingPathSeparator=OFTrue)
remove root directory prefix from given path name.
static const unsigned int ftoa_leftadj
left-justify number be within the field
Definition: ofstd.h:531
static size_t searchDirectoryRecursively(const OFString &directory, OFList< OFString > &fileList, const OFString &pattern, const OFString &dirPrefix, const OFBool recurse=OFTrue)
scan a given directory (recursively) and add all filenames found to a list
static size_t decodeBase64(const OFString &data, unsigned char *&result)
decode "Base64" encoded string.
a simple string class that implements a subset of std::string.
Definition: ofstring.h:86
static OFBool fileExists(const OFString &fileName)
check whether the given file exists.
static size_t getFileSize(const OFString &filename)
determine size of given file (in bytes)
A class for various helper functions.
Definition: ofstd.h:59
static const unsigned int ftoa_format_e
Use e or E conversion format instead of g or G.
Definition: ofstd.h:516
static OFBool check32BitAddOverflow(const Uint32 summand1, const Uint32 summand2)
check whether the addition of two 32-bit integers yields in an overflow
Definition: ofstd.h:564
static size_t my_strlcat(char *dst, const char *src, size_t siz)
private implementation of strlcat.
static OFBool deleteFile(const OFString &filename)
delete given file from filesystem
static OFCondition convertToMarkupStream(STD_NAMESPACE ostream &out, const OFString &sourceString, const OFBool convertNonASCII=OFFalse, const E_MarkupMode markupMode=MM_XML, const OFBool newlineAllowed=OFFalse)
convert character string to HTML/XHTML/XML mnenonic stream.
static double atof(const char *s, OFBool *success=NULL)
converts a floating-point number from an ASCII decimal representation to internal double-precision fo...
static OFString & combineDirAndFilename(OFString &result, const OFString &dirName, const OFString &fileName, const OFBool allowEmptyDirName=OFFalse)
combine the given directory and file name.
static size_t my_strlcpy(char *dst, const char *src, size_t siz)
private implementation of strlcpy.
General purpose class for condition codes.
Definition: ofcond.h:305


Generated on Wed Jan 4 2017 for OFFIS DCMTK Version 3.6.0 by Doxygen 1.8.6