[4] | 1 | /**
|
---|
[28] | 2 | * @file cf_file.c
|
---|
| 3 | * @author myusgun <myusgun@gmail.com>
|
---|
| 4 | * @version 0.1
|
---|
[4] | 5 | */
|
---|
| 6 | #include "cf_file.h"
|
---|
[40] | 7 | #include "cf_error.h"
|
---|
[4] | 8 |
|
---|
[41] | 9 | #include <stdio.h>
|
---|
| 10 | #include <errno.h>
|
---|
| 11 | #include <sys/stat.h>
|
---|
| 12 |
|
---|
[50] | 13 | #if defined(_WIN32) || defined(_WIN64)
|
---|
[4] | 14 | # include <io.h>
|
---|
[14] | 15 |
|
---|
[41] | 16 | # define DELIMITER '\\'
|
---|
| 17 | # define mkdir(a,b) _mkdir (a)
|
---|
| 18 | # define access(a,b) _access (a,b)
|
---|
| 19 |
|
---|
[43] | 20 | # define F_OK 0
|
---|
| 21 | # define W_OK 2
|
---|
| 22 | # define R_OK 4
|
---|
| 23 |
|
---|
[41] | 24 | /*------------------------------*/
|
---|
[14] | 25 | # define S_IWUSR _S_IWRITE
|
---|
| 26 | # define S_IRUSR _S_IREAD
|
---|
| 27 | # define S_IXUSR _S_IEXEC
|
---|
[41] | 28 | /*------------------------------*/
|
---|
[14] | 29 | # define S_IRGRP 0x00000000
|
---|
| 30 | # define S_IWGRP 0x00000000
|
---|
| 31 | # define S_IXGRP 0x00000000
|
---|
[41] | 32 | /*------------------------------*/
|
---|
[14] | 33 | # define S_IROTH 0x00000000
|
---|
| 34 | # define S_IWOTH 0x00000000
|
---|
| 35 | # define S_IXOTH 0x00000000
|
---|
[41] | 36 | /*------------------------------*/
|
---|
| 37 | # define S_IRWXU 0x00000000
|
---|
[50] | 38 | #else // #if defined(_WIN32) || defined(_WIN64)
|
---|
[4] | 39 | # include <unistd.h>
|
---|
[41] | 40 |
|
---|
| 41 | # define DELIMITER '/'
|
---|
[26] | 42 | # define O_BINARY 0x00000000
|
---|
[50] | 43 | #endif // #if defined(_WIN32) || defined(_WIN64)
|
---|
[4] | 44 |
|
---|
[41] | 45 | #define DIRECTORY_MODE S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH
|
---|
| 46 | #define FILE_MODE S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH
|
---|
[15] | 47 |
|
---|
[35] | 48 | /**
|
---|
| 49 | * 파일 열기
|
---|
| 50 | *
|
---|
| 51 | * @return 성공 시, 파일 디스크립터; 실패 시, 오류 코드
|
---|
| 52 | *
|
---|
| 53 | * @param path 파일 경로
|
---|
| 54 | * @param flag 파일 열기 플래그
|
---|
| 55 | *
|
---|
| 56 | * @see CF_FILE_FLAG
|
---|
| 57 | */
|
---|
[4] | 58 | int
|
---|
[16] | 59 | CF_File_Open (const char * path,
|
---|
| 60 | const CF_FILE_FLAG flag)
|
---|
[4] | 61 | {
|
---|
[29] | 62 | int result = open (path, (int)(flag|O_BINARY));
|
---|
[4] | 63 |
|
---|
| 64 | if (result < 0)
|
---|
| 65 | return CF_ERROR_FILE_OPEN;
|
---|
| 66 |
|
---|
| 67 | return result;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[35] | 70 | /**
|
---|
| 71 | * 파일 생성
|
---|
| 72 | *
|
---|
| 73 | * @return 성공 시, 파일 디스크립터; 실패 시, 오류 코드
|
---|
| 74 | *
|
---|
[41] | 75 | * @param path 파일 경로
|
---|
[35] | 76 | */
|
---|
[4] | 77 | int
|
---|
| 78 | CF_File_Create (const char * path)
|
---|
| 79 | {
|
---|
[16] | 80 | int result = open (path, CF_FILE_CR|CF_FILE_WO|CF_FILE_TR, FILE_MODE);
|
---|
[4] | 81 |
|
---|
| 82 | if (result < 0)
|
---|
| 83 | return CF_ERROR_FILE_CREATE;
|
---|
| 84 |
|
---|
| 85 | return result;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[35] | 88 | /**
|
---|
| 89 | * 파일 닫기
|
---|
| 90 | *
|
---|
| 91 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
| 92 | *
|
---|
| 93 | * @param fd 파일 디스크립터
|
---|
| 94 | */
|
---|
[4] | 95 | int
|
---|
| 96 | CF_File_Close (const int fd)
|
---|
| 97 | {
|
---|
| 98 | int result = 0;
|
---|
| 99 |
|
---|
| 100 | if (fd < 0)
|
---|
| 101 | return CF_ERROR_FILE_INVALID_ARGS;
|
---|
| 102 |
|
---|
| 103 | result = close (fd);
|
---|
| 104 |
|
---|
| 105 | if (result < 0)
|
---|
| 106 | return CF_ERROR_FILE_CLOSE;
|
---|
| 107 |
|
---|
| 108 | return CF_OK;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[35] | 111 | /**
|
---|
| 112 | * 파일 읽기
|
---|
| 113 | *
|
---|
| 114 | * @return 성공 시, 읽은 바이트 수; 실패 시, 오류 코드
|
---|
| 115 | *
|
---|
| 116 | * @param fd 파일 디스크립터
|
---|
| 117 | * @param buf 데이터를 저장할 메모리
|
---|
| 118 | * @param len 데이터를 저장할 메모리의 크기
|
---|
| 119 | */
|
---|
[4] | 120 | int
|
---|
| 121 | CF_File_Read (const int fd,
|
---|
| 122 | void * buf,
|
---|
| 123 | const size_t len)
|
---|
| 124 | {
|
---|
| 125 | int result = (int) read (fd, buf, len);
|
---|
| 126 |
|
---|
| 127 | if (result < 0)
|
---|
| 128 | return CF_ERROR_FILE_READ;
|
---|
| 129 |
|
---|
[26] | 130 | return result;
|
---|
[4] | 131 | }
|
---|
| 132 |
|
---|
[35] | 133 | /**
|
---|
| 134 | * 파일 쓰기
|
---|
| 135 | *
|
---|
| 136 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
| 137 | *
|
---|
| 138 | * @param fd 파일 디스크립터
|
---|
| 139 | * @param buf 데이터가 저장된 메모리
|
---|
| 140 | * @param len 쓸 데이터의 길이
|
---|
| 141 | */
|
---|
[4] | 142 | int
|
---|
| 143 | CF_File_Write (const int fd,
|
---|
| 144 | const void * buf,
|
---|
| 145 | const size_t len)
|
---|
| 146 | {
|
---|
| 147 | int result = (int) write (fd, buf, len);
|
---|
| 148 |
|
---|
| 149 | if (result != len)
|
---|
| 150 | return CF_ERROR_FILE_WRITE;
|
---|
| 151 |
|
---|
| 152 | return CF_OK;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[35] | 155 | /**
|
---|
| 156 | * 파일 크기 얻기
|
---|
| 157 | *
|
---|
| 158 | * @return 성공 시, 파일 크기; 실패 시, 오류 코드
|
---|
| 159 | *
|
---|
[41] | 160 | * @param fd 파일 디스크립터
|
---|
[35] | 161 | */
|
---|
[4] | 162 | int
|
---|
| 163 | CF_File_GetSize (const int fd)
|
---|
| 164 | {
|
---|
| 165 | int result = (int) lseek (fd, 0, SEEK_END);
|
---|
| 166 |
|
---|
| 167 | if (result < 0 || lseek (fd, 0, SEEK_SET) < 0)
|
---|
| 168 | return CF_ERROR_FILE_GET_SIZE;
|
---|
| 169 |
|
---|
| 170 | return result;
|
---|
| 171 | }
|
---|
[41] | 172 |
|
---|
| 173 | /**
|
---|
| 174 | * 디렉터리 생성
|
---|
| 175 | *
|
---|
| 176 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
| 177 | *
|
---|
| 178 | * @param path 생성할 디렉터리 경로
|
---|
| 179 | */
|
---|
| 180 | int
|
---|
| 181 | CF_File_MakeDirectory (const char * path)
|
---|
| 182 | {
|
---|
| 183 | int result = 0;
|
---|
| 184 | char fullPath[1024] = {0x00,};
|
---|
| 185 | char stepPath[256] = {0x00,};
|
---|
| 186 |
|
---|
| 187 | char * f = fullPath;
|
---|
| 188 | char * d = stepPath;
|
---|
| 189 |
|
---|
[46] | 190 | snprintf (fullPath, sizeof (fullPath) - 1, "%s%c", path, DELIMITER);
|
---|
[41] | 191 |
|
---|
| 192 | for (*d++ = *f++ ; *f ; *d++ = *f++)
|
---|
| 193 | {
|
---|
| 194 | if (*f != DELIMITER)
|
---|
| 195 | continue;
|
---|
| 196 |
|
---|
| 197 | if (access (stepPath, F_OK) == 0)
|
---|
| 198 | continue;
|
---|
| 199 |
|
---|
| 200 | result = mkdir (stepPath, DIRECTORY_MODE);
|
---|
| 201 | if (result && errno != EEXIST)
|
---|
| 202 | return CF_ERROR_FILE_MAKE_DIRECTORY;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | return CF_OK;
|
---|
| 206 | }
|
---|