2 * Copyright 2009 The Native Client Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
8 * Some useful formatting tools that allow us to build our own directive
9 * processing simply. The code assumes that directives of the form
10 * %X (for some character x). Further, in a format string '\\' is treated
11 * the backslash character ('\') while '\%' escapes out the interpretation
12 * of '%' as the beginning of a directive.
14 * If the specified %X directive is not understood by the directive processing
15 * function, the directive will not be translated while processing the format
18 * Note: Buffer insertions are based on the routines FormatDataAppend and
19 * FormatAppend. Each of these routines append text to the buffer, based
20 * on the current cursor position. The cursor is the index to add the next
21 * character to the buffer, if the buffer was sufficiently large enough to
22 * hold the appended text. Therefore, after all text has been appended, if
23 * the cursor is smaller than the buffer size, no truncation occurs.
25 * Note: Buffer insertions will automatically add the null terminator after
26 * the appended string, but the cursor is not updated to reflect this change.
27 * If a buffer overflows, the last character in the buffer will be the null
28 * terminator, unless the buffer size is empty. If the buffer size is empty,
29 * there is no room for the null terminator and it is not inserted.
31 * Note: To dynamically compute the amount of space needed to format a string,
32 * call on an empty buffer with size zero, and the cursor initially set to zero.
33 * After the call, the value of the cursor, plus one, is the minimum sized
34 * buffer that will be needed to format that string.
37 #ifndef NATIVE_CLIENT_SRC_SHARED_UTILS_FORMMATTING_H__
38 #define NATIVE_CLIENT_SRC_SHARED_UTILS_FORMMATTING_H__
40 #include "native_client/src/shared/utils/types.h"
43 * Defines the generic format for a fucntion that processses directives
44 * when they are found. Returns true if the directive was processed and
45 * the resulting string was added at the given cursor position. When the buffer
46 * fills, no additional text is added to the buffer, even though the cursor
47 * is incremented accordingly. Otherwise, the buffer is left unchanged,
48 * and the function must return false.
51 * directive - The character X of the found %X directive.
52 * buffer - The buffer to add the resulting text of the directive
53 * buffer_size - The size of the buffer.
54 * data - (Generic) pointer to the data that should be used to process the
56 * cursor - The index into the buffer, where the next character
57 * should be added, if there is sufficient room in the buffer.
59 typedef Bool (*FormatDataUsingDirectiveFcn)(
67 * Defines a driver routine to process a format string and put the rsulting
68 * generated text in at the cursor position. When the buffer fills, no
69 * additional text is added to the buffer, even though the cursor is
70 * incremented accordingly.
72 * Note: Buffer overflow occurs iff the cursor is greater than or
73 * equal to the buffer size.
76 * buffer - The buffer to fill using the format.
77 * buffer_size - The size of the buffer.
78 * format - The format string to use.
79 * data - (Generic) pointer to the data that should be used to process the
80 * directives in the format string.
81 * directive_fcn - The function to process directives as they are found.
82 * cursor - The index into the buffer, where the next character should
83 * be added, if there is sufficient room in the buffer.
85 void FormatDataAppend(char* buffer,
89 FormatDataUsingDirectiveFcn directive_fcn,
93 * Defines a driver routine to process a format string and put the resulting
94 * generated text in the given buffer. Returns true iff buffer overflow doesn't
98 * buffer - The buffer to fill using the format.
99 * buffer_size - The size of the buffer.
100 * format - The format string to use.
101 * data - (Generic) pointer to the data that should be used to process the
102 * directives in the format string.
103 * directive_fcn - The function to process directives as they are found.
105 Bool FormatData(char* buffer,
109 FormatDataUsingDirectiveFcn directive_fcn);
112 * Append the given text at the cursor position. When the buffer fills, no
113 * additional text is added to the buffer, even though the cursor is incremented
116 * Note: Buffer overflow occurs iff the cursor is greater than or
117 * equal to the buffer size.
120 * buffer - The buffer to fill with the text.
121 * buffer_size - The size of the buffer.
122 * text - The text to append.
123 * cursor - The index into the buffer, where the next character
124 * should be added, if there is sufficient room in the buffer.
126 void FormatAppend(char* buffer,
131 #endif /* NATIVE_CLIENT_SRC_SHARED_UTILS_FORMMATTING_H__ */