fork of PCE focusing on macplus, supporting DaynaPort SCSI network emulation
at master 192 lines 3.8 kB view raw
1/***************************************************************************** 2 * pce * 3 *****************************************************************************/ 4 5/***************************************************************************** 6 * File name: src/utils/pfi/comment.c * 7 * Created: 2013-12-26 by Hampa Hug <hampa@hampa.ch> * 8 * Copyright: (C) 2013-2017 Hampa Hug <hampa@hampa.ch> * 9 *****************************************************************************/ 10 11/***************************************************************************** 12 * This program is free software. You can redistribute it and / or modify it * 13 * under the terms of the GNU General Public License version 2 as published * 14 * by the Free Software Foundation. * 15 * * 16 * This program is distributed in the hope that it will be useful, but * 17 * WITHOUT ANY WARRANTY, without even the implied warranty of * 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * 19 * Public License for more details. * 20 *****************************************************************************/ 21 22 23#include "main.h" 24 25#include <stdlib.h> 26#include <stdio.h> 27#include <string.h> 28 29#include <drivers/pfi/pfi.h> 30 31 32int pfi_comment_add (pfi_img_t *img, const char *str) 33{ 34 unsigned char c; 35 const unsigned char *tmp; 36 37 if (img->comment_size > 0) { 38 c = 0x0a; 39 40 if (pfi_img_add_comment (img, &c, 1)) { 41 return (1); 42 } 43 } 44 45 tmp = (const unsigned char *) str; 46 47 if (pfi_img_add_comment (img, tmp, strlen (str))) { 48 return (1); 49 } 50 51 return (0); 52} 53 54int pfi_comment_load (pfi_img_t *img, const char *fname) 55{ 56 int c, cr; 57 unsigned i, nl; 58 FILE *fp; 59 unsigned char buf[256]; 60 61 if ((fp = fopen (fname, "r")) == NULL) { 62 return (1); 63 } 64 65 pfi_img_set_comment (img, NULL, 0); 66 67 cr = 0; 68 nl = 0; 69 i = 0; 70 71 while (1) { 72 c = fgetc (fp); 73 74 if (c == EOF) { 75 break; 76 } 77 78 if (c == 0x0d) { 79 if (cr) { 80 nl += 1; 81 } 82 83 cr = 1; 84 } 85 else if (c == 0x0a) { 86 nl += 1; 87 cr = 0; 88 } 89 else { 90 if (cr) { 91 nl += 1; 92 } 93 94 if (i > 0) { 95 while (nl > 0) { 96 buf[i++] = 0x0a; 97 nl -= 1; 98 99 if (i >= 256) { 100 pfi_img_add_comment (img, buf, i); 101 i = 0; 102 } 103 } 104 } 105 106 nl = 0; 107 cr = 0; 108 109 buf[i++] = c; 110 111 if (i >= 256) { 112 pfi_img_add_comment (img, buf, i); 113 i = 0; 114 } 115 } 116 } 117 118 if (i > 0) { 119 pfi_img_add_comment (img, buf, i); 120 i = 0; 121 } 122 123 fclose (fp); 124 125 if (par_verbose) { 126 fprintf (stderr, "%s: load comments from %s\n", arg0, fname); 127 } 128 129 return (0); 130} 131 132int pfi_comment_save (pfi_img_t *img, const char *fname) 133{ 134 unsigned cnt; 135 FILE *fp; 136 137 if ((fp = fopen (fname, "w")) == NULL) { 138 return (1); 139 } 140 141 cnt = img->comment_size; 142 143 if (cnt > 0) { 144 if (fwrite (img->comment, 1, cnt, fp) != cnt) { 145 fclose (fp); 146 return (1); 147 } 148 149 fputc (0x0a, fp); 150 } 151 152 fclose (fp); 153 154 if (par_verbose) { 155 fprintf (stderr, "%s: save comments to %s\n", arg0, fname); 156 } 157 158 return (0); 159} 160 161int pfi_comment_set (pfi_img_t *img, const char *str) 162{ 163 const unsigned char *tmp; 164 165 if ((str == NULL) || (*str == 0)) { 166 pfi_img_set_comment (img, NULL, 0); 167 return (0); 168 } 169 170 tmp = (const unsigned char *) str; 171 172 if (pfi_img_set_comment (img, tmp, strlen (str))) { 173 return (1); 174 } 175 176 return (0); 177} 178 179int pfi_comment_show (pfi_img_t *img) 180{ 181 unsigned i; 182 183 fputs ("comments:\n", stdout); 184 185 for (i = 0; i < img->comment_size; i++) { 186 fputc (img->comment[i], stdout); 187 } 188 189 fputs ("\n", stdout); 190 191 return (0); 192}