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