fork of PCE focusing on macplus, supporting DaynaPort SCSI network emulation
1/*****************************************************************************
2 * pce *
3 *****************************************************************************/
4
5/*****************************************************************************
6 * File name: src/utils/pti/comment.c *
7 * Created: 2020-04-25 by Hampa Hug <hampa@hampa.ch> *
8 * Copyright: (C) 2020 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/pti/pti.h>
31
32
33int pti_comment_add (pti_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 (pti_img_add_comment (img, &c, 1)) {
42 return (1);
43 }
44 }
45
46 tmp = (const unsigned char *) str;
47
48 if (pti_img_add_comment (img, tmp, strlen (str))) {
49 return (1);
50 }
51
52 return (0);
53}
54
55int pti_comment_load (pti_img_t *img, const char *fname)
56{
57 int c, cr;
58 unsigned i, nl;
59 FILE *fp;
60 unsigned char buf[256];
61
62 if ((fp = fopen (fname, "r")) == NULL) {
63 return (1);
64 }
65
66 pti_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 pti_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 pti_img_add_comment (img, buf, i);
114 i = 0;
115 }
116 }
117 }
118
119 if (i > 0) {
120 pti_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 pti_comment_save (pti_img_t *img, const char *fname)
134{
135 unsigned cnt;
136 FILE *fp;
137
138 if ((fp = fopen (fname, "w")) == NULL) {
139 return (1);
140 }
141
142 cnt = img->comment_size;
143
144 if (cnt > 0) {
145 if (fwrite (img->comment, 1, cnt, fp) != cnt) {
146 fclose (fp);
147 return (1);
148 }
149
150 fputc (0x0a, fp);
151 }
152
153 fclose (fp);
154
155 if (par_verbose) {
156 fprintf (stderr, "%s: save comments to %s\n", arg0, fname);
157 }
158
159 return (0);
160}
161
162int pti_comment_set (pti_img_t *img, const char *str)
163{
164 const unsigned char *tmp;
165
166 if ((str == NULL) || (*str == 0)) {
167 pti_img_set_comment (img, NULL, 0);
168 return (0);
169 }
170
171 tmp = (const unsigned char *) str;
172
173 if (pti_img_set_comment (img, tmp, strlen (str))) {
174 return (1);
175 }
176
177 return (0);
178}
179
180int pti_comment_show (pti_img_t *img)
181{
182 unsigned i;
183
184 fputs ("comments:\n", stdout);
185
186 for (i = 0; i < img->comment_size; i++) {
187 fputc (img->comment[i], stdout);
188 }
189
190 fputs ("\n", stdout);
191
192 return (0);
193}