/* return the pos in the file ; RETURN_FAILURE in case of error */ int writec_ifile(file_desc_t *fd, char c) { unsigned int ibloc; /* write the char in the buffer */ fd->fds_buf[ibloc_of_pos(fd->fds_pos)] = c; /* first write in the bloc ? ensure the data bloc allocation */ if (! fd->fds_dirty) { ibloc = vbloc_of_fbloc(fd->fds_inumber, bloc_of_pos(fd->fds_pos), TRUE); if (! ibloc) { return RETURN_FAILURE; } fd->fds_dirty = TRUE; } /* is the buffer full? */ if (ibloc_of_pos(fd->fds_pos) == BLOC_SIZE-1) { /* write the buffer */ ibloc = vbloc_of_fbloc(fd->fds_inumber, bloc_of_pos(fd->fds_pos), FALSE); write_bloc(current_vol, ibloc, fd->fds_buf); /* read the new buffer */ ibloc = vbloc_of_fbloc(fd->fds_inumber, bloc_of_pos(fd->fds_pos+1), FALSE); if (! ibloc) memset(fd->fds_buf, 0, BLOC_SIZE); else read_bloc(current_vol, ibloc, fd->fds_buf); fd->fds_dirty = FALSE; } /* update the file cursor and size */ if (fd->fds_size < fd->fds_pos) fd->fds_size = fd->fds_pos; fd->fds_pos++; /* the position of the written char */ return fd->fds_pos - 1; }
/*------------------------------ Read a char in a file ------------------------------------------------------------*/ int readc_ifile(file_desc_t *fd) { char c; /* eof? */ if (fd->fds_pos > fd->fds_size) return READ_EOF; /* the data is in the buffer, just return it */ c = fd->fds_buf[ibloc_of_pos(fd->fds_pos)]; /* seek + 1 */ seek_ifile(fd, 1); return c; }