TPCCLIB
Loading...
Searching...
No Matches
readasciifile.c File Reference

Read ASCII text file raw contents for further processing. More...

#include "tpcclibConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tpcextensions.h"

Go to the source code of this file.

Functions

size_t asciiFileSize (FILE *fp, int *nonprintable)
char * asciiFileRead (FILE *fp, char *data, size_t maxlen)
int asciiCommentLine (const char *line, int *cont)

Detailed Description

Read ASCII text file raw contents for further processing.

Definition in file readasciifile.c.

Function Documentation

◆ asciiCommentLine()

int asciiCommentLine ( const char * line,
int * cont )

Check if ASCII text line starts with comment character '#'.

Comment character is searched from the first non-space character (space characters here include spaces and tabs).

Returns
1 if this is comment line and 0 if not.
Author
Vesa Oikonen
See also
asciiFileRead, asciiFileSize, strTrimLeft, strstrNoQuotation
Parameters
linePointer to string containing one line of ASCII text file.
contOptional pointer which is set to the index of line where the first non-space character after the comment character starts. If line does not start with comment character, then this will point to the first non-space character of the line. Enter NULL if not needed.

Definition at line 149 of file readasciifile.c.

156 {
157 if(cont!=NULL) *cont=0;
158 if(line==NULL) return 0;
159 char *cptr=(char*)line;
160 int i=strspn(cptr, " \t"); cptr+=i; if(cont!=NULL) *cont=i;
161 if(*cptr!='#') return 0;
162 if(cont==NULL) return 1;
163 cptr++; i=strspn(cptr, " \t"); *cont+=(i+1);
164 return 1;
165}

Referenced by iftPutFromString(), iftPutFromStringWithSpaceSeparator(), and tacFormatDetermine().

◆ asciiFileRead()

char * asciiFileRead ( FILE * fp,
char * data,
size_t maxlen )

Read at most maxlen-1 characters from given file pointer to ASCII text file.

Author
Vesa Oikonen
Returns
Pointer to the string, or NULL in case of an error.
See also
asciiFileSize, asciiCommentLine, fileExist, filenameGetExtension
Parameters
fpFile pointer; rewind() is NOT called here to allow reading data in chunks.
dataPointer to character array in which the data is written; NULL, if memory is to allocated here (in that case remember to free it after use). Terminal zero is added.
maxlenNr of characters to read, and the size of data array (including terminal zero).

Definition at line 117 of file readasciifile.c.

125 {
126 /* Check the input */
127 if(fp==NULL || maxlen<2) return NULL;
128 /* Allocate memory if needed */
129 char *cptr;
130 if(data!=NULL) cptr=data; else {
131 cptr=malloc(maxlen*sizeof(char)); if(cptr==NULL) return NULL;
132 }
133 size_t i=0; int c;
134 while((c=fgetc(fp))!=EOF && i<maxlen-1) cptr[i++]=(char)c;
135 cptr[i]=(char)0; if(i==0) {if(data==NULL) free(cptr); cptr=NULL;}
136 return cptr;
137}

Referenced by csvRead(), iftRead(), and tacFormatDetermine().

◆ asciiFileSize()

size_t asciiFileSize ( FILE * fp,
int * nonprintable )

Get the size of ASCII text file, or size of initial ASCII part of file containing binary data after the text part (in certain image formats).

One NUL character is not counted as binary data, but two already is; this, because equipment failures sometimes lead to data ending with NUL.

Author
Vesa Oikonen
Returns
The number of characters in the initial ASCII part of file. This does not include the terminal zero that would be added to a string.
See also
asciiFileRead, asciiCommentLine, fileExist
Parameters
fpFile pointer; rewind() is called before returning.
nonprintableValue will be set to 1 if reading was stopped at a non-printable character meaning that file may contain a binary part, or to 0 if file contains only printable characters and size of all contents was counted. Enter NULL, if not needed.

Definition at line 26 of file readasciifile.c.

34 {
35 if(nonprintable!=NULL) *nonprintable=0;
36 if(fp==NULL) return(0);
37 size_t n, bn;
38 int c;
39 n=bn=0;
40 while((c=fgetc(fp))!=EOF && n<SIZE_MAX-1 && bn<2) {
41 if(iscntrl(c) && c!=13 && c!=10 && c!=9 && c!=169) {
42 bn++;
43 if(c!=0) {
44 if(nonprintable!=NULL) *nonprintable=1;
45 break;
46 }
47 }
48 n++;
49 }
50 rewind(fp);
51 if(nonprintable!=NULL && bn>1) *nonprintable=1;
52 return n;
53}

Referenced by csvRead(), iftRead(), tacFormatDetermine(), and tacRead().