DCL 4.0
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#if __DCL_WINDOWS
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:22
#define __DCL_ASSERT(expr)
Definition Object.h:371

◆ basename()

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

Definition at line 253 of file Files.cpp.

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

◆ chdir()

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

Definition at line 183 of file Files.cpp.

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

◆ copy()

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

Definition at line 520 of file Files.cpp.

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

◆ delimiter()

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

Definition at line 285 of file Files.cpp.

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

◆ dirname()

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

Definition at line 269 of file Files.cpp.

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

◆ 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 300 of file Files.cpp.

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

◆ getcwd()

String Files::getcwd ( )
static

Definition at line 195 of file Files.cpp.

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

◆ isAbsPath()

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

Definition at line 381 of file Files.cpp.

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

◆ isDirectory()

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

Definition at line 116 of file Files.cpp.

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

◆ mkdir()

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

Definition at line 219 of file Files.cpp.

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

◆ mtime()

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

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

Definition at line 130 of file Files.cpp.

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

◆ readBytes() [1/2]

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

Definition at line 417 of file Files.cpp.

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

◆ readBytes() [2/2]

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

Definition at line 398 of file Files.cpp.

400{
401 BytesOutputStream out;
402 char buf[4096];
403 size_t total = 0;
404 for (; ; ) {
405 size_t n = __MIN(_n - total, sizeof(buf));
406 if (n && (n = _input.read(buf, n))) {
407 out.write(buf, n);
408 total += n;
409 }
410 else {
411 break;
412 }
413 }
414 return out.toByteString();
415}
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 437 of file Files.cpp.

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

◆ readText() [2/2]

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

Definition at line 452 of file Files.cpp.

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

352{
353 __DCL_ASSERT(!_path.isEmpty());
354
355#if __DCL_WINDOWS
356 CharBuffer* buf = CharBuffer::create(PATH_MAX);
357 wchar_t* pFilePart = NULL;
358 DWORD dwLen = ::GetFullPathNameW(_path, PATH_MAX, buf->data(), &pFilePart);
359 if (dwLen == 0) {
360 buf->release();
361 throw new IOException(_path, WINAPI_ERROR(::GetLastError()));
362 }
363 buf->__dataLength = dwLen;
364 String resolved = buf;
365 buf->release();
366#else
367 String resolved;
368 int error = __realpath(_path, resolved);
369 if (error) {
370 throw new IOException(_path, errno);
371 }
372#endif
373 return resolved;
374}
DCLCAPI int __realpath(const String &_path, String &_resolved)
Definition _stdlib.cpp:65
#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#if __DCL_WINDOWS
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:28

◆ 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#if __DCL_WINDOWS
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:34

◆ rmdir()

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

Definition at line 236 of file Files.cpp.

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

◆ size()

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

Definition at line 161 of file Files.cpp.

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

◆ temppath()

String Files::temppath ( )
static

Definition at line 317 of file Files.cpp.

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

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

◆ unixpath()

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

Definition at line 376 of file Files.cpp.

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

◆ unlink()

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

Definition at line 97 of file Files.cpp.

99{
100 __DCL_ASSERT(_path.length() > 0);
101#if __DCL_WINDOWS
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:92

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