gno/sys/fileport/support/Write.c

1 line
10 KiB
C
Raw Normal View History

/* * Copyright (c) Kopriha Software, 1990-1991 * All Rights Reserved * * Write.CC * * Description: * This module exists to abstract the data of the file I/O * primitives of GS/OS. * * * History:Oct 13, 1990 Dave Created this file * * Feb 25, 1991 Dave Added I/O buffering * * May 26, 1991 Dave Added set EOF * * Jun 07, 1991 Dave Broke the single source into lots * of small sources so we can build * a library to use... * */ /* * define DEBUG_CODE * - add # to define to create the local * debug code (IE:module) */ #ifndef _KS_FILEIO_ #include "ks.fileio.h" #endif #pragma noroot /* ****************************************************************** * * ks_file_write - Perform an write to an open file (possibly using * * an internal buffer). * * * * History: Feb 27, 1991 Dave Created this routine * * ****************************************************************** */ #undef ROUTINE_NAME #define ROUTINE_NAME "ks_file_write" KS_E_ERROR ks_file_write(KS_FILE_PTR file_ptr, LongWord position, LongWord data_size, Pointer data_buffer) { /* ************************************************************** * * Local declarations: * * ************************************************************** */ KS_E_ERROR error; /* Holds error codes for subroutine*/ /* calls */ LongWord data_offset; /* Offset into the buffer to return*/ LongWord remaining_space; /* Space remaining in file buffer */ LongWord buffer_request; /* Size of each copy from the */ /* file buffer */ ROUTINE_ENTER(); /* ************************************************************** * * Verify the structure ID passed in is the correct one. * * ************************************************************** */ if (file_ptr->struct_id != KS_FILE_ID) { KS_ERROR(KS_E_INVALID_STRUCT_ID, KS_FILE_ID); }; /* ************************************************************** * * Zero the number of bytes transfered in the KS_FILE structure. * * ************************************************************** */ file_ptr->data_size = 0; /* ************************************************************** * * If there is a buffer, then lets put data into the file buffer.* * ************************************************************** */ if (file_ptr->buffer_size != NULL) { /* ********************************************************** * * Loop till we satisfy the request (or take an error) * * ********************************************************** */ data_offset = 0; while (data_size > 0) { /* ****************************************************** * * Calculate the remaining space in the buffer. If * * there is any space left in the buffer then lets copy * * as much as we need to into the file buffer. * * ****************************************************** */ remaining_space = (file_ptr->buffer_available) - (file_ptr->buffer_offset); if (remaining_space > 0) { buffer_request = MIN(data_size, remaining_space); COPY_BYTES(data_buffer, data_offset, file_ptr->buffer, file_ptr->buffer_offset,