libarmutils  1.4
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Groups
File Buffer

.h File Buffer Header. More...

Data Structures

struct  FileBuffer
 FileBuffer Structure. More...

Functions

void file_buffer_destroy (FileBuffer *fbuf)
 Free memory used by a FileBuffer structure.
FileBufferfile_buffer_init (void)
 Allocate memory for a FileBuffer structure.
int file_buffer_read (FileBuffer *fbuf, const char *full_path, char **data)
 Read a file into a FileBuffer.
int file_buffer_split_lines (FileBuffer *fbuf, int *nlines, char ***lines)
 Create array of line pointers into a FileBuffer.

Detailed Description

.h File Buffer Header.


Function Documentation

void file_buffer_destroy ( FileBuffer fbuf)

Free memory used by a FileBuffer structure.

Parameters:
fbufpointer to the FileBuffer structure

Definition at line 38 of file file_buffer.c.

FileBuffer* file_buffer_init ( void  )

Allocate memory for a FileBuffer structure.

Error messages from this function are sent to the message handler (see msngr_init_log() and msngr_init_mail()).

Return values:
fbufpointer to the FileBuffer structure
NULLif a memory allocation error occurred

Definition at line 58 of file file_buffer.c.

int file_buffer_read ( FileBuffer fbuf,
const char *  full_path,
char **  data 
)

Read a file into a FileBuffer.

The in memory copy of the file can be accessed via the 'data' member of the FileBuffer structure. This memory is managed by the FileBuffer structure and should not be freed by the calling process. It will be freed when file_buffer_destroy() is called.

Using the same FileBuffer to read additional files will reused the previously allocated memory, reallocating more as necessary.

Example:

const char *full_path = "/full/path/to/file";
int errcode = 0;
FileBuffer *fbuf;
int nlines;
char **lines;
int status;
char *linep;
int li;
if (!(fbuf = file_buffer_init())) {
return(errcode);
}
if (!file_buffer_read(fbuf, full_path, NULL)) {
return(errcode);
}
if (!file_buffer_split_lines(fbuf, &nlines, &lines)) {
return(errcode);
}
for (li = 0; li < nlines; ++li) {
linep = lines[li];
printf("%s\n", linep);
}

Error messages from this function are sent to the message handler (see msngr_init_log() and msngr_init_mail()).

Parameters:
fbufpointer to the FileBuffer structure
full_pathfull path to the input file
dataoutput: fbuf->data if not NULL
Return values:
1if successful (or zero length file)
0if an errror occurred

Definition at line 148 of file file_buffer.c.

int file_buffer_split_lines ( FileBuffer fbuf,
int *  nlines,
char ***  lines 
)

Create array of line pointers into a FileBuffer.

This function will replace newline characters '
' with string terminator characters '\0' and create an array of pointers to each line in the buffer. The output fbuf->lines array will be null terminated.

The memory used by the returned array of line pointers is managed by the FileBuffer structure and should not be freed by the calling process.

The number of lines and line pointers can also be accessed using the 'nlines' and 'lines' members of the FileBuffer structure.

Example:

Error messages from this function are sent to the message handler (see msngr_init_log() and msngr_init_mail()).

Parameters:
fbufpointer to the FileBuffer structure
nlinesoutput: fbuf->nlines if not NULL
linesoutput: fbuf->lines if not NULL
Return values:
1if successful
0if a memory allocation error occurred

Definition at line 281 of file file_buffer.c.