[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"
|
---|
| 7 |
|
---|
| 8 | #ifdef _WIN32
|
---|
[14] | 9 | # include <stdio.h>
|
---|
[4] | 10 | # include <io.h>
|
---|
[14] | 11 | # include <sys/stat.h>
|
---|
| 12 |
|
---|
| 13 | # define S_IWUSR _S_IWRITE
|
---|
| 14 | # define S_IRUSR _S_IREAD
|
---|
| 15 | # define S_IXUSR _S_IEXEC
|
---|
| 16 | # define S_IRGRP 0x00000000
|
---|
| 17 | # define S_IWGRP 0x00000000
|
---|
| 18 | # define S_IXGRP 0x00000000
|
---|
| 19 | # define S_IROTH 0x00000000
|
---|
| 20 | # define S_IWOTH 0x00000000
|
---|
| 21 | # define S_IXOTH 0x00000000
|
---|
[4] | 22 | #else // #ifdef _WIN32
|
---|
| 23 | # include <unistd.h>
|
---|
[26] | 24 | # define O_BINARY 0x00000000
|
---|
[4] | 25 | #endif // #ifdef _WIN32
|
---|
| 26 |
|
---|
[15] | 27 | #define FILE_MODE S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH
|
---|
| 28 |
|
---|
[35] | 29 | /**
|
---|
| 30 | * 파일 열기
|
---|
| 31 | *
|
---|
| 32 | * @return 성공 시, 파일 디스크립터; 실패 시, 오류 코드
|
---|
| 33 | *
|
---|
| 34 | * @param path 파일 경로
|
---|
| 35 | * @param flag 파일 열기 플래그
|
---|
| 36 | *
|
---|
| 37 | * @see CF_FILE_FLAG
|
---|
| 38 | */
|
---|
[4] | 39 | int
|
---|
[16] | 40 | CF_File_Open (const char * path,
|
---|
| 41 | const CF_FILE_FLAG flag)
|
---|
[4] | 42 | {
|
---|
[29] | 43 | int result = open (path, (int)(flag|O_BINARY));
|
---|
[4] | 44 |
|
---|
| 45 | if (result < 0)
|
---|
| 46 | return CF_ERROR_FILE_OPEN;
|
---|
| 47 |
|
---|
| 48 | return result;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[35] | 51 | /**
|
---|
| 52 | * 파일 생성
|
---|
| 53 | *
|
---|
| 54 | * @return 성공 시, 파일 디스크립터; 실패 시, 오류 코드
|
---|
| 55 | *
|
---|
| 56 | * @param path 파일 경로
|
---|
| 57 | */
|
---|
[4] | 58 | int
|
---|
| 59 | CF_File_Create (const char * path)
|
---|
| 60 | {
|
---|
[16] | 61 | int result = open (path, CF_FILE_CR|CF_FILE_WO|CF_FILE_TR, FILE_MODE);
|
---|
[4] | 62 |
|
---|
| 63 | if (result < 0)
|
---|
| 64 | return CF_ERROR_FILE_CREATE;
|
---|
| 65 |
|
---|
| 66 | return result;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[35] | 69 | /**
|
---|
| 70 | * 파일 닫기
|
---|
| 71 | *
|
---|
| 72 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
| 73 | *
|
---|
| 74 | * @param fd 파일 디스크립터
|
---|
| 75 | */
|
---|
[4] | 76 | int
|
---|
| 77 | CF_File_Close (const int fd)
|
---|
| 78 | {
|
---|
| 79 | int result = 0;
|
---|
| 80 |
|
---|
| 81 | if (fd < 0)
|
---|
| 82 | return CF_ERROR_FILE_INVALID_ARGS;
|
---|
| 83 |
|
---|
| 84 | result = close (fd);
|
---|
| 85 |
|
---|
| 86 | if (result < 0)
|
---|
| 87 | return CF_ERROR_FILE_CLOSE;
|
---|
| 88 |
|
---|
| 89 | return CF_OK;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[35] | 92 | /**
|
---|
| 93 | * 파일 읽기
|
---|
| 94 | *
|
---|
| 95 | * @return 성공 시, 읽은 바이트 수; 실패 시, 오류 코드
|
---|
| 96 | *
|
---|
| 97 | * @param fd 파일 디스크립터
|
---|
| 98 | * @param buf 데이터를 저장할 메모리
|
---|
| 99 | * @param len 데이터를 저장할 메모리의 크기
|
---|
| 100 | */
|
---|
[4] | 101 | int
|
---|
| 102 | CF_File_Read (const int fd,
|
---|
| 103 | void * buf,
|
---|
| 104 | const size_t len)
|
---|
| 105 | {
|
---|
| 106 | int result = (int) read (fd, buf, len);
|
---|
| 107 |
|
---|
| 108 | if (result < 0)
|
---|
| 109 | return CF_ERROR_FILE_READ;
|
---|
| 110 |
|
---|
[26] | 111 | return result;
|
---|
[4] | 112 | }
|
---|
| 113 |
|
---|
[35] | 114 | /**
|
---|
| 115 | * 파일 쓰기
|
---|
| 116 | *
|
---|
| 117 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
| 118 | *
|
---|
| 119 | * @param fd 파일 디스크립터
|
---|
| 120 | * @param buf 데이터가 저장된 메모리
|
---|
| 121 | * @param len 쓸 데이터의 길이
|
---|
| 122 | */
|
---|
[4] | 123 | int
|
---|
| 124 | CF_File_Write (const int fd,
|
---|
| 125 | const void * buf,
|
---|
| 126 | const size_t len)
|
---|
| 127 | {
|
---|
| 128 | int result = (int) write (fd, buf, len);
|
---|
| 129 |
|
---|
| 130 | if (result != len)
|
---|
| 131 | return CF_ERROR_FILE_WRITE;
|
---|
| 132 |
|
---|
| 133 | return CF_OK;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[35] | 136 | /**
|
---|
| 137 | * 파일 크기 얻기
|
---|
| 138 | *
|
---|
| 139 | * @return 성공 시, 파일 크기; 실패 시, 오류 코드
|
---|
| 140 | *
|
---|
| 141 | * @param fd 파일 디스크립터
|
---|
| 142 | */
|
---|
[4] | 143 | int
|
---|
| 144 | CF_File_GetSize (const int fd)
|
---|
| 145 | {
|
---|
| 146 | int result = (int) lseek (fd, 0, SEEK_END);
|
---|
| 147 |
|
---|
| 148 | if (result < 0 || lseek (fd, 0, SEEK_SET) < 0)
|
---|
| 149 | return CF_ERROR_FILE_GET_SIZE;
|
---|
| 150 |
|
---|
| 151 | return result;
|
---|
| 152 | }
|
---|