fork of PCE focusing on macplus, supporting DaynaPort SCSI network emulation
at master 154 lines 3.4 kB view raw
1/***************************************************************************** 2 * pce * 3 *****************************************************************************/ 4 5/***************************************************************************** 6 * File name: src/utils/psi/comment.c * 7 * Created: 2013-06-09 by Hampa Hug <hampa@hampa.ch> * 8 * Copyright: (C) 2013-2024 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#include "comment.h" 25 26#include <stdlib.h> 27#include <stdio.h> 28#include <string.h> 29 30#include <drivers/psi/psi.h> 31 32 33int psi_add_comment (psi_img_t *img, const char *str) 34{ 35 unsigned char c; 36 const unsigned char *tmp; 37 38 if (img->comment_size > 0) { 39 c = 0x0a; 40 41 if (psi_img_add_comment (img, &c, 1)) { 42 return (1); 43 } 44 } 45 46 tmp = (const unsigned char *) str; 47 48 if (psi_img_add_comment (img, tmp, strlen (str))) { 49 return (1); 50 } 51 52 psi_img_clean_comment (img); 53 54 return (0); 55} 56 57int psi_load_comment (psi_img_t *img, const char *fname) 58{ 59 unsigned n; 60 FILE *fp; 61 unsigned char buf[256]; 62 63 fp = fopen (fname, "r"); 64 65 if (fp == NULL) { 66 return (1); 67 } 68 69 psi_img_set_comment (img, NULL, 0); 70 71 while (1) { 72 n = fread (buf, 1, 256, fp); 73 74 if (n == 0) { 75 break; 76 } 77 78 psi_img_add_comment (img, buf, n); 79 } 80 81 fclose (fp); 82 83 psi_img_clean_comment (img); 84 85 if (par_verbose) { 86 fprintf (stderr, "%s: load comments from %s\n", arg0, fname); 87 } 88 89 return (0); 90} 91 92int psi_save_comment (psi_img_t *img, const char *fname) 93{ 94 unsigned cnt; 95 FILE *fp; 96 97 fp = fopen (fname, "w"); 98 99 if (fp == NULL) { 100 return (1); 101 } 102 103 cnt = img->comment_size; 104 105 if (cnt > 0) { 106 if (fwrite (img->comment, 1, cnt, fp) != cnt) { 107 fclose (fp); 108 return (1); 109 } 110 111 fputc (0x0a, fp); 112 } 113 114 fclose (fp); 115 116 if (par_verbose) { 117 fprintf (stderr, "%s: save comments to %s\n", arg0, fname); 118 } 119 120 return (0); 121} 122 123int psi_set_comment (psi_img_t *img, const char *str) 124{ 125 const unsigned char *tmp; 126 127 if ((str == NULL) || (*str == 0)) { 128 psi_img_set_comment (img, NULL, 0); 129 return (0); 130 } 131 132 tmp = (const unsigned char *) str; 133 134 if (psi_img_set_comment (img, tmp, strlen (str))) { 135 return (1); 136 } 137 138 psi_img_clean_comment (img); 139 140 return (0); 141} 142 143int psi_show_comment (psi_img_t *img) 144{ 145 unsigned i; 146 147 for (i = 0; i < img->comment_size; i++) { 148 fputc (img->comment[i], stdout); 149 } 150 151 fputs ("\n", stdout); 152 153 return (0); 154}