/** * @file cf_file.c * @author myusgun * @version 0.1 */ #include "cf_file.h" #ifdef _WIN32 # include # include # include # define S_IWUSR _S_IWRITE # define S_IRUSR _S_IREAD # define S_IXUSR _S_IEXEC # define S_IRGRP 0x00000000 # define S_IWGRP 0x00000000 # define S_IXGRP 0x00000000 # define S_IROTH 0x00000000 # define S_IWOTH 0x00000000 # define S_IXOTH 0x00000000 #else // #ifdef _WIN32 # include # define O_BINARY 0x00000000 #endif // #ifdef _WIN32 #define FILE_MODE S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH /** * 파일 열기 * * @return 성공 시, 파일 디스크립터; 실패 시, 오류 코드 * * @param path 파일 경로 * @param flag 파일 열기 플래그 * * @see CF_FILE_FLAG */ int CF_File_Open (const char * path, const CF_FILE_FLAG flag) { int result = open (path, (int)(flag|O_BINARY)); if (result < 0) return CF_ERROR_FILE_OPEN; return result; } /** * 파일 생성 * * @return 성공 시, 파일 디스크립터; 실패 시, 오류 코드 * * @param path 파일 경로 */ int CF_File_Create (const char * path) { int result = open (path, CF_FILE_CR|CF_FILE_WO|CF_FILE_TR, FILE_MODE); if (result < 0) return CF_ERROR_FILE_CREATE; return result; } /** * 파일 닫기 * * @return 성공 시, CF_OK; 실패 시, 오류 코드 * * @param fd 파일 디스크립터 */ int CF_File_Close (const int fd) { int result = 0; if (fd < 0) return CF_ERROR_FILE_INVALID_ARGS; result = close (fd); if (result < 0) return CF_ERROR_FILE_CLOSE; return CF_OK; } /** * 파일 읽기 * * @return 성공 시, 읽은 바이트 수; 실패 시, 오류 코드 * * @param fd 파일 디스크립터 * @param buf 데이터를 저장할 메모리 * @param len 데이터를 저장할 메모리의 크기 */ int CF_File_Read (const int fd, void * buf, const size_t len) { int result = (int) read (fd, buf, len); if (result < 0) return CF_ERROR_FILE_READ; return result; } /** * 파일 쓰기 * * @return 성공 시, CF_OK; 실패 시, 오류 코드 * * @param fd 파일 디스크립터 * @param buf 데이터가 저장된 메모리 * @param len 쓸 데이터의 길이 */ int CF_File_Write (const int fd, const void * buf, const size_t len) { int result = (int) write (fd, buf, len); if (result != len) return CF_ERROR_FILE_WRITE; return CF_OK; } /** * 파일 크기 얻기 * * @return 성공 시, 파일 크기; 실패 시, 오류 코드 * * @param fd 파일 디스크립터 */ int CF_File_GetSize (const int fd) { int result = (int) lseek (fd, 0, SEEK_END); if (result < 0 || lseek (fd, 0, SEEK_SET) < 0) return CF_ERROR_FILE_GET_SIZE; return result; }