|
| 1 | +/****************************************************************************** |
| 2 | + Copyright (c) 2017, Ioannis Nompelis |
| 3 | + All rights reserved. |
| 4 | +
|
| 5 | + Redistribution and use in source and binary forms, with or without any |
| 6 | + modification, are permitted provided that the following conditions are met: |
| 7 | + 1. Redistribution of source code must retain the above copyright |
| 8 | + notice, this list of conditions and the following disclaimer. |
| 9 | + 2. Redistribution in binary form must reproduce the above copyright |
| 10 | + notice, this list of conditions and the following disclaimer in the |
| 11 | + documentation and/or other materials provided with the distribution. |
| 12 | + 3. All advertising materials mentioning features or use of this software |
| 13 | + must display the following acknowledgement: |
| 14 | + "This product includes software developed by Ioannis Nompelis." |
| 15 | + 4. Neither the name of Ioannis Nompelis and his partners/affiliates nor the |
| 16 | + names of other contributors may be used to endorse or promote products |
| 17 | + derived from this software without specific prior written permission. |
| 18 | + 5. Redistribution or use of source code and binary forms for profit must |
| 19 | + have written permission of the copyright holder. |
| 20 | + |
| 21 | + THIS SOFTWARE IS PROVIDED BY IOANNIS NOMPELIS ''AS IS'' AND ANY |
| 22 | + EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 23 | + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 | + DISCLAIMED. IN NO EVENT SHALL IOANNIS NOMPELIS BE LIABLE FOR ANY |
| 25 | + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 26 | + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 27 | + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 28 | + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | + ******************************************************************************/ |
| 32 | + |
| 33 | +#include <stdio.h> |
| 34 | +#include <stdlib.h> |
| 35 | +#include <string.h> |
| 36 | + |
| 37 | +// |
| 38 | +// Items have been trimmed from this header file from the original. |
| 39 | +// This is to allow for distribution of a "trimmed" version of my utilities, |
| 40 | +// and in this case only the ones presently relevant to this project. |
| 41 | +// IN 2019/05/23 |
| 42 | +// |
| 43 | + |
| 44 | +#include "utils.h" |
| 45 | +#include "utils_proto.h" |
| 46 | + |
| 47 | + |
| 48 | +/* |
| 49 | + * Function to read a line of data from an ASCII file similar to and intended |
| 50 | + * to replace fgets(). It takes a pointer to a pointer to char and the size that |
| 51 | + * is associated with the memory pointed to by that pointer to which the pointer |
| 52 | + * is pointing... The pointer to a null pointer can be passed. The idea is to |
| 53 | + * keep augmenting the memory as the text lines encountered in the file get |
| 54 | + * larger; the user does not care about providing a large enough buffer. Error |
| 55 | + * trapping is done by checking the return value; a negative value means failure |
| 56 | + * to re-allocate mamory. |
| 57 | + * Ioannis Nompelis <nompelis@nobelware.com> Created: 20170517 |
| 58 | + * Ioannis Nompelis <nompelis@nobelware.com> Last modified: 20170517 |
| 59 | + */ |
| 60 | + |
| 61 | +int inUtils_ReadTextline( size_t *isize, char **buf, FILE *fp ) |
| 62 | +{ |
| 63 | + const size_t iseg_size = 256; // this is arbitrary but a good guess |
| 64 | + const size_t isafe = 1; // this is to allow stupid external queries... |
| 65 | + char *tbuf = NULL; |
| 66 | + size_t i; |
| 67 | + int idone = 0; |
| 68 | + |
| 69 | + |
| 70 | + // check if we have a valid stream open for reading |
| 71 | + if( fp == NULL ) { |
| 72 | + printf("Not a valid pointer to stream\n"); |
| 73 | + return(0); |
| 74 | + } |
| 75 | + |
| 76 | +#ifdef _DEBUG_ |
| 77 | + printf("Buffer incoming at: %p \n", *buf ); |
| 78 | +#endif |
| 79 | + |
| 80 | + // keep reading until an end-of-line is encountered |
| 81 | + // allocate memory as needed |
| 82 | + i = 0; |
| 83 | + while( idone == 0 ) { |
| 84 | + int ic; |
| 85 | + char c; |
| 86 | + |
| 87 | + // allocate memory if there is inadequate space |
| 88 | + if( i == (*isize) ) { |
| 89 | + size_t itmp = (*isize) + iseg_size + isafe; |
| 90 | + tbuf = (char *) realloc( *buf, itmp*sizeof(char) ); |
| 91 | + if( tbuf == NULL ) { |
| 92 | + printf("Could not (re)allocate buffer\n"); |
| 93 | + return(-1); |
| 94 | + } |
| 95 | + // assign buffer pointer |
| 96 | + *buf = tbuf; |
| 97 | + // set temporary pointer to new segment of the buffer |
| 98 | + tbuf = (char *) &((*buf)[ (*isize) ]); |
| 99 | + // clean-up the newly allocated memory |
| 100 | + memset( tbuf, '\0', iseg_size+isafe ); |
| 101 | + // update size of buffer |
| 102 | + *isize += iseg_size; |
| 103 | + |
| 104 | +#ifdef _DEBUG_ |
| 105 | + printf("Buffer allocated (size: %ld) at %p / %p\n", |
| 106 | + (unsigned long) (*isize), *buf, tbuf ); |
| 107 | +#endif |
| 108 | + } |
| 109 | + |
| 110 | + // get a single character |
| 111 | + ic = fgetc( fp ); |
| 112 | + |
| 113 | + // check for end-of-file |
| 114 | + if( ic == EOF ) { |
| 115 | +#ifdef _DEBUG_ |
| 116 | + printf("End-of-file encountered\n"); |
| 117 | +#endif |
| 118 | + (*buf)[i] = '\0'; |
| 119 | + idone = 1; |
| 120 | + } else { |
| 121 | + // put byte in buffer |
| 122 | + c = (char) ic; |
| 123 | + (*buf)[i] = c; |
| 124 | + ++i; |
| 125 | +#ifdef _DEBUG2_ |
| 126 | + printf(" Got \"%c\" (%d) \n", c, (int) c); |
| 127 | +#endif |
| 128 | + } |
| 129 | + |
| 130 | + // terminate on newline character |
| 131 | + if( c == '\n' ) { |
| 132 | + idone = 1; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + // return count of bytes read |
| 137 | + return( (int) i ); |
| 138 | +} |
| 139 | + |
| 140 | + |
| 141 | +/* |
| 142 | + * Function to trim leading spaces from a string that is properly terminated |
| 143 | + * Ioannis Nompelis <nompelis@nobelware.com> Created: 20170517 |
| 144 | + * Ioannis Nompelis <nompelis@nobelware.com> Last modified: 20170517 |
| 145 | + */ |
| 146 | + |
| 147 | +int inUtils_TrimLeadingSpaceTextline( char *buf ) |
| 148 | +{ |
| 149 | + size_t i,ie=0,is=0; |
| 150 | + |
| 151 | + |
| 152 | + if( buf == NULL ) return(1); |
| 153 | + |
| 154 | + while( buf[is] == ' ' ) ++is; |
| 155 | + while( buf[ie] != '\0' ) ++ie; |
| 156 | + |
| 157 | + if( is == 0 ) return(0); |
| 158 | + |
| 159 | + for(i=0;i<ie-is;++i) { |
| 160 | + buf[i] = buf[i+is]; |
| 161 | + buf[i+is] = '\0'; |
| 162 | + } |
| 163 | + |
| 164 | + return(0); |
| 165 | +} |
| 166 | + |
0 commit comments