1 : //========================================================================
2 : //
3 : // GooString.h
4 : //
5 : // Simple variable-length string type.
6 : //
7 : // Copyright 1996-2003 Glyph & Cog, LLC
8 : //
9 : //========================================================================
10 :
11 : //========================================================================
12 : //
13 : // Modified under the Poppler project - http://poppler.freedesktop.org
14 : //
15 : // All changes made under the Poppler project to this file are licensed
16 : // under GPL version 2 or later
17 : //
18 : // Copyright (C) 2006 Kristian Høgsberg <krh@redhat.com>
19 : // Copyright (C) 2006 Krzysztof Kowalczyk <kkowalczyk@gmail.com>
20 : // Copyright (C) 2008-2010, 2012 Albert Astals Cid <aacid@kde.org>
21 : // Copyright (C) 2012 Fabio D'Urso <fabiodurso@hotmail.it>
22 : //
23 : // To see a description of the changes please see the Changelog file that
24 : // came with your tarball or type make ChangeLog if you are building from git
25 : //
26 : //========================================================================
27 :
28 : #ifndef GooString_H
29 : #define GooString_H
30 :
31 : #ifdef USE_GCC_PRAGMAS
32 : #pragma interface
33 : #endif
34 :
35 : #include <limits.h> // for LLONG_MAX and ULLONG_MAX
36 : #include <stdarg.h>
37 : #include <stdlib.h> // for NULL
38 : #include "gtypes.h"
39 :
40 : class GooString {
41 : public:
42 :
43 : // Create an empty string.
44 : GooString();
45 :
46 : // Create a string from a C string.
47 : explicit GooString(const char *sA);
48 :
49 : // Create a string from <lengthA> chars at <sA>. This string
50 : // can contain null characters.
51 : GooString(const char *sA, int lengthA);
52 :
53 : // Create a string from <lengthA> chars at <idx> in <str>.
54 : GooString(GooString *str, int idx, int lengthA);
55 :
56 : // Set content of a string to concatination of <s1> and <s2>. They can both
57 : // be NULL. if <s1Len> or <s2Len> is CALC_STRING_LEN, then length of the string
58 : // will be calculated with strlen(). Otherwise we assume they are a valid
59 : // length of string (or its substring)
60 : GooString* Set(const char *s1, int s1Len=CALC_STRING_LEN, const char *s2=NULL, int s2Len=CALC_STRING_LEN);
61 :
62 : // Copy a string.
63 : explicit GooString(const GooString *str);
64 : GooString *copy() const { return new GooString(this); }
65 :
66 : // Concatenate two strings.
67 : GooString(GooString *str1, GooString *str2);
68 :
69 : // Convert an integer to a string.
70 : static GooString *fromInt(int x);
71 :
72 : // Create a formatted string. Similar to printf, but without the
73 : // string overflow issues. Formatting elements consist of:
74 : // {<arg>:[<width>][.<precision>]<type>}
75 : // where:
76 : // - <arg> is the argument number (arg 0 is the first argument
77 : // following the format string) -- NB: args must be first used in
78 : // order; they can be reused in any order
79 : // - <width> is the field width -- negative to reverse the alignment;
80 : // starting with a leading zero to zero-fill (for integers)
81 : // - <precision> is the number of digits to the right of the decimal
82 : // point (for floating point numbers)
83 : // - <type> is one of:
84 : // d, x, X, o, b -- int in decimal, lowercase hex, uppercase hex, octal, binary
85 : // ud, ux, uX, uo, ub -- unsigned int
86 : // ld, lx, lX, lo, lb, uld, ulx, ulX, ulo, ulb -- long, unsigned long
87 : // lld, llx, llX, llo, llb, ulld, ullx, ullX, ullo, ullb
88 : // -- long long, unsigned long long
89 : // f, g -- double
90 : // c -- char
91 : // s -- string (char *)
92 : // t -- GooString *
93 : // w -- blank space; arg determines width
94 : // To get literal curly braces, use {{ or }}.
95 : static GooString *format(const char *fmt, ...);
96 : static GooString *formatv(const char *fmt, va_list argList);
97 :
98 : // Destructor.
99 : ~GooString();
100 :
101 : // Get length.
102 4500 : int getLength() { return length; }
103 :
104 : // Get C string.
105 7909 : char *getCString() const { return s; }
106 :
107 : // Get <i>th character.
108 : char getChar(int i) { return s[i]; }
109 :
110 : // Change <i>th character.
111 : void setChar(int i, char c) { s[i] = c; }
112 :
113 : // Clear string to zero length.
114 : GooString *clear();
115 :
116 : // Append a character or string.
117 : GooString *append(char c);
118 : GooString *append(GooString *str);
119 : GooString *append(const char *str, int lengthA=CALC_STRING_LEN);
120 :
121 : // Append a formatted string.
122 : GooString *appendf(const char *fmt, ...);
123 : GooString *appendfv(const char *fmt, va_list argList);
124 :
125 : // Insert a character or string.
126 : GooString *insert(int i, char c);
127 : GooString *insert(int i, GooString *str);
128 : GooString *insert(int i, const char *str, int lengthA=CALC_STRING_LEN);
129 :
130 : // Delete a character or range of characters.
131 : GooString *del(int i, int n = 1);
132 :
133 : // Convert string to all-upper/all-lower case.
134 : GooString *upperCase();
135 : GooString *lowerCase();
136 :
137 : // Compare two strings: -1:< 0:= +1:>
138 : int cmp(GooString *str) const;
139 : int cmpN(GooString *str, int n) const;
140 : int cmp(const char *sA) const;
141 : int cmpN(const char *sA, int n) const;
142 :
143 : GBool hasUnicodeMarker(void);
144 :
145 : // Sanitizes the string so that it does
146 : // not contain any ( ) < > [ ] { } / %
147 : // The postscript mode also has some more strict checks
148 : // The caller owns the return value
149 : GooString *sanitizedName(GBool psmode);
150 :
151 : private:
152 : GooString(const GooString &other);
153 : GooString& operator=(const GooString &other);
154 :
155 : // you can tweak this number for a different speed/memory usage tradeoffs.
156 : // In libc malloc() rounding is 16 so it's best to choose a value that
157 : // results in sizeof(GooString) be a multiple of 16.
158 : // 24 makes sizeof(GooString) to be 32.
159 : static const int STR_STATIC_SIZE = 24;
160 : // a special value telling that the length of the string is not given
161 : // so it must be calculated from the strings
162 : static const int CALC_STRING_LEN = -1;
163 :
164 : int roundedSize(int len);
165 :
166 : char sStatic[STR_STATIC_SIZE];
167 : int length;
168 : char *s;
169 :
170 : void resize(int newLength);
171 : #ifdef LLONG_MAX
172 : static void formatInt(long long x, char *buf, int bufSize,
173 : GBool zeroFill, int width, int base,
174 : char **p, int *len, GBool upperCase = gFalse);
175 : #else
176 : static void formatInt(long x, char *buf, int bufSize,
177 : GBool zeroFill, int width, int base,
178 : char **p, int *len, GBool upperCase = gFalse);
179 : #endif
180 : #ifdef ULLONG_MAX
181 : static void formatUInt(unsigned long long x, char *buf, int bufSize,
182 : GBool zeroFill, int width, int base,
183 : char **p, int *len, GBool upperCase = gFalse);
184 : #else
185 : static void formatUInt(Gulong x, char *buf, int bufSize,
186 : GBool zeroFill, int width, int base,
187 : char **p, int *len, GBool upperCase = gFalse);
188 : #endif
189 : static void formatDouble(double x, char *buf, int bufSize, int prec,
190 : GBool trim, char **p, int *len);
191 : static void formatDoubleSmallAware(double x, char *buf, int bufSize, int prec,
192 : GBool trim, char **p, int *len);
193 : };
194 :
195 : #endif
|