File: file.h

package info (click to toggle)
intel-processor-trace 2.1.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,376 kB
  • sloc: ansic: 41,262; sh: 747; cpp: 36; makefile: 9
file content (147 lines) | stat: -rw-r--r-- 5,436 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Copyright (c) 2013-2025, Intel Corporation
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  * Neither the name of Intel Corporation nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef FILE_H
#define FILE_H

#include <stddef.h>

/* Provides linewise access to a string.
 * Access to the lines is guarded by the text_line function.
 */
struct text {
	/* Number of lines.  */
	size_t n;

	/* Each line[0] to line[n-1] points to the start of the
	 * corresponding line.
	 */
	char **line;
};

/* Turns @s into a new text; @n is the length of @s excluding termination.
 *
 * On success, the returned text owns @s and @s will be freed by text_free();
 * otherwise @s will be freed by text_alloc() and NULL will be returned.
 *
 * Note, if s is NULL or the empty string the text has zero lines.
 *
 * Returns a non-NULL text object on success; NULL otherwise.
 */
extern struct text *text_alloc(char *s, size_t n);

/* Deallocates @t.
 * If @t is the NULL pointer, nothing happens.
 */
extern void text_free(struct text *t);

/* Initializes @t with @s.  All "\n" or "\r\n" lineendings, will be
 * replaced with '\0'.
 *
 * Returns 0 on success; a negative enum errcode otherwise.
 * Returns -err_internal if @t is the NULL pointer.
 */
extern int text_parse(struct text *t, const char *s);

/* Copies at most @destlen characters of line @n from text @t to @dest.
 * The line counts start with 0.
 * If @dest is the NULL pointer just the line number is checked.
 *
 * Returns 0 on success; a negative enum errcode otherwise.
 * Returns -err_internal if @t is the NULL pointer or if @dest is the
 * NULL pointer, but @destlen is non-zero.
 * Returns -err_out_of_range if @n is not in the range.
 *
 * Note, the string is always null byte terminated on success.
 */
extern int text_line(const struct text *t, char *dest, size_t destlen,
		     size_t n);

/* Provides access to lines of files.  Access to all files is cached
 * after the first request.
 *
 * By convention, the first file_list element in the list is the head
 * and stores no file information.
 */
struct file_list {
	/* Name of the file.  */
	char *filename;

	/* The content of the file.  */
	struct text *text;

	/* Points to the next file list entry.  It's NULL if the
	 * current file_list is the last entry in the list.
	 */
	struct file_list *next;
};

/* Allocates a new file list.
 *
 * Returns a non-NULL file list object on succes; NULL otherwise.
 */
extern struct file_list *fl_alloc(void);

/* Deallocates @fl.
 * If @fl is the NULL pointer, nothing happens.
 */
extern void fl_free(struct file_list *fl);

/* Looks up line @n in a file @filename.  The line content is stored in
 * @dest, which should have a capacity of @destlen.
 * If @dest is the NULL pointer just the line number is checked.
 * See function text_line how the line is copied to @dest.
 * The file @filename is loaded implicitly.
 *
 * Returns 0 on success; a negative enum errcode otherwise.
 * Returns -err_internal if @fl or @filename is the NULL pointer or if
 * @dest is the NULL pointer, but @destlen is non-zero.
 * Returns -err_out_of_range if n is not a valid line number.
 * Returns -err_file_stat if @filename could not be found.
 * Returns -err_file_open if @filename could not be opened.
 * Returns -err_file_read if the content of @filename could not be fully
 * read.
 */
extern int fl_getline(struct file_list *fl, char *dest, size_t destlen,
		      const char *filename, size_t n);

/* Looks up the text for @filename and stores its contents in @t.
 * The file @filename is loaded implicitly.
 *
 * Returns 0 on success; a negative enum errcode otherwise.
 * Returns -err_internal if @fl or @t or @filename is the NULL pointer.
 * Returns -err_file_stat if @filename could not be found.
 * Returns -err_file_open if @filename could not be opened.
 * Returns -err_file_read if the content of @filename could not be fully
 * read.
 */
extern int fl_gettext(struct file_list *fl, const struct text **t,
		      const char *filename);

#endif /* FILE_H */