forked from scottransom/psrfits_utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ascii_header.c
209 lines (174 loc) · 4.68 KB
/
ascii_header.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/***************************************************************************
*
* Copyright (C) 2002-2008 by Willem van Straten
* Licensed under the Academic Free License version 2.1
*
***************************************************************************/
//#include <config.h>
#include "ascii_header.h"
#define STRLEN 4096
#define DEFAULT_HEADER_SIZE STRLEN
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
static char* whitespace = " \t\n";
// search header for keyword and ensure that it is preceded by whitespace */
char* ascii_header_find (const char* header, const char* keyword)
{
char* key = strstr (header, keyword);
// keyword might be the very first word in header
while (key > header)
{
// fprintf (stderr, "found=%s", key);
// if preceded by a new line, return the found key
if ( ((*(key-1) == '\n') || (*(key-1) == '\\')) &&
((*(key+strlen(keyword)) == '\t') || (*(key+strlen(keyword)) == ' ')))
break;
// otherwise, search again, starting one byte later
key = strstr (key+1, keyword);
}
return key;
}
int ascii_header_set (char* header, const char* keyword,
const char* format, ...)
{
va_list arguments;
char value[STRLEN];
char* eol = 0;
char* dup = 0;
int ret = 0;
/* find the keyword (also the insertion point) */
char* key = ascii_header_find (header, keyword);
if (key) {
/* if the keyword is present, find the first '#' or '\n' to follow it */
eol = key + strcspn (key, "#\n");
}
else {
/* if the keyword is not present, append to the end, before "DATA" */
eol = strstr (header, "DATA\n");
if (eol)
/* insert in front of DATA */
key = eol;
else
/* insert at end of string */
key = header + strlen (header);
}
va_start (arguments, format);
ret = vsnprintf (value, STRLEN, format, arguments);
va_end (arguments);
if (ret < 0) {
perror ("ascii_header_set: error snprintf\n");
return -1;
}
if (eol)
/* make a copy */
dup = strdup (eol);
/* %Xs dictates only a minumum string length */
if (sprintf (key, "%-12s %-20s ", keyword, value) < 0) {
if (dup)
free (dup);
perror ("ascii_header_set: error sprintf\n");
return -1;
}
if (dup) {
strcat (key, dup);
free (dup);
}
else
strcat (key, "\n");
return 0;
}
int ascii_header_get (const char* header, const char* keyword,
const char* format, ...)
{
va_list arguments;
char* value = 0;
int ret = 0;
/* find the keyword */
char* key = ascii_header_find (header, keyword);
if (!key)
return -1;
/* find the value after the keyword */
value = key + strcspn (key, whitespace);
/* parse the value */
va_start (arguments, format);
ret = vsscanf (value, format, arguments);
va_end (arguments);
return ret;
}
int ascii_header_del (char * header, const char * keyword)
{
/* find the keyword (also the delete from point) */
char * key = ascii_header_find (header, keyword);
/* if the keyword is present, find the first '#' or '\n' to follow it */
if (key)
{
char * eol = key + strcspn (key, "\n") + 1;
// make a copy of everything after the end of the key we are deleting
char * dup = strdup (eol);
if (dup)
{
key[0] = '\0';
strcat (header, dup);
free (dup);
return 0;
}
else
return -1;
}
else
return -1;
}
size_t ascii_header_get_size (char * filename)
{
size_t hdr_size = -1;
int fd = open (filename, O_RDONLY);
if (!fd)
{
fprintf (stderr, "ascii_header_get_size: failed to open %s for reading\n", filename);
}
else
{
hdr_size = ascii_header_get_size_fd (fd);
close (fd);
}
return hdr_size;
}
size_t ascii_header_get_size_fd (int fd)
{
size_t hdr_size = -1;
char * header = (char *) malloc (DEFAULT_HEADER_SIZE+1);
if (!header)
{
fprintf (stderr, "ascii_header_get_size: failed to allocate %d bytes\n", DEFAULT_HEADER_SIZE+1);
}
else
{
// seek to start of file
lseek (fd, 0, SEEK_SET);
// read the header
ssize_t ret = read (fd, header, DEFAULT_HEADER_SIZE);
if (ret != DEFAULT_HEADER_SIZE)
{
fprintf (stderr, "ascii_header_get_size: failed to read %d bytes from file\n", DEFAULT_HEADER_SIZE);
}
else
{
// check the actual HDR_SIZE in the header
if (ascii_header_get (header, "HDR_SIZE", "%ld", &hdr_size) != 1)
{
fprintf (stderr, "ascii_header_get_size: failed to read HDR_SIZE from header\n");
hdr_size = -1;
}
}
// seek back to start of file
lseek (fd, 0, SEEK_SET);
free (header);
}
return hdr_size;
}