1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
/*
*
* Copyright (C) 2011-2024, OFFIS e.V.
* All rights reserved. See COPYRIGHT file for details.
*
* This software and supporting documentation were developed by
*
* OFFIS e.V.
* R&D Division Health
* Escherweg 2
* D-26121 Oldenburg, Germany
*
*
* Module: ofstd
*
* Author: Marco Eichelberg, Joerg Riesmeier
*
* Purpose: C++ wrapper class for stdio FILE functions and
* wide character filenames
*
*/
#include "dcmtk/config/osconfig.h"
#include "dcmtk/ofstd/offile.h"
#include "dcmtk/ofstd/offilsys.h"
#include "dcmtk/ofstd/ofutil.h"
#include <cerrno>
#ifdef __SUNPRO_CC
BEGIN_EXTERN_C
#include <stdio.h>
END_EXTERN_C
#else
#include <cstdio>
#endif
#ifdef HAVE_WINDOWS_H
#include "dcmtk/ofstd/ofchrenc.h" /* for class OFCharacterEncoding */
#endif
OFFilename::OFFilename()
: filename_(NULL)
#if (defined(WIDE_CHAR_FILE_IO_FUNCTIONS) || defined(WIDE_CHAR_MAIN_FUNCTION)) && defined(_WIN32)
, wfilename_(NULL)
#endif
{
}
OFFilename::OFFilename(const char *filename,
const OFBool convert)
: filename_(NULL)
#if (defined(WIDE_CHAR_FILE_IO_FUNCTIONS) || defined(WIDE_CHAR_MAIN_FUNCTION)) && defined(_WIN32)
, wfilename_(NULL)
#endif
{
set(filename, convert);
}
OFFilename::OFFilename(const OFString &filename,
const OFBool convert)
: filename_(NULL)
#if (defined(WIDE_CHAR_FILE_IO_FUNCTIONS) || defined(WIDE_CHAR_MAIN_FUNCTION)) && defined(_WIN32)
, wfilename_(NULL)
#endif
{
set(filename, convert);
}
OFFilename::OFFilename(const OFpath &path,
const OFBool convert)
: filename_(NULL)
#if (defined(WIDE_CHAR_FILE_IO_FUNCTIONS) || defined(WIDE_CHAR_MAIN_FUNCTION)) && defined(_WIN32)
, wfilename_(NULL)
#endif
{
set(path, convert);
}
#if (defined(WIDE_CHAR_FILE_IO_FUNCTIONS) || defined(WIDE_CHAR_MAIN_FUNCTION)) && defined(_WIN32)
OFFilename::OFFilename(const wchar_t *filename,
const OFBool convert)
: filename_(NULL),
wfilename_(NULL)
{
set(filename, convert);
}
#endif
OFFilename::OFFilename(const OFFilename &arg)
: filename_(NULL)
#if (defined(WIDE_CHAR_FILE_IO_FUNCTIONS) || defined(WIDE_CHAR_MAIN_FUNCTION)) && defined(_WIN32)
, wfilename_(NULL)
#endif
{
if (arg.filename_ != NULL)
filename_ = strdup(arg.filename_);
#if (defined(WIDE_CHAR_FILE_IO_FUNCTIONS) || defined(WIDE_CHAR_MAIN_FUNCTION)) && defined(_WIN32)
if (arg.wfilename_ != NULL)
wfilename_ = _wcsdup(arg.wfilename_);
#endif
}
OFFilename::~OFFilename()
{
clear();
}
OFFilename &OFFilename::operator=(const OFFilename &arg)
{
if (&arg != this)
{
free(filename_);
filename_ = (arg.filename_ != NULL) ? strdup(arg.filename_) : NULL;
#if (defined(WIDE_CHAR_FILE_IO_FUNCTIONS) || defined(WIDE_CHAR_MAIN_FUNCTION)) && defined(_WIN32)
free(wfilename_);
wfilename_ = (arg.wfilename_ != NULL) ? _wcsdup(arg.wfilename_) : NULL;
#endif
}
return *this;
}
void OFFilename::clear()
{
free(filename_);
filename_ = NULL;
#if (defined(WIDE_CHAR_FILE_IO_FUNCTIONS) || defined(WIDE_CHAR_MAIN_FUNCTION)) && defined(_WIN32)
free(wfilename_);
wfilename_ = NULL;
#endif
}
void OFFilename::swap(OFFilename &arg)
{
OFswap(filename_, arg.filename_);
#if (defined(WIDE_CHAR_FILE_IO_FUNCTIONS) || defined(WIDE_CHAR_MAIN_FUNCTION)) && defined(_WIN32)
OFswap(wfilename_, arg.wfilename_);
#endif
}
OFBool OFFilename::isEmpty() const
{
OFBool result = (filename_ == NULL) || (filename_[0] == '\0');
#if (defined(WIDE_CHAR_FILE_IO_FUNCTIONS) || defined(WIDE_CHAR_MAIN_FUNCTION)) && defined(_WIN32)
if (result)
result = (wfilename_ == NULL) || (wfilename_[0] == L'\0');
#endif
return result;
}
void OFFilename::set(const char *filename,
const OFBool convert)
{
clear();
if (filename != NULL)
{
filename_ = strdup(filename);
#if (defined(WIDE_CHAR_FILE_IO_FUNCTIONS) || defined(WIDE_CHAR_MAIN_FUNCTION)) && defined(_WIN32)
#ifdef HAVE_WINDOWS_H
if (convert)
{
wchar_t *tmpString = NULL;
size_t strLen = 0;
/* convert UTF-8 representation to wide character encoding */
OFCharacterEncoding::convertToWideCharString(filename, strlen(filename),
tmpString, strLen, OFCharacterEncoding::CPC_UTF8);
wfilename_ = _wcsdup(tmpString);
delete[] tmpString;
}
#endif
#endif
/* avoid compiler warning on unused variable */
(void)convert;
}
}
void OFFilename::set(const OFString &filename,
const OFBool convert)
{
set(filename.c_str(), convert);
}
void OFFilename::set(const OFpath &path,
const OFBool convert)
{
set(path.native(), convert);
}
#if (defined(WIDE_CHAR_FILE_IO_FUNCTIONS) || defined(WIDE_CHAR_MAIN_FUNCTION)) && defined(_WIN32)
void OFFilename::set(const wchar_t *filename,
const OFBool convert)
{
clear();
if (filename != NULL)
{
wfilename_ = _wcsdup(filename);
#ifdef HAVE_WINDOWS_H
if (convert)
{
OFString tmpString;
/* convert wide character encoding to UTF-8 representation */
OFCharacterEncoding::convertFromWideCharString(filename, wcslen(filename),
tmpString, OFCharacterEncoding::CPC_UTF8);
filename_ = strdup(tmpString.c_str());
}
#endif
/* avoid compiler warning on unused variable */
(void)convert;
}
}
#endif
STD_NAMESPACE ostream &operator<<(STD_NAMESPACE ostream &stream,
const OFFilename &filename)
{
/* always output the 8-bit representation */
stream << OFSTRING_GUARD(filename.getCharPointer());
return stream;
}
OFBool OFFile::popen(const char *command, const char *modes)
{
if (file_) fclose();
#if defined(HAVE_POPEN) || defined(__SUNPRO_CC)
// SunPro defines popen() in a header file where CMake cannot find it, but it is there.
file_ = :: popen(command, modes);
#else
file_ = _popen(command, modes);
#endif
if (file_) popened_ = OFTrue; else storeLastError();
return (file_ != NULL);
}
int OFFile::fclose()
{
int result = 0;
if (file_)
{
if (popened_)
{
#if defined(HAVE_PCLOSE) || defined(__SUNPRO_CC)
// SunPro defines pclose() in a header file where CMake cannot find it, but it is there.
result = :: pclose(file_);
#else
result = _pclose(file_);
#endif
}
else
{
result = :: fclose(file_);
}
// After calling fclose() once, the FILE* is gone even if fclose() failed.
file_ = NULL;
}
if (result) storeLastError();
return result;
}
|