DCL 3.7.4
Loading...
Searching...
No Matches
Files Class Reference

#include <Files.h>

Public Types

enum  AccessType { READ_OK = 4 , WRITE_OK = 2 , EXECUTE_OK = 1 , EXISTS = 0 }

Static Public Member Functions

static bool access (const String &_path, int _mode)
static void rename (const String &_oldpath, const String &_newpath) __DCL_THROWS1(IOException *)
static void remove (const String &_path) __DCL_THROWS1(IOException *)
static void unlink (const String &_path) __DCL_THROWS1(IOException *)
static bool exists (const String &_path)
static bool isDirectory (const String &_path)
static DateTime mtime (const String &_path) __DCL_THROWS1(IOException *)
static bool time (const String &_path, time_t *_atime, time_t *_mtime, time_t *_ctime)
static uint64_t size (const String &_path) __DCL_THROWS1(IOException *)
static void chdir (const String &_path) __DCL_THROWS1(IOException *)
static String getcwd () __DCL_THROWS1(IOException *)
static void mkdir (const String &_path, int _mode=0755) __DCL_THROWS1(IOException *)
static void rmdir (const String &_path) __DCL_THROWS1(IOException *)
static String basename (const String &_path)
static String dirname (const String &_path)
static wchar_t delimiter (const String &_path)
static String filename (const String &_dirname, const String &_basename)
static String temppath ()
static String realpath (const String &_path) __DCL_THROWS1(IOException *)
static String unixpath (const String &_path)
static bool isAbsPath (const String &_path)
static ByteString readBytes (InputStream &_input, size_t _n=(size_t) -1) __DCL_THROWS1(IOException *)
static ByteString readBytes (const String &_filename, size_t _n=(size_t) -1) __DCL_THROWS1(IOException *)
static String readText (const String &_filename) __DCL_THROWS1(IOException *)
static String readText (const String &_filename, CharsetDecoder &_decoder) __DCL_THROWS1(IOException *)
static size_t copy (InputStream &_input, OutputStream &_output) __DCL_THROWS1(IOException *)

Detailed Description

Definition at line 25 of file Files.h.

Member Enumeration Documentation

◆ AccessType

Enumerator
READ_OK 
WRITE_OK 
EXECUTE_OK 
EXISTS 

Definition at line 34 of file Files.h.

35 {
36 READ_OK = 4,
37 WRITE_OK = 2,
38 EXECUTE_OK = 1, // windows 무시
39 EXISTS = 0,
40 };
@ EXISTS
Definition Files.h:39
@ READ_OK
Definition Files.h:36
@ WRITE_OK
Definition Files.h:37
@ EXECUTE_OK
Definition Files.h:38

Member Function Documentation

◆ access()

__DCL_BEGIN_NAMESPACE bool Files::access ( const String & _path,
int _mode )
static

프로세스의 접근권한을 검사 path가 있고 성공하면 true, 그 외에는 모두 false

Returns
== false 이면 errno 검사

Definition at line 43 of file Files.cpp.

44{
45 __DCL_ASSERT(_path.length() > 0);
46#ifdef __WINNT__
47 int types = 00;
48 if (_mode & READ_OK)
49 types |= 04;
50 if (_mode & WRITE_OK)
51 types |= 02;
52
53 return _waccess(_path, types) == 0;
54#else
55 int types = F_OK;
56 if (_mode & READ_OK)
57 types |= R_OK;
58 if (_mode & WRITE_OK)
59 types |= W_OK;
60 if (_mode & EXECUTE_OK)
61 types |= X_OK;
62
63 return __access(_path, types) == 0;
64#endif
65}
DCLCAPI int __access(const String &_path, int _type)
Definition _unistd.cpp:25
#define __DCL_ASSERT(expr)
Definition Object.h:394

◆ basename()

String Files::basename ( const String & _path)
static

Definition at line 252 of file Files.cpp.

253{
254 __DCL_ASSERT(_path.length() > 0);
255
256 const wchar_t* end = _path.data() + _path.length();
257 do {
258 end--;
259 if (__is_dir_delimiter(*end)) {
260 return _path.right(_path.length() - (end + 1 - _path));
261 }
262 } while (_path.data() <= end);
263
264 // not found
265 return String();
266}
bool __is_dir_delimiter(wchar_t _c)
Definition Files.cpp:247

◆ chdir()

void Files::chdir ( const String & _path)
static

Definition at line 182 of file Files.cpp.

184{
185 __DCL_ASSERT(!_path.isEmpty());
186#ifdef __WINNT__
187#define __chdir _wchdir
188#endif
189 if (__chdir(_path)) {
190 throw new IOException(_path, errno);
191 }
192}
DCLCAPI int __chdir(const String &_path)
Definition _unistd.cpp:43

◆ copy()

size_t Files::copy ( InputStream & _input,
OutputStream & _output )
static

Definition at line 518 of file Files.cpp.

520{
521 char buf[4096];
522 size_t total = 0;
523 size_t read;
524 while ((read = _input.read(buf, sizeof(buf))) > 0) {
525 _output.write(buf, read);
526 total += read;
527 }
528 return total;
529}

◆ delimiter()

wchar_t Files::delimiter ( const String & _path)
static

Definition at line 284 of file Files.cpp.

285{
286 const wchar_t* psz = _path;
287 wchar_t ch = L'/';
288 while(*psz) {
289 if (__is_dir_delimiter(*psz)) {
290 ch = *psz;
291 break;
292 }
293 psz++;
294 }
295
296 return ch;
297}

◆ dirname()

String Files::dirname ( const String & _path)
static

Definition at line 268 of file Files.cpp.

269{
270 __DCL_ASSERT(_path.length() > 0);
271
272 const wchar_t* end = _path.data() + _path.length();
273 do {
274 end--;
275 if (__is_dir_delimiter(*end)) {
276 return _path.left(end - _path + 1);
277 }
278 } while (_path.data() <= end);
279
280 // not found
281 return L"./";
282}

◆ exists()

bool Files::exists ( const String & _path)
static

Definition at line 109 of file Files.cpp.

110{
111 __DCL_ASSERT(_path.length() > 0);
112 return access(_path, EXISTS);
113}
static bool access(const String &_path, int _mode)
Definition Files.cpp:43

◆ filename()

String Files::filename ( const String & _dirname,
const String & _basename )
static

Definition at line 299 of file Files.cpp.

303{
304 __DCL_ASSERT(_basename.length() > 0);
305
306 String str = _dirname;
307 if (!str.isEmpty()) {
308 if (!__is_dir_delimiter(str[str.length() - 1]))
309 str = str + Files::delimiter(str);
310 }
311 str = str + _basename;
312
313 return str;
314}
static wchar_t delimiter(const String &_path)
Definition Files.cpp:284

◆ getcwd()

String Files::getcwd ( )
static

Definition at line 194 of file Files.cpp.

195{
196#ifdef __WINNT__
197 CharBuffer* buf = CharBuffer::create(PATH_MAX);
198 wchar_t* s = _wgetcwd(buf->data(), PATH_MAX);
199 if (s == NULL) {
200 buf->release();
201 throw new IOException(L"_wgetcwd", errno);
202 }
203 buf->__dataLength = String::length(buf->data());
204 String r = buf;
205 buf->release();
206
207 r = r.replace('\\', '/');
208#else
209 String r;
210 if (__getcwd(r)) {
211 throw new IOException(L"getcwd", errno);
212 }
213#endif
214 return r;
215}
DCLCAPI int __getcwd(String &_r)
Definition _unistd.cpp:49
#define NULL
Definition Config.h:312
IOException *size_t r
Definition MediaInfo.cpp:82

◆ isAbsPath()

bool Files::isAbsPath ( const String & _path)
static

Definition at line 380 of file Files.cpp.

381{
382 const wchar_t* pszPath = _path;
383 if (*pszPath == L'/' || *pszPath == L'\\')
384 return true;
385
386#ifdef __WINNT__
387 if (iswalpha(*pszPath)
388 && *(pszPath + 1) == L':'
389 && (*(pszPath + 2) == L'\\' || (*(pszPath + 2) == L'/'))
390 )
391 return true;
392#endif
393
394 return false;
395}

◆ isDirectory()

bool Files::isDirectory ( const String & _path)
static

Definition at line 115 of file Files.cpp.

116{
117 __DCL_ASSERT(!_path.isEmpty());
118#ifdef __WINNT__
119#define __stat _wstat
120#define stat _stat
121#endif
122 struct stat sb;
123 if (__stat(_path, &sb) == -1) {
124 throw new IOException(_path, errno);
125 }
126 return (sb.st_mode & S_IFMT) == S_IFDIR;
127}
DCLCAPI int __stat(const String &_path, struct stat *_buf)
Definition _stat.cpp:24

◆ mkdir()

void Files::mkdir ( const String & _path,
int _mode = 0755 )
static

Definition at line 218 of file Files.cpp.

220{
221 __DCL_ASSERT(!_path.isEmpty());
222
223 if (
224#ifdef __WINNT__
225 _wmkdir(_path)
226#else
227 __mkdir(_path, _mode)
228#endif
229 ) {
230 throw new IOException(_path, errno);
231 }
232}
DCLCAPI int __mkdir(const String &_path, mode_t _mode)
Definition _stat.cpp:48

◆ mtime()

DateTime Files::mtime ( const String & _path)
static

파일 최종 수정된 시간을 얻는다.

Definition at line 129 of file Files.cpp.

131{
132 time_t mtime;
133 if (!Files::time(_path,
134 (time_t*)NULL, &mtime, (time_t*)NULL)) {
135 throw (new IOException(_path, errno));
136 }
137 return DateTime(mtime);
138}
static bool time(const String &_path, time_t *_atime, time_t *_mtime, time_t *_ctime)
Definition Files.cpp:140
static DateTime mtime(const String &_path) __DCL_THROWS1(IOException *)
Definition Files.cpp:129

◆ readBytes() [1/2]

ByteString Files::readBytes ( const String & _filename,
size_t _n = (size_t)-1 )
static

Definition at line 416 of file Files.cpp.

418{
419 __DCL_ASSERT(_n == (size_t)-1 || _n < INT64_MAX);
420 File file(_filename);
421 size_t totalBytes = _n == (size_t) -1 ? file.size() : _n;
422 ByteBuffer* buf = ByteBuffer::create(totalBytes);
423
424 size_t read = file.read(buf->data(), totalBytes);
425 buf->__dataLength = read;
426 *(buf->data() + read) = '\0';
427
428 ByteString r = buf;
429 buf->release();
430
431 __DCL_ASSERT(totalBytes == read);
432 return r;
433}
#define INT64_MAX
Definition Config.h:291

◆ readBytes() [2/2]

ByteString Files::readBytes ( InputStream & _input,
size_t _n = (size_t)-1 )
static

Definition at line 397 of file Files.cpp.

399{
400 BytesOutputStream out;
401 char buf[4096];
402 size_t total = 0;
403 for (; ; ) {
404 size_t n = __MIN(_n - total, sizeof(buf));
405 if (n && (n = _input.read(buf, n))) {
406 out.write(buf, n);
407 total += n;
408 }
409 else {
410 break;
411 }
412 }
413 return out.toByteString();
414}
size_t __MIN(size_t x, size_t y)
Definition size_t.h:27

◆ readText() [1/2]

String Files::readText ( const String & _filename)
static

Definition at line 435 of file Files.cpp.

437{
438 FileReader reader(_filename);
439 StringWriter writer;
440
441 wchar_t buf[1024];
442 size_t readcCount = 0;
443 while ((readcCount = reader.read(buf, __countof(buf, wchar_t))) > 0) {
444 writer.write(buf, readcCount);
445 }
446
447 return writer.toString();
448}
#define __countof(array, type)
Definition Config.h:336
virtual String toString() const
Definition Object.cpp:187

◆ readText() [2/2]

String Files::readText ( const String & _filename,
CharsetDecoder & _decoder )
static

Definition at line 450 of file Files.cpp.

452{
453#if 0
454 FileInputStream input(_filename);
455 InputStreamReader reader(input, _decoder);
456 StringWriter writer;
457
458 wchar_t buf[1024];
459 size_t n = 0;
460 while (n = reader.read(buf, __countof(buf, wchar_t))) {
461 writer.write(buf, n);
462 }
463
464 return writer.toString();
465#else
466 File file(_filename);
467 File::off_t totalBytes = file.size();
468
469 // 디코드된 char_t의 최대 길이는 입력 파일이 UTF-8인 경우의 바이트 수 이다.
470 CharBuffer* outBuf = CharBuffer::create(totalBytes);
471 wchar_t* outCur = outBuf->data();
472 wchar_t* outEnd = outCur + outBuf->__allocLength;
473
474#define BUFFER_SIZE 4096
475#define EXTRA_SIZE 20
477 size_t extraCount = 0;
478 size_t readCount;
479 while ((readCount = file.read(inBuf + extraCount, BUFFER_SIZE)) > 0) {
480 size_t inCount = readCount + extraCount;
481 size_t inCountSave = inCount;
482 size_t outCount = outEnd - outCur;
483 int r = _decoder.decode(inBuf, inCount, outCur, outCount);
484 extraCount = inCountSave - inCount;
485 /*__DCL_TRACE4(__T("%ls read[%d], extra[%d], out[%d]\n"),
486 _decoder.className(), readCount, extraCount, outCount);*/
487 switch (r) {
488 case CS_SOURCE_FEW:
489 __DCL_ASSERT(extraCount <= EXTRA_SIZE);
490 if (inCount > 0) {
491 memmove(inBuf, inBuf + inCount, extraCount);
492 }
493 case CS_NOERROR:
494 break;
495 default:
496 outBuf->release();
497 throw(new IOException(_filename, new CharsetConvertException(r)));
498 }
499 outCur += outCount;
500 }
501 __DCL_ASSERT(outCur <= outEnd);
502#ifdef __DCL_DEBUG
503 if (extraCount) {
504 __DCL_TRACE1(__T("Warning! Illegal sequence at the end of the text. [%d]\n"),
505 extraCount);
506 }
507#endif
508
509 *outCur = __T('\0');
510 outBuf->__dataLength = outCur - outBuf->data();
511 String r = outBuf;
512 outBuf->release();
513
514 return r;
515#endif
516}
@ CS_SOURCE_FEW
Definition Charset.h:70
@ CS_NOERROR
Definition Charset.h:66
unsigned char byte_t
Definition Config.h:246
#define EXTRA_SIZE
#define BUFFER_SIZE
#define __DCL_TRACE1(fmt, arg1)
Definition Object.h:399
#define __T(str)
Definition Object.h:60
virtual int decode(const byte_t *_in, size_t &_inCount, wchar_t *_out, size_t &_outCount)

◆ realpath()

String Files::realpath ( const String & _path)
static

Definition at line 349 of file Files.cpp.

351{
352 __DCL_ASSERT(!_path.isEmpty());
353
354#ifdef __WINNT__
355 CharBuffer* buf = CharBuffer::create(PATH_MAX);
356 wchar_t* pFilePart = NULL;
357 DWORD dwLen = ::GetFullPathNameW(_path, PATH_MAX, buf->data(), &pFilePart);
358 if (dwLen == 0) {
359 buf->release();
360 throw new IOException(_path, WINAPI_ERROR(::GetLastError()));
361 }
362 buf->__dataLength = dwLen;
363 String resolved = buf;
364 buf->release();
365#else
366 String resolved;
367 int error = __realpath(_path, resolved);
368 if (error) {
369 throw new IOException(_path, errno);
370 }
371#endif
372 return resolved;
373}
DCLCAPI int __realpath(const String &_path, String &_resolved)
Definition _stdlib.cpp:83
#define WINAPI_ERROR(n)
Definition Exception.h:72

◆ remove()

void Files::remove ( const String & _path)
static

Definition at line 85 of file Files.cpp.

87{
88 __DCL_ASSERT(_path.length() > 0);
89#ifdef __WINNT__
90#define __remove _wremove
91#endif
92 if (__remove(_path)) {
93 throw new IOException(_path, errno);
94 }
95}
DCLCAPI int __remove(const String &_path)
Definition _stdio.cpp:32

◆ rename()

void Files::rename ( const String & _oldpath,
const String & _newpath )
static

Definition at line 67 of file Files.cpp.

69{
70 __DCL_ASSERT(_oldpath.length() > 0);
71 __DCL_ASSERT(_newpath.length() > 0);
72#ifdef __WINNT__
73#define __rename _wrename
74#endif
75 if (__rename(_oldpath, _newpath)) {
76 StringBuilder sb = _oldpath;
77 sb += __T("==>");
78 sb += _newpath;
79
80 throw new IOException(sb.toString(), errno);
81 }
82}
DCLCAPI int __rename(const char *_oldpath, const char *_newpath)
Definition _stdio.cpp:38

◆ rmdir()

void Files::rmdir ( const String & _path)
static

Definition at line 235 of file Files.cpp.

237{
238 __DCL_ASSERT(!_path.isEmpty());
239#ifdef __WINNT__
240#define __rmdir _wrmdir
241#endif
242 if (__rmdir(_path)) {
243 throw IOException(_path, errno);
244 }
245}
DCLCAPI int __rmdir(const String &_path)
Definition _unistd.cpp:101

◆ size()

uint64_t Files::size ( const String & _path)
static

Definition at line 160 of file Files.cpp.

162{
163 __DCL_ASSERT(!_path.isEmpty());
164#ifdef __WINNT__
165 WIN32_FIND_DATAW findData;
166 HANDLE h = FindFirstFileW(_path, &findData);
167 if (h == INVALID_HANDLE_VALUE)
168 throw new IOException(_path, GetLastError());
169 FindClose(h);
170 LARGE_INTEGER size;
171 size.HighPart = findData.nFileSizeHigh;
172 size.LowPart = findData.nFileSizeLow;
173 return size.QuadPart;
174#else
175 struct stat sb;
176 if (__stat(_path, &sb) == -1)
177 throw new IOException(_path, errno);
178 return sb.st_size;
179#endif
180}
#define INVALID_HANDLE_VALUE
Definition Dir.cpp:34
static uint64_t size(const String &_path) __DCL_THROWS1(IOException *)
Definition Files.cpp:160

◆ temppath()

String Files::temppath ( )
static

Definition at line 316 of file Files.cpp.

317{
318 String r;
319#ifdef __WINNT__
320 wchar_t* env = NULL;
321 if ((env = _wgetenv(L"TMP"))
322 || (env = _wgetenv(L"TEMP"))) {
323 r = env;
324 }
325 else {
326 CharBuffer* buf = CharBuffer::create(PATH_MAX);
327 DWORD n = GetTempPathW(PATH_MAX, buf->data());
328 __DCL_ASSERT(n <= PATH_MAX);
329 buf->__dataLength = n;
330 r = buf;
331 buf->release();
332 }
333 r = r.replace('\\', '/');
334#else
335 if (__getenv(String(__T("TMP")), r)
336 && __getenv(String(__T("TEMP")), r)) {
337 r = L"/tmp";
338 }
339#endif
340 if (!Files::exists(r)) {
341 r = L".";
342 }
343 if (!r.endsWith(L"/")) {
344 r = r + '/';
345 }
346 return r;
347}
DCLCAPI int __getenv(const String &_name, String &_value)
Definition _stdlib.cpp:57
static bool exists(const String &_path)
Definition Files.cpp:109

◆ time()

bool Files::time ( const String & _path,
time_t * _atime,
time_t * _mtime,
time_t * _ctime )
static

Definition at line 140 of file Files.cpp.

142{
143 __DCL_ASSERT(!_path.isEmpty());
144 struct stat sb;
145 if (__stat(_path, &sb) == -1) {
146 return false;
147 }
148 if (_atime) {
149 *_atime = sb.st_atime;
150 }
151 if (_mtime) {
152 *_mtime = sb.st_mtime;
153 }
154 if (_ctime) {
155 *_ctime = sb.st_ctime;
156 }
157 return true;
158}

◆ unixpath()

String Files::unixpath ( const String & _path)
static

Definition at line 375 of file Files.cpp.

376{
377 return _path.replace(L'\\', L'/');
378}

◆ unlink()

void Files::unlink ( const String & _path)
static

Definition at line 97 of file Files.cpp.

99{
100 __DCL_ASSERT(_path.length() > 0);
101#ifdef __WINNT__
102#define __unlink _wunlink
103#endif
104 if (__unlink(_path)) {
105 throw new IOException(_path, errno);
106 }
107}
DCLCAPI int __unlink(const String &_path)
Definition _unistd.cpp:95

The documentation for this class was generated from the following files: