1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/fsaccess/input.c Fri Mar 18 00:14:07 2022 +0100
1.3 @@ -0,0 +1,118 @@
1.4 +/*
1.5 + * Elementary input utilities.
1.6 + *
1.7 + * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#include <string.h>
1.26 +
1.27 +#include "input.h"
1.28 +
1.29 +
1.30 +
1.31 +/* Read a line from a file into the given buffer. */
1.32 +
1.33 +char *read_line(FILE *fp, struct read_line_state *state)
1.34 +{
1.35 + size_t nread;
1.36 +
1.37 + do
1.38 + {
1.39 + do
1.40 + {
1.41 + /* Search for a newline character in any available text. */
1.42 +
1.43 + if (state->end > state->start)
1.44 + {
1.45 + state->eolp = strchr(state->start, '\n');
1.46 +
1.47 + if (state->eolp != NULL)
1.48 + {
1.49 + *(state->eolp) = '\0';
1.50 + return state->eolp;
1.51 + }
1.52 + }
1.53 +
1.54 + /* Obtain more text if necessary. */
1.55 +
1.56 + nread = fread(state->end, sizeof(char), state->remaining, fp);
1.57 +
1.58 + /* Handle end of file condition. */
1.59 +
1.60 + if (!nread)
1.61 + {
1.62 + if (state->end > state->start)
1.63 + return state->end;
1.64 + else
1.65 + return NULL;
1.66 + }
1.67 +
1.68 + /* Zero-terminate the string for searching. */
1.69 +
1.70 + *(state->end + nread) = '\0';
1.71 +
1.72 + /* Advance the end of string and subtract remaining space. */
1.73 +
1.74 + state->end += nread;
1.75 + state->remaining -= nread;
1.76 + }
1.77 + while (state->remaining);
1.78 +
1.79 + /* Copy the remaining text to the start of the buffer. */
1.80 +
1.81 + if (state->start > state->buffer)
1.82 + {
1.83 + strcpy(state->buffer, state->start);
1.84 +
1.85 + state->end -= (state->start - state->buffer);
1.86 + state->start = state->buffer;
1.87 + state->remaining = state->buffer_size - 1 - (state->end - state->buffer);
1.88 + }
1.89 + }
1.90 + while (state->remaining);
1.91 +
1.92 + return NULL;
1.93 +}
1.94 +
1.95 +/* Parse the text in the given region, returning details of arguments. */
1.96 +
1.97 +void parse_line(char *start, char *end, int *num_args, char *args[], const int max_args)
1.98 +{
1.99 + *num_args = 0;
1.100 +
1.101 + while ((start != NULL) && (start < end) && (*num_args < max_args))
1.102 + {
1.103 + args[*num_args] = start;
1.104 + (*num_args)++;
1.105 +
1.106 + /* NOTE: Only handling spaces as delimiters. */
1.107 +
1.108 + start = strchr(start, ' ');
1.109 +
1.110 + if (start != NULL)
1.111 + {
1.112 + *start = '\0';
1.113 +
1.114 + if (start < end)
1.115 + start++;
1.116 + }
1.117 + }
1.118 +}
1.119 +
1.120 +/* vim: tabstop=4 expandtab shiftwidth=4
1.121 +*/