fork of PCE focusing on macplus, supporting DaynaPort SCSI network emulation
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

char-stdio: Support reading from an input file

+77 -31
+8 -1
doc/char-drivers.txt
··· 104 104 105 105 106 106 stdio: 107 - Output is written to a file. No input is ever provided. 107 + Output is written to a file. Input is read from a file. 108 108 109 109 file=<filename> 110 110 Set the output file name. ··· 114 114 115 115 flush=[0|1] 116 116 If true, flush the output file whenever data is written. 117 + 118 + read=<filename> 119 + Set the input file name. 120 + 121 + write=<filename> 122 + Set the output file name. This option is synonymous to 123 + the "file" options. 117 124 118 125 119 126 tcp:
+63 -26
src/drivers/char/char-stdio.c
··· 5 5 /***************************************************************************** 6 6 * File name: src/drivers/char/char-stdio.c * 7 7 * Created: 2009-03-06 by Hampa Hug <hampa@hampa.ch> * 8 - * Copyright: (C) 2009-2019 Hampa Hug <hampa@hampa.ch> * 8 + * Copyright: (C) 2009-2020 Hampa Hug <hampa@hampa.ch> * 9 9 *****************************************************************************/ 10 10 11 11 /***************************************************************************** ··· 45 45 return (0); 46 46 } 47 47 48 - if (drv->fname == NULL) { 48 + if (drv->write_name == NULL) { 49 49 return (0); 50 50 } 51 51 52 - if (stat (drv->fname, &st) == 0) { 52 + if (stat (drv->write_name, &st) == 0) { 53 53 return (0); 54 54 } 55 55 56 - if ((drv->fp != NULL) && (drv->fp != stdout)) { 57 - fclose (drv->fp); 56 + if ((drv->write_fp != NULL) && (drv->write_fp != stdout)) { 57 + fclose (drv->write_fp); 58 58 } 59 59 60 - drv->fp = fopen (drv->fname, "wb"); 60 + drv->write_fp = fopen (drv->write_name, "wb"); 61 61 #endif 62 62 63 63 return (0); 64 64 } 65 65 66 66 static 67 + void chr_stdio_fclose (char *name, FILE *fp) 68 + { 69 + if (name != NULL) { 70 + free (name); 71 + } 72 + 73 + if ((fp != NULL) && (fp != stdin) && (fp != stdout) && (fp != stderr)) { 74 + fclose (fp); 75 + } 76 + } 77 + 78 + static 67 79 void chr_stdio_close (char_drv_t *cdrv) 68 80 { 69 81 char_stdio_t *drv; 70 82 71 83 drv = cdrv->ext; 72 84 73 - if (drv->fname != NULL) { 74 - free (drv->fname); 75 - } 76 - 77 - if (drv->fp != NULL) { 78 - if ((drv->fp != stdin) && (drv->fp != stdout) && (drv->fp != stderr)) { 79 - fclose (drv->fp); 80 - } 81 - } 85 + chr_stdio_fclose (drv->write_name, drv->write_fp); 86 + chr_stdio_fclose (drv->read_name, drv->read_fp); 82 87 83 88 free (drv); 84 89 } ··· 86 91 static 87 92 unsigned chr_stdio_read (char_drv_t *cdrv, void *buf, unsigned cnt) 88 93 { 89 - return (0); 94 + char_stdio_t *drv; 95 + 96 + drv = cdrv->ext; 97 + 98 + if (drv->read_fp == NULL) { 99 + return (0); 100 + } 101 + 102 + cnt = fread (buf, 1, cnt, drv->read_fp); 103 + 104 + return (cnt); 90 105 } 91 106 92 107 static ··· 98 113 99 114 stdio_check_reopen (drv); 100 115 101 - if (drv->fp == NULL) { 116 + if (drv->write_fp == NULL) { 102 117 return (cnt); 103 118 } 104 119 105 - cnt = fwrite (buf, 1, cnt, drv->fp); 120 + cnt = fwrite (buf, 1, cnt, drv->write_fp); 106 121 107 122 if (drv->flush) { 108 - fflush (drv->fp); 123 + fflush (drv->write_fp); 109 124 } 110 125 111 126 return (cnt); ··· 120 135 drv->cdrv.read = chr_stdio_read; 121 136 drv->cdrv.write = chr_stdio_write; 122 137 123 - drv->fp = NULL; 138 + drv->read_name = NULL; 139 + drv->read_fp = NULL; 140 + drv->write_name = NULL; 141 + drv->write_fp = NULL; 124 142 125 - drv->fname = drv_get_option (name, "file"); 126 143 drv->flush = drv_get_option_bool (name, "flush", 1); 127 144 drv->reopen = drv_get_option_bool (name, "reopen", 0); 128 145 129 - if (drv->fname != NULL) { 130 - if (strcmp (drv->fname, "-") == 0) { 131 - drv->fp = stdout; 146 + drv->write_name = drv_get_option (name, "write"); 147 + 148 + if (drv->write_name == NULL) { 149 + drv->write_name = drv_get_option (name, "file"); 150 + } 151 + 152 + if (drv->write_name != NULL) { 153 + if (strcmp (drv->write_name, "-") == 0) { 154 + drv->write_fp = stdout; 132 155 drv->reopen = 0; 133 156 } 134 157 else { 135 - drv->fp = fopen (drv->fname, "wb"); 158 + drv->write_fp = fopen (drv->write_name, "wb"); 136 159 137 - if (drv->fp == NULL) { 160 + if (drv->write_fp == NULL) { 138 161 return (1); 139 162 } 140 163 } 141 164 } 142 165 166 + drv->read_name = drv_get_option (name, "read"); 167 + 168 + if (drv->read_name != NULL) { 169 + if (strcmp (drv->read_name, "-") == 0) { 170 + drv->read_fp = stdin; 171 + } 172 + else { 173 + drv->read_fp = fopen (drv->read_name, "rb"); 174 + 175 + if (drv->read_fp == NULL) { 176 + return (1); 177 + } 178 + } 179 + } 143 180 return (0); 144 181 } 145 182
+6 -4
src/drivers/char/char-stdio.h
··· 5 5 /***************************************************************************** 6 6 * File name: src/drivers/char/char-stdio.h * 7 7 * Created: 2009-03-06 by Hampa Hug <hampa@hampa.ch> * 8 - * Copyright: (C) 2009-2019 Hampa Hug <hampa@hampa.ch> * 8 + * Copyright: (C) 2009-2020 Hampa Hug <hampa@hampa.ch> * 9 9 *****************************************************************************/ 10 10 11 11 /***************************************************************************** ··· 32 32 typedef struct char_stdio_t { 33 33 char_drv_t cdrv; 34 34 35 - char *fname; 36 - 37 35 char flush; 38 36 char reopen; 39 37 40 - FILE *fp; 38 + char *read_name; 39 + FILE *read_fp; 40 + 41 + char *write_name; 42 + FILE *write_fp; 41 43 } char_stdio_t; 42 44 43 45