fork of PCE focusing on macplus, supporting DaynaPort SCSI network emulation
at master 195 lines 3.8 kB view raw
1/***************************************************************************** 2 * pce * 3 *****************************************************************************/ 4 5/***************************************************************************** 6 * File name: src/utils/pri/comment.c * 7 * Created: 2013-12-19 by Hampa Hug <hampa@hampa.ch> * 8 * Copyright: (C) 2013 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 <stdio.h> 26#include <string.h> 27 28#include <drivers/pri/pri.h> 29 30 31int pri_comment_add (pri_img_t *img, const char *str) 32{ 33 unsigned char c; 34 const unsigned char *tmp; 35 36 if (img->comment_size > 0) { 37 c = 0x0a; 38 39 if (pri_img_add_comment (img, &c, 1)) { 40 return (1); 41 } 42 } 43 44 tmp = (const unsigned char *) str; 45 46 if (pri_img_add_comment (img, tmp, strlen (str))) { 47 return (1); 48 } 49 50 return (0); 51} 52 53int pri_comment_load (pri_img_t *img, const char *fname) 54{ 55 int c, cr; 56 unsigned i, nl; 57 FILE *fp; 58 unsigned char buf[256]; 59 60 fp = fopen (fname, "r"); 61 62 if (fp == NULL) { 63 return (1); 64 } 65 66 pri_img_set_comment (img, NULL, 0); 67 68 cr = 0; 69 nl = 0; 70 i = 0; 71 72 while (1) { 73 c = fgetc (fp); 74 75 if (c == EOF) { 76 break; 77 } 78 79 if (c == 0x0d) { 80 if (cr) { 81 nl += 1; 82 } 83 84 cr = 1; 85 } 86 else if (c == 0x0a) { 87 nl += 1; 88 cr = 0; 89 } 90 else { 91 if (cr) { 92 nl += 1; 93 } 94 95 if (i > 0) { 96 while (nl > 0) { 97 buf[i++] = 0x0a; 98 nl -= 1; 99 100 if (i >= 256) { 101 pri_img_add_comment (img, buf, i); 102 i = 0; 103 } 104 } 105 } 106 107 nl = 0; 108 cr = 0; 109 110 buf[i++] = c; 111 112 if (i >= 256) { 113 pri_img_add_comment (img, buf, i); 114 i = 0; 115 } 116 } 117 } 118 119 if (i > 0) { 120 pri_img_add_comment (img, buf, i); 121 i = 0; 122 } 123 124 fclose (fp); 125 126 if (par_verbose) { 127 fprintf (stderr, "%s: load comments from %s\n", arg0, fname); 128 } 129 130 return (0); 131} 132 133int pri_comment_save (pri_img_t *img, const char *fname) 134{ 135 unsigned cnt; 136 FILE *fp; 137 138 fp = fopen (fname, "w"); 139 140 if (fp == NULL) { 141 return (1); 142 } 143 144 cnt = img->comment_size; 145 146 if (cnt > 0) { 147 if (fwrite (img->comment, 1, cnt, fp) != cnt) { 148 fclose (fp); 149 return (1); 150 } 151 152 fputc (0x0a, fp); 153 } 154 155 fclose (fp); 156 157 if (par_verbose) { 158 fprintf (stderr, "%s: save comments to %s\n", arg0, fname); 159 } 160 161 return (0); 162} 163 164int pri_comment_set (pri_img_t *img, const char *str) 165{ 166 const unsigned char *tmp; 167 168 if ((str == NULL) || (*str == 0)) { 169 pri_img_set_comment (img, NULL, 0); 170 return (0); 171 } 172 173 tmp = (const unsigned char *) str; 174 175 if (pri_img_set_comment (img, tmp, strlen (str))) { 176 return (1); 177 } 178 179 return (0); 180} 181 182int pri_comment_show (pri_img_t *img) 183{ 184 unsigned i; 185 186 fputs ("comments:\n", stdout); 187 188 for (i = 0; i < img->comment_size; i++) { 189 fputc (img->comment[i], stdout); 190 } 191 192 fputs ("\n", stdout); 193 194 return (0); 195}