Index: src/OFExceptions.h ================================================================== --- src/OFExceptions.h +++ src/OFExceptions.h @@ -37,13 +37,10 @@ * \param obj The object which caused the exception * \return An initialized OFException */ - initWithObject: (id)obj; -/** - * Frees an OFException and the memory it allocated. - */ - free; /** * Raises an OFException and aborts execution if the exception is not caught. */ @@ -77,11 +74,11 @@ /** * Initializes an already allocated no memory exception. * * \param obj The object which caused the exception * \param size The size of the memory that couldn't be allocated - * \return An initialized new no memory exception + * \return An initialized no memory exception */ - initWithObject: (id)obj andSize: (size_t)size; /** @@ -93,77 +90,218 @@ * \return The size of the memoory that couldn't be allocated */ - (size_t)requestedSize; @end +/** + * An OFException indicating the requested selector is not implemented. + */ @interface OFNotImplementedException: OFException { SEL selector; } +/** + * Creates a new not impemented exception. + * + * \param obj The object which caused the exception + * \param sel A selector for the function not implemented + * \return A new not implemented exception + */ + newWithObject: (id)obj andSelector: (SEL)sel; + +/** + * Initializes an already allocated not implemented exception. + * + * \param obj The object which caused the exception + * \param sel A selector for the function not implemented + * \return An initialized not implemented exception + */ - initWithObject: (id)obj andSelector: (SEL)sel; + +/** + * \return An error message for the exception as a C String + */ - (char*)cString; + +/** + * \return The requested selector that is not implemented + */ - (SEL)selector; @end +/** + * An OFException indicating the given memory is not part of the object. + */ @interface OFMemNotPartOfObjException: OFException { void *pointer; } +/** + * Creates a new memory not part of object exception. + * + * \param obj The object which caused the exception + * \param ptr A pointer to the memory that is not part of the object + * \return A new memory not part of object exception + */ + newWithObject: (id)obj andPointer: (void*)ptr; + +/** + * Initializes an already allocated memory not part of object exception. + * + * \param obj The object which caused the exception + * \param ptr A pointer to the memory that is not part of the object + * \return An initialized memory not part of object exception + */ - initWithObject: (id)obj andPointer: (void*)ptr; + +/** + * \return An error message for the exception as a C String + */ - (char*)cString; + +/*** + * \return A pointer to the memory which is not part of the object + */ - (void*)pointer; @end +/** + * An OFException indicating the given value is out of range. + */ @interface OFOutOfRangeException: OFException +/** + * Creates a new out of range exception. + * + * \param obj The object which caused the exception + * \return A new out of range exception + */ + newWithObject: (id)obj; + +/** + * Initializes an already allocated out of range exception. + * + * \param obj The object which caused the exception + * \return An initialized out of range exception + */ - initWithObject: (id)obj; @end +/** + * An OFException indicating the file couldn't be opened. + */ @interface OFOpenFileFailedException: OFException { char *path; char *mode; } +/** + * Creates a new open file failed exception. + * + * \param obj The object which caused the exception + * \param p A C string of the path to the file tried to open + * \param m A C string of the mode in which the file should have been opened + * \return A new open file failed exception + */ + newWithObject: (id)obj andPath: (const char*)p andMode: (const char*)m; + +/** + * Initializes an already allocated open file failed exception. + * + * \param obj The object which caused the exception + * \param p A C string of the path to the file which couldn't be opened + * \param m A C string of the mode in which the file should have been opened + * \return An initialized open file failed exception + */ - initWithObject: (id)obj andPath: (const char*)p andMode: (const char*)m; + - free; + +/** + * \return An error message for the exception as a C String + */ - (char*)cString; + +/** + * \return A C string of the path to the file which couldn't be opened + */ - (char*)path; + +/** + * \return A C string of the mode in which the file should have been opened + */ - (char*)mode; @end +/** + * An OFException indicating a read or write to the file failed. + */ @interface OFReadOrWriteFailedException: OFException { size_t req_size; size_t req_items; } +/** + * Creates a new read or write failed exception. + * + * \param obj The object which caused the exception + * \param size The requested size of the data that couldn't be read / written + * \param nitems The requested number of items that couldn't be read / written + * \return A new open file failed exception + */ + newWithObject: (id)obj andSize: (size_t)size andNItems: (size_t)nitems; + +/** + * Initializes an already allocated read or write failed exception. + * + * \param obj The object which caused the exception + * \param size The requested size of the data that couldn't be read / written + * \param nitems The requested number of items that couldn't be read / written + * \return A new open file failed exception + */ - initWithObject: (id)obj andSize: (size_t)size andNItems: (size_t)nitems; + +/** + * \return The requested size of the data that couldn't be read / written + */ - (size_t)requestedSize; + +/** + * \return The requested number of items that coudln't be read / written + */ - (size_t)requestedItems; @end +/** + * An OFException indicating a read to the file failed. + */ @interface OFReadFailedException: OFReadOrWriteFailedException +/** + * \return An error message for the exception as a C String + */ - (char*)cString; @end +/** + * An OFException indicating a write to the file failed. + */ @interface OFWriteFailedException: OFReadOrWriteFailedException +/** + * \return An error message for the exception as a C String + */ - (char*)cString; @end Index: src/OFFile.h ================================================================== --- src/OFFile.h +++ src/OFFile.h @@ -14,36 +14,126 @@ #import #import "OFObject.h" +/** + * The OFFile class provides functions to read, write and manipulate files. + */ @interface OFFile: OFObject { FILE *fp; } +/** + * \param path The path to the file to open as a C string + * \param mode The mode in which the file should be opened as a C string + * \return A new OFFile + */ + newWithPath: (const char*)path andMode: (const char*)mode; -+ (int)changeModeOfFile: (const char*)path - toMode: (mode_t)mode; -+ (int)changeOwnerOfFile: (const char*)path - toOwner: (uid_t)owner - andGroup: (gid_t)group; -+ (int)delete: (const char*)path; -+ (int)link: (const char*)src - to: (const char*)dest; -+ (int)symlink: (const char*)src - to: (const char*)dest; - +/** + * Changes the mode of a file. + * + * \param path The path to the file of which the mode should be changed as a + * C string + * \param mode The new mode for the file + * \return A boolean whether the operation succeeded + */ ++ (BOOL)changeModeOfFile: (const char*)path + toMode: (mode_t)mode; + +/** + * Changes the owner of a file. + * + * \param path The path to the file of which the owner should be changed as a + * C string + * \param owner The new owner for the file + * \param group The new group for the file + * \return A boolean whether the operation succeeded + */ ++ (BOOL)changeOwnerOfFile: (const char*)path + toOwner: (uid_t)owner + andGroup: (gid_t)group; + +/** + * Deletes a file. + * + * \param path The path to the file of which should be deleted as a C string + * \return A boolean whether the operation succeeded + */ ++ (BOOL)delete: (const char*)path; + +/** + * Hardlinks a file. + * + * \param src The path to the file of which should be linked as a C string + * \param dest The path to where the file should be linked as a C string + * \return A boolean whether the operation succeeded + */ ++ (BOOL)link: (const char*)src + to: (const char*)dest; + +/** + * Symlinks a file. + * + * \param src The path to the file of which should be symlinked as a C string + * \param dest The path to where the file should be symlinked as a C string + * \return A boolean whether the operation succeeded + */ ++ (BOOL)symlink: (const char*)src + to: (const char*)dest; + +/** + * Initializes an already allocated OFFile. + * + * \param path The path to the file to open as a C string + * \param mode The mode in which the file should be opened as a C string + * \return An initialized OFFile + */ - initWithPath: (const char*)path andMode: (const char*)mode; + - free; + +/** + * \return A boolean whether the end of the file has been reached + */ - (BOOL)atEndOfFile; + +/** + * Reads from the file into a buffer. + * + * \param buf The buffer into which the data is read + * \param size The size of the data that should be read. + * The buffer MUST be at least size * nitems big! + * \param nitems nitem The number of items to read + * The buffer MUST be at least size * nitems big! + * \return The number of bytes read + */ - (size_t)readIntoBuffer: (uint8_t*)buf withSize: (size_t)size - andNItems: (size_t)nItems; + andNItems: (size_t)nitems; + +/** + * Reads from the file into a new buffer. + * + * \param size The size of the data that should be read + * \param nitem The number of items to read + * \return A new buffer with the data read. + * It is part of the memory pool of the OFFile. + */ - (uint8_t*)readWithSize: (size_t)size andNItems: (size_t)nitems; + +/** + * Writes from a buffer into the file. + * + * \param buf The buffer from which the data is written to the file + * \param size The size of the data that should be written + * \param nitem The number of items to write + * \return The number of bytes written + */ - (size_t)writeBuffer: (uint8_t*)buf withSize: (size_t)size andNItems: (size_t)nitems; @end Index: src/OFFile.m ================================================================== --- src/OFFile.m +++ src/OFFile.m @@ -26,43 +26,43 @@ { return [[OFFile alloc] initWithPath: path andMode: mode]; } -+ (int)changeModeOfFile: (const char*)path ++ (BOOL)changeModeOfFile: (const char*)path toMode: (mode_t)mode { // FIXME: On error, throw exception - return chmod(path, mode); + return (chmod(path, mode) == 0 ? YES : NO); } -+ (int)changeOwnerOfFile: (const char*)path ++ (BOOL)changeOwnerOfFile: (const char*)path toOwner: (uid_t)owner andGroup: (gid_t)group { // FIXME: On error, throw exception - return chown(path, owner, group); + return (chown(path, owner, group) == 0 ? YES : NO); } -+ (int)delete: (const char*)path ++ (BOOL)delete: (const char*)path { // FIXME: On error, throw exception - return unlink(path); + return (unlink(path) == 0 ? YES : NO); } -+ (int)link: (const char*)src ++ (BOOL)link: (const char*)src to: (const char*)dest { // FIXME: On error, throw exception - return link(src, dest); + return (link(src, dest) == 0 ? YES : NO); } -+ (int)symlink: (const char*)src ++ (BOOL)symlink: (const char*)src to: (const char*)dest { // FIXME: On error, throw exception - return symlink(src, dest); + return (symlink(src, dest) == 0 ? YES : NO); } - initWithPath: (const char*)path andMode: (const char*)mode {