|
Packit |
b1f7ae |
/*
|
|
Packit |
b1f7ae |
* Copyright (c) 2013-2017, Intel Corporation
|
|
Packit |
b1f7ae |
*
|
|
Packit |
b1f7ae |
* Redistribution and use in source and binary forms, with or without
|
|
Packit |
b1f7ae |
* modification, are permitted provided that the following conditions are met:
|
|
Packit |
b1f7ae |
*
|
|
Packit |
b1f7ae |
* * Redistributions of source code must retain the above copyright notice,
|
|
Packit |
b1f7ae |
* this list of conditions and the following disclaimer.
|
|
Packit |
b1f7ae |
* * Redistributions in binary form must reproduce the above copyright notice,
|
|
Packit |
b1f7ae |
* this list of conditions and the following disclaimer in the documentation
|
|
Packit |
b1f7ae |
* and/or other materials provided with the distribution.
|
|
Packit |
b1f7ae |
* * Neither the name of Intel Corporation nor the names of its contributors
|
|
Packit |
b1f7ae |
* may be used to endorse or promote products derived from this software
|
|
Packit |
b1f7ae |
* without specific prior written permission.
|
|
Packit |
b1f7ae |
*
|
|
Packit |
b1f7ae |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
Packit |
b1f7ae |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
b1f7ae |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
b1f7ae |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
Packit |
b1f7ae |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
Packit |
b1f7ae |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
Packit |
b1f7ae |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
Packit |
b1f7ae |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
Packit |
b1f7ae |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
Packit |
b1f7ae |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
Packit |
b1f7ae |
* POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
b1f7ae |
*/
|
|
Packit |
b1f7ae |
|
|
Packit |
b1f7ae |
#ifndef FILE_H
|
|
Packit |
b1f7ae |
#define FILE_H
|
|
Packit |
b1f7ae |
|
|
Packit |
b1f7ae |
#include <stddef.h>
|
|
Packit |
b1f7ae |
|
|
Packit |
b1f7ae |
/* Provides linewise access to a string.
|
|
Packit |
b1f7ae |
* Access to the lines is guarded by the text_line function.
|
|
Packit |
b1f7ae |
*/
|
|
Packit |
b1f7ae |
struct text {
|
|
Packit |
b1f7ae |
/* Number of lines. */
|
|
Packit |
b1f7ae |
size_t n;
|
|
Packit |
b1f7ae |
|
|
Packit |
b1f7ae |
/* Each line[0] to line[n-1] points to the start of the
|
|
Packit |
b1f7ae |
* corresponding line.
|
|
Packit |
b1f7ae |
*/
|
|
Packit |
b1f7ae |
char **line;
|
|
Packit |
b1f7ae |
};
|
|
Packit |
b1f7ae |
|
|
Packit |
b1f7ae |
/* Allocates new text.
|
|
Packit |
b1f7ae |
*
|
|
Packit |
b1f7ae |
* Note, if s is NULL or the empty string the text has zero lines.
|
|
Packit |
b1f7ae |
*
|
|
Packit |
b1f7ae |
* Returns a non-NULL text object on success; NULL otherwise.
|
|
Packit |
b1f7ae |
*/
|
|
Packit |
b1f7ae |
extern struct text *text_alloc(const char *s);
|
|
Packit |
b1f7ae |
|
|
Packit |
b1f7ae |
/* Deallocates @t.
|
|
Packit |
b1f7ae |
* If @t is the NULL pointer, nothing happens.
|
|
Packit |
b1f7ae |
*/
|
|
Packit |
b1f7ae |
extern void text_free(struct text *t);
|
|
Packit |
b1f7ae |
|
|
Packit |
b1f7ae |
/* Initializes @t with @s. All "\n" or "\r\n" lineendings, will be
|
|
Packit |
b1f7ae |
* replaced with '\0'.
|
|
Packit |
b1f7ae |
*
|
|
Packit |
b1f7ae |
* Returns 0 on success; a negative enum errcode otherwise.
|
|
Packit |
b1f7ae |
* Returns -err_internal if @t is the NULL pointer.
|
|
Packit |
b1f7ae |
*/
|
|
Packit |
b1f7ae |
extern int text_parse(struct text *t, const char *s);
|
|
Packit |
b1f7ae |
|
|
Packit |
b1f7ae |
/* Copies at most @destlen characters of line @n from text @t to @dest.
|
|
Packit |
b1f7ae |
* The line counts start with 0.
|
|
Packit |
b1f7ae |
* If @dest is the NULL pointer just the line number is checked.
|
|
Packit |
b1f7ae |
*
|
|
Packit |
b1f7ae |
* Returns 0 on success; a negative enum errcode otherwise.
|
|
Packit |
b1f7ae |
* Returns -err_internal if @t is the NULL pointer or if @dest is the
|
|
Packit |
b1f7ae |
* NULL pointer, but @destlen is non-zero.
|
|
Packit |
b1f7ae |
* Returns -err_out_of_range if @n is not in the range.
|
|
Packit |
b1f7ae |
*
|
|
Packit |
b1f7ae |
* Note, the string is always null byte terminated on success.
|
|
Packit |
b1f7ae |
*/
|
|
Packit |
b1f7ae |
extern int text_line(const struct text *t, char *dest, size_t destlen,
|
|
Packit |
b1f7ae |
size_t n);
|
|
Packit |
b1f7ae |
|
|
Packit |
b1f7ae |
/* Provides access to lines of files. Access to all files is cached
|
|
Packit |
b1f7ae |
* after the first request.
|
|
Packit |
b1f7ae |
*
|
|
Packit |
b1f7ae |
* By convention, the first file_list element in the list is the head
|
|
Packit |
b1f7ae |
* and stores no file information.
|
|
Packit |
b1f7ae |
*/
|
|
Packit |
b1f7ae |
struct file_list {
|
|
Packit |
b1f7ae |
/* Name of the file. */
|
|
Packit |
b1f7ae |
char *filename;
|
|
Packit |
b1f7ae |
|
|
Packit |
b1f7ae |
/* The content of the file. */
|
|
Packit |
b1f7ae |
struct text *text;
|
|
Packit |
b1f7ae |
|
|
Packit |
b1f7ae |
/* Points to the next file list entry. It's NULL if the
|
|
Packit |
b1f7ae |
* current file_list is the last entry in the list.
|
|
Packit |
b1f7ae |
*/
|
|
Packit |
b1f7ae |
struct file_list *next;
|
|
Packit |
b1f7ae |
};
|
|
Packit |
b1f7ae |
|
|
Packit |
b1f7ae |
/* Allocates a new file list.
|
|
Packit |
b1f7ae |
*
|
|
Packit |
b1f7ae |
* Returns a non-NULL file list object on succes; NULL otherwise.
|
|
Packit |
b1f7ae |
*/
|
|
Packit |
b1f7ae |
extern struct file_list *fl_alloc(void);
|
|
Packit |
b1f7ae |
|
|
Packit |
b1f7ae |
/* Deallocates @fl.
|
|
Packit |
b1f7ae |
* If @fl is the NULL pointer, nothing happens.
|
|
Packit |
b1f7ae |
*/
|
|
Packit |
b1f7ae |
extern void fl_free(struct file_list *fl);
|
|
Packit |
b1f7ae |
|
|
Packit |
b1f7ae |
/* Looks up line @n in a file @filename. The line content is stored in
|
|
Packit |
b1f7ae |
* @dest, which should have a capacity of @destlen.
|
|
Packit |
b1f7ae |
* If @dest is the NULL pointer just the line number is checked.
|
|
Packit |
b1f7ae |
* See function text_line how the line is copied to @dest.
|
|
Packit |
b1f7ae |
* The file @filename is loaded implicitly.
|
|
Packit |
b1f7ae |
*
|
|
Packit |
b1f7ae |
* Returns 0 on success; a negative enum errcode otherwise.
|
|
Packit |
b1f7ae |
* Returns -err_internal if @fl or @filename is the NULL pointer or if
|
|
Packit |
b1f7ae |
* @dest is the NULL pointer, but @destlen is non-zero.
|
|
Packit |
b1f7ae |
* Returns -err_out_of_range if n is not a valid line number.
|
|
Packit |
b1f7ae |
* Returns -err_file_stat if @filename could not be found.
|
|
Packit |
b1f7ae |
* Returns -err_file_open if @filename could not be opened.
|
|
Packit |
b1f7ae |
* Returns -err_file_read if the content of @filename could not be fully
|
|
Packit |
b1f7ae |
* read.
|
|
Packit |
b1f7ae |
*/
|
|
Packit |
b1f7ae |
extern int fl_getline(struct file_list *fl, char *dest, size_t destlen,
|
|
Packit |
b1f7ae |
const char *filename, size_t n);
|
|
Packit |
b1f7ae |
|
|
Packit |
b1f7ae |
/* Looks up the text for @filename and stores its contents in @t.
|
|
Packit |
b1f7ae |
* The file @filename is loaded implicitly.
|
|
Packit |
b1f7ae |
*
|
|
Packit |
b1f7ae |
* Returns 0 on success; a negative enum errcode otherwise.
|
|
Packit |
b1f7ae |
* Returns -err_internal if @fl or @t or @filename is the NULL pointer.
|
|
Packit |
b1f7ae |
* Returns -err_file_stat if @filename could not be found.
|
|
Packit |
b1f7ae |
* Returns -err_file_open if @filename could not be opened.
|
|
Packit |
b1f7ae |
* Returns -err_file_read if the content of @filename could not be fully
|
|
Packit |
b1f7ae |
* read.
|
|
Packit |
b1f7ae |
*/
|
|
Packit |
b1f7ae |
extern int fl_gettext(struct file_list *fl, const struct text **t,
|
|
Packit |
b1f7ae |
const char *filename);
|
|
Packit |
b1f7ae |
|
|
Packit |
b1f7ae |
#endif /* FILE_H */
|