adamdunkels | 28144b4 | 2004-02-16 21:00:14 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \addtogroup c64fs |
| 3 | * @{ |
| 4 | */ |
| 5 | |
| 6 | /** |
| 7 | * \file |
| 8 | * Implementation of C64 file writes. |
| 9 | * \author Adam Dunkels <adam@dunkels.com> |
| 10 | * |
| 11 | * The functions in this file are not included in the core Contiki |
| 12 | * code, but must be explicitly linked by an application that that |
| 13 | * wishes to be able to write to files. |
| 14 | */ |
| 15 | |
| 16 | |
adamdunkels | fbd87db | 2003-08-06 22:58:21 +0000 | [diff] [blame] | 17 | /* |
| 18 | * Copyright (c) 2003, Adam Dunkels. |
| 19 | * All rights reserved. |
| 20 | * |
| 21 | * Redistribution and use in source and binary forms, with or without |
| 22 | * modification, are permitted provided that the following conditions |
| 23 | * are met: |
| 24 | * 1. Redistributions of source code must retain the above copyright |
| 25 | * notice, this list of conditions and the following disclaimer. |
| 26 | * 2. Redistributions in binary form must reproduce the above |
| 27 | * copyright notice, this list of conditions and the following |
| 28 | * disclaimer in the documentation and/or other materials provided |
| 29 | * with the distribution. |
adamdunkels | 28144b4 | 2004-02-16 21:00:14 +0000 | [diff] [blame] | 30 | * 3. The name of the author may not be used to endorse or promote |
adamdunkels | fbd87db | 2003-08-06 22:58:21 +0000 | [diff] [blame] | 31 | * products derived from this software without specific prior |
| 32 | * written permission. |
| 33 | * |
| 34 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
| 35 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 36 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 37 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 38 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 39 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 40 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 41 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 42 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 43 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 44 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 45 | * |
| 46 | * This file is part of the Contiki desktop environment |
| 47 | * |
adamdunkels | 28144b4 | 2004-02-16 21:00:14 +0000 | [diff] [blame] | 48 | * $Id: c64-fs-write.c,v 1.3 2004/02/16 21:00:14 adamdunkels Exp $ |
adamdunkels | fbd87db | 2003-08-06 22:58:21 +0000 | [diff] [blame] | 49 | * |
| 50 | */ |
| 51 | |
| 52 | #include "c64-dio.h" |
| 53 | #include "c64-fs.h" |
| 54 | #include <string.h> |
| 55 | |
| 56 | /* An *UGLY* implementation of c64_fs_write that only can be used to |
| 57 | overwrite a single block file. */ |
| 58 | |
| 59 | extern unsigned char _c64_fs_filebuf[256]; |
| 60 | extern unsigned char _c64_fs_filebuftrack, |
| 61 | _c64_fs_filebufsect; |
| 62 | |
| 63 | |
| 64 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 28144b4 | 2004-02-16 21:00:14 +0000 | [diff] [blame] | 65 | /** |
| 66 | * Write data to an open file. |
| 67 | * |
| 68 | * \note This function currently does not support writing to other than a single block file (cannot be more than 254 bytes long). |
| 69 | * |
| 70 | * \param f A pointer to a file descriptor previously opened with c64_fs_open(). |
| 71 | * |
| 72 | * \param buf A pointer to a buffer with data that should be written |
| 73 | * to the file. |
| 74 | * |
| 75 | * \param len The length of the data that should be written. |
| 76 | * |
| 77 | * \return The number of bytes actually written. |
| 78 | * |
| 79 | */ |
| 80 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | fbd87db | 2003-08-06 22:58:21 +0000 | [diff] [blame] | 81 | int __fastcall__ |
| 82 | c64_fs_write(register struct c64_fs_file *f, char *buf, int len) |
| 83 | { |
| 84 | int i; |
| 85 | |
| 86 | if(len <= 0) { |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | /* Check if current block is already in buffer, and if not read it |
| 91 | from disk. */ |
| 92 | if(_c64_fs_filebuftrack != f->track || |
| 93 | _c64_fs_filebufsect != f->sect) { |
| 94 | _c64_fs_filebuftrack = f->track; |
| 95 | _c64_fs_filebufsect = f->sect; |
| 96 | c64_dio_read_block(_c64_fs_filebuftrack, |
| 97 | _c64_fs_filebufsect, |
| 98 | _c64_fs_filebuf); |
| 99 | } |
| 100 | |
| 101 | i = 256 - f->ptr; |
| 102 | if(len < i) { |
| 103 | i = len; |
| 104 | } |
| 105 | |
| 106 | memcpy(&_c64_fs_filebuf[f->ptr], buf, i); |
| 107 | |
| 108 | f->ptr += i; |
adamdunkels | 097c410 | 2003-08-06 23:39:33 +0000 | [diff] [blame] | 109 | if(_c64_fs_filebuf[0] == 0 && |
| 110 | f->ptr > _c64_fs_filebuf[1]) { |
| 111 | _c64_fs_filebuf[1] = f->ptr; |
| 112 | } |
adamdunkels | fbd87db | 2003-08-06 22:58:21 +0000 | [diff] [blame] | 113 | |
| 114 | c64_dio_write_block(_c64_fs_filebuftrack, |
| 115 | _c64_fs_filebufsect, |
| 116 | _c64_fs_filebuf); |
| 117 | |
| 118 | return i; |
| 119 | } |
| 120 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 28144b4 | 2004-02-16 21:00:14 +0000 | [diff] [blame] | 121 | /** @} */ |