1 /* 2 * Elementary input utilities. 3 * 4 * Copyright (C) 2022, 2024 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <stdio.h> 23 #include <string.h> 24 25 #include "input.h" 26 27 28 29 /* Read a line from a file into the given buffer, removing any newline. */ 30 31 char *read_line(FILE *fp, char *buffer, size_t bufsize) 32 { 33 char *last; 34 35 if (fgets(buffer, bufsize, fp) == NULL) 36 return NULL; 37 38 last = buffer + strlen(buffer) - 1; 39 40 if ((last >= buffer) && (*last == '\n')) 41 *last = '\0'; 42 43 return buffer; 44 } 45 46 /* Parse the text in the given region, returning details of arguments. */ 47 48 void parse_line(char *start, char *end, int *num_args, char *args[], const int max_args) 49 { 50 *num_args = 0; 51 52 while ((start != NULL) && (start < end) && (*num_args < max_args)) 53 { 54 args[*num_args] = start; 55 (*num_args)++; 56 57 /* NOTE: Only handling spaces as delimiters. */ 58 59 start = strchr(start, ' '); 60 61 if (start != NULL) 62 { 63 *start = '\0'; 64 65 if (start < end) 66 start++; 67 } 68 } 69 } 70 71 /* vim: tabstop=4 expandtab shiftwidth=4 72 */