-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuffer.c
157 lines (130 loc) · 3.21 KB
/
buffer.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
//
// Created by paul on 12/28/20.
//
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "buffer.h"
tBuffer * bufferNew( const char * p, size_t len )
{
tBuffer * result = malloc(sizeof(tBuffer));
result->pointer = p;
result->remaining = len;
return result;
}
void bufferDropAnchor( tBuffer * buffer )
{
buffer->anchor = buffer->pointer;
}
const char * bufferGetAnchorString( tBuffer * buffer )
{
char * result = NULL;
size_t len = buffer->pointer - buffer->anchor;
if ( len > 0 )
{
result = malloc( len + 1 );
if ( result != NULL)
{
memcpy( result, buffer->anchor, len );
result[len] = '\0';
}
}
return (const char *)result;
}
unsigned char bufferGetChar( tBuffer * buffer )
{
unsigned char result = '\0';
if ( buffer->remaining > 0 )
{
result = *buffer->pointer++;
buffer->remaining--;
}
return result;
}
size_t bufferGetRemaining( tBuffer * buffer )
{
return buffer->remaining;
}
const char * bufferGetQuotedString( tBuffer * buffer )
{
char * result = NULL;
const char * start = buffer->pointer;
while ( buffer->remaining > 0
&& *buffer->pointer != '\"'
&& *buffer->pointer != '\n'
&& *buffer->pointer != '\r' )
{
buffer->pointer++;
buffer->remaining--;
}
unsigned int len = buffer->pointer - start;
/* Skip over trailing quote(s) */
/* Sometimes entries in the M3U are malformed, and has two adjacent closing quotes */
while ( buffer->remaining > 0
&& *buffer->pointer == '\"' )
{
buffer->pointer++;
buffer->remaining--;
}
result = malloc( len + 1 );
if ( result != NULL )
{
memcpy( result, start, len );
/* always null-terminated */
result[len] = '\0';
}
return (const char *)result;
}
const char * bufferGetStringToEOL( tBuffer * buffer )
{
char * result;
const char * start = buffer->pointer;
while ( buffer->remaining > 0
&& *buffer->pointer != '\n'
&& *buffer->pointer != '\r' )
{
buffer->pointer++;
buffer->remaining--;
}
unsigned int len = buffer->pointer - start;
/* skip past any run of end-of-line characters */
while ( buffer->remaining > 0
&& ( *buffer->pointer == '\n' || *buffer->pointer == '\r') )
{
buffer->pointer++;
buffer->remaining--;
}
result = NULL;
if ( len > 0 )
{
result = malloc( len + 1 );
if ( result != NULL )
{
memcpy( result, start, len );
/* always null-terminated */
result[len] = '\0';
}
}
return (const char *)result;
}
void bufferPrintToEOL( FILE * output, tBuffer * buffer )
{
const char * end = buffer->pointer;
size_t left = buffer->remaining;
while ( *end != '\0'
&& *end != '\n'
&& *end != '\r'
&& left > 0 )
{
end++;
left--;
}
size_t len = end - buffer->pointer;
if (len > buffer->remaining)
{
len = buffer->remaining;
}
fputc( '\'', output );
fwrite( buffer->pointer, len, 1, output );
fputs( "\'\n", output );
}