27 #ifndef _UCOMMON_FILE_H_
28 #define _UCOMMON_FILE_H_
30 #ifndef _UCOMMON_CONFIG_H_
34 #ifndef _UCOMMON_PROTOCOLS_H_
38 #ifndef _UCOMMON_THREAD_H_
42 #ifndef _UCOMMON_STRING_H_
62 #define __S_ISTYPE(mode, mask) (((mode) & S_IFMT) == (mask))
65 #if !defined(S_ISDIR) && defined(S_IFDIR)
66 #define S_ISDIR(mode) __S_ISTYPE((mode), S_IFDIR)
69 #if !defined(S_ISCHR) && defined(S_IFCHR)
70 #define S_ISCHR(mode) __S_ISTYPE((mode), S_IFCHR)
71 #elif !defined(S_ISCHR)
72 #define S_ISCHR(mode) 0
75 #if !defined(S_ISBLK) && defined(S_IFBLK)
76 #define S_ISBLK(mode) __S_ISTYPE((mode), S_IFBLK)
77 #elif !defined(S_ISBLK)
78 #define S_ISBLK(mode) 0
81 #if !defined(S_ISREG) && defined(S_IFREG)
82 #define S_ISREG(mode) __S_ISTYPE((mode), S_IFREG)
83 #elif !defined(S_ISREG)
84 #define S_ISREG(mode) 1
87 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
88 #define S_ISSOCK(mode) __S_ISTYPE((mode), S_IFSOCK)
89 #elif !defined(S_ISSOCK)
90 #define S_ISSOCK(mode) (0)
93 #if !defined(S_ISFIFO) && defined(S_IFIFO)
94 #define S_ISFIFO(mode) __S_ISTYPE((mode), S_IFIFO)
95 #elif !defined(S_ISFIFO)
96 #define S_ISFIFO(mode) (0)
99 #if !defined(S_ISLNK) && defined(S_IFLNK)
100 #define S_ISLNK(mode) __S_ISTYPE((mode), S_IFLNK)
101 #elif !defined(S_ISLNK)
102 #define S_ISLNK(mode) (0)
130 WIN32_FIND_DATA *ptr;
138 typedef struct stat fileinfo_t;
141 static int remapError(
void);
143 inline static int remapError(
void)
154 ACCESS_RDWR = ACCESS_REWRITE,
212 inline fd_t operator*()
const
219 inline operator fd_t()
const
226 inline operator bool()
const
227 {
return fd != INVALID_HANDLE_VALUE || ptr != NULL;};
233 inline bool operator!()
const
234 {
return fd == INVALID_HANDLE_VALUE && ptr == NULL;};
240 void operator=(
const fsys& descriptor);
246 void operator=(fd_t descriptor);
252 inline fd_t getHandle(
void)
const
259 void set(fd_t descriptor);
272 int seek(offset_t offset);
279 int drop(offset_t size = 0);
291 static bool istty(fd_t fd);
299 ssize_t read(
void *buffer,
size_t count);
307 ssize_t write(
const void *buffer,
size_t count);
314 int fileinfo(fileinfo_t *buffer);
322 int trunc(offset_t offset);
335 static int changeDir(
const char *path);
343 static int getPrefix(
char *path,
size_t size);
351 static int fileinfo(
const char *path, fileinfo_t *buffer);
358 static int remove(
const char *path);
367 static int copy(
const char *source,
const char *target,
size_t size = 1024);
375 static int rename(
const char *oldpath,
const char *newpath);
383 static int change(
const char *path,
unsigned mode);
391 static int access(
const char *path,
unsigned mode);
398 static bool isfile(
const char *path);
405 static bool isdir(
const char *path);
412 static bool islink(
const char *path);
419 static bool ishidden(
const char *path);
428 inline static ssize_t read(
fsys& descriptor,
void *buffer,
size_t count)
429 {
return descriptor.
read(buffer, count);};
438 inline static ssize_t write(
fsys& descriptor,
const void *buffer,
size_t count)
439 {
return descriptor.
write(buffer, count);};
448 {
return descriptor.
seek(offset);};
457 {
return descriptor.
drop(size);};
464 void open(
const char *path, access_t
access);
470 inline void assign(fd_t descriptor)
471 {close(); fd = descriptor;};
478 inline static void assign(
fsys&
object, fd_t descriptor)
479 {
object.close();
object.fd = descriptor;};
487 void create(
const char *path, access_t
access,
unsigned mode);
495 static int createDir(
const char *path,
unsigned mode);
502 static int removeDir(
const char *path);
511 static int unlink(
const char *path);
519 static int link(
const char *path,
const char *target);
527 static int hardlink(
const char *path,
const char *target);
535 static int linkinfo(
const char *path,
char *buffer,
size_t size);
541 inline static void close(
fsys& descriptor)
542 {descriptor.
close();};
553 inline int err(
void)
const
563 {
object.
open(path, access);};
570 static fd_t input(
const char *path);
577 static fd_t output(
const char *path);
584 static fd_t append(
const char *path);
590 static void release(fd_t descriptor);
600 {
object.create(path, access, mode);};
607 static int load(
const char *path);
614 static void load(
fsys& module,
const char *path);
620 static void unload(
fsys& module);
628 static void *find(
fsys& module,
const char *symbol);
630 static inline bool isfile(
struct stat *inode)
631 {
return S_ISREG(inode->st_mode);}
633 static inline bool isdir(
struct stat *inode)
634 {
return S_ISDIR(inode->st_mode);}
636 static inline bool islink(
struct stat *inode)
637 {
return S_ISLNK(inode->st_mode);}
639 static inline bool isdev(
struct stat *inode)
640 {
return S_ISBLK(inode->st_mode) || S_ISCHR(inode->st_mode);}
642 static inline bool ischar(
struct stat *inode)
643 {
return S_ISCHR(inode->st_mode);}
645 static inline bool isdisk(
struct stat *inode)
646 {
return S_ISBLK(inode->st_mode);}
648 static inline bool issys(
struct stat *inode)
649 {
return S_ISSOCK(inode->st_mode) || S_ISFIFO(inode->st_mode);}
668 typedef ::fpos_t bookmark_t;
675 {fp = file; opened =
false;}
682 charfile(
const char *path,
const char *mode);
698 inline operator bool()
705 inline bool operator !()
713 void open(
const char *path,
const char *mode);
725 size_t put(
const char *
string);
736 size_t readline(
char *
string,
size_t size);
746 size_t readline(
string&
string);
748 inline size_t put(
const void *data,
size_t size)
749 {
return fp == NULL ? 0 : fwrite(data, 1, size, fp);}
751 size_t get(
void *data,
size_t size)
752 {
return fp == NULL ? 0 : fread(data, 1, size, fp);}
754 inline void get(bookmark_t& pos)
755 {
if(fp) fsetpos(fp, &pos);}
757 inline void set(bookmark_t& pos)
758 {
if(fp) fgetpos(fp, &pos);}
764 inline void seek(
long offset)
765 {
if(fp) fseek(fp, offset, SEEK_SET);}
767 inline void move(
long offset)
768 {
if(fp) fseek(fp, offset, SEEK_CUR);}
770 inline void append(
void)
771 {
if (fp) fseek(fp, 0l, SEEK_END);}
773 inline void rewind(
void)
774 {
if(fp) ::rewind(fp);}
776 size_t printf(
const char *format, ...) __PRINTF(2, 3);
788 extern
charfile cstdin, cstdout, cstderr;