1 /* AbiSource Program Utilities
2  * Copyright (C) 1998 AbiSource, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA.
18  */
19 
20 #ifndef UTMISC_H
21 #define UTMISC_H
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 /* pre-emptive dismissal; ut_types.h is needed by just about everything,
28  * so even if it's commented out in-file that's still a lot of work for
29  * the preprocessor to do...
30  */
31 #ifndef UT_TYPES_H
32 #include "ut_types.h"
33 #endif
34 
35 #ifndef UTVECTOR_H
36 #include "ut_vector.h"
37 #endif
38 
39 #include <string>
40 
41 class UT_Rect;
42 class UT_String;
43 class UT_UTF8String;
44 class PP_AttrProp;
45 
46 
47 // ----------------------------------------------------------------
48 class ABI_EXPORT UT_Rect
49 {
50 public:
51 	UT_Rect();
52 	UT_Rect(UT_sint32 iLeft, UT_sint32 iTop, UT_sint32 iWidth, UT_sint32 iHeight);
53 	UT_Rect(const UT_Rect &);
54 	UT_Rect(const UT_Rect * r);
55 
56 	bool containsPoint(UT_sint32 x, UT_sint32 y) const;
57 	void set(UT_sint32 iLeft, UT_sint32 iTop, UT_sint32 iWidth, UT_sint32 iHeight);
58 	bool intersectsRect(const UT_Rect * pRect) const;
59 	void unionRect( const UT_Rect *pRect);
60 	UT_sint32	left;
61 	UT_sint32	top;
62 	UT_sint32	width;
63 	UT_sint32	height;
64 };
65 
66 // ----------------------------------------------------------------
67 struct ABI_EXPORT UT_Point
68 {
69 	UT_sint32	x;
70 	UT_sint32	y;
71 };
72 
73 #define UT_MAX(A,B)	(((A) > (B)) ? (A) : (B))
74 #define UT_MIN(A,B)	(((A) < (B)) ? (A) : (B))
75 #define UT_ABS(A)	( ((A) < 0) ? (-(A)) : (A) )
76 
77 std::string UT_pathSuffix(std::string path);
78 bool         UT_addOrReplacePathSuffix(std::string & sPath, const char* sSuffix);
79 
80 bool UT_isWordDelimiter(UT_UCSChar currentChar, UT_UCSChar followChar, UT_UCSChar prevChar);
81 
82 /**
83  * Get the attribute "name" from atts or NULL if no such attribute is in atts
84  */
85 ABI_EXPORT const gchar* UT_getAttribute(const gchar* name,
86                                         const gchar** atts);
87 
88 /**
89  * Like UT_getAttribute(name,atts) but return the def value
90  * if there is no attibute name in atts.
91  */
92 ABI_EXPORT const gchar* UT_getAttribute( const gchar* name,
93                                          const gchar** atts, const gchar* def );
94 
95 
96 
97 /**
98  * s is not null and is not a string that is "0" or false or something
99  * sane that indicates "false".
100  */
101 ABI_EXPORT bool isTrue( const char* s );
102 
103 
104 
105 ABI_EXPORT gchar ** UT_cloneAndDecodeAttributes (const gchar ** attrs);
106 
107 UT_sint32 signedHiWord(UT_uint32 dw);
108 UT_sint32 signedLoWord(UT_uint32 dw);
109 
110 UT_GenericVector<UT_String*> * simpleSplit (const UT_String & str, char separator = ' ',
111 						 size_t max = 0 /* 0 == full split */);
112 
113 UT_uint32 UT_HeadingDepth(const char * szHeadName);
114 
115 /*
116    The purpose of this class is to generate document-unique
117    numerical identifiers; uniqueness is type-specific, i.e., id's of
118    the different types generated by the class can overlap. Most of the
119    time you probably will not want to use this class directly, but use
120    the methods provided in PD_Document class.
121 */
122 
123 #define UT_UID_INVALID 0xffffffff
124 class ABI_EXPORT UT_UniqueId
125 {
126   public:
127 	UT_UniqueId();
~UT_UniqueId()128 	~UT_UniqueId(){};
129 
130 	enum idType
131 	{
132 		List = 0,
133 		Footnote,
134 		Endnote,
135 		Annotation,
136 		HeaderFtr,
137 		Image,
138 		Math,
139 		Embed,
140         ODTCTSplit,
141 
142 		/* must be last; for internal use only !*/
143 		_Last
144 	};
145 
146 	UT_uint32 getUID(idType t);
147 	bool      setMinId(idType t, UT_uint32 iMin);
148 	bool      isIdUnique(idType t, UT_uint32 iId) const;
149 
150   private:
151 	UT_uint32 m_iID[(UT_uint32)_Last];
152 };
153 
154 ABI_EXPORT bool UT_parseBool (const char * param, bool dfl);
155 
156 
157 #if 0
158 /*
159    if your platform does not define timeval, turn on the following definitions
160 */
161 #include <time.h>
162 
163 typedef signed long suseconds_t;
164 
165 struct timeval
166 {
167 	time_t           tv_sec;
168 	suseconds_t      tv_usec;
169 };
170 #endif
171 
172 /*!
173     UT_gettimeofday() fills in the timeval structure with current
174     time; the platform implementation needs to be as accurate as
175     possible since this function is used in the UT_UUID class.
176  */
177 void UT_gettimeofday(struct timeval *tv);
178 
179 typedef unsigned char UT_EthernetAddress[6];
180 /*!
181     retrieve the 6-byte address of the network card; returns true on success
182 */
183 bool UT_getEthernetAddress(UT_EthernetAddress &a);
184 
185 class ABI_EXPORT UT_VersionInfo
186 {
187   public:
UT_VersionInfo(UT_uint32 maj,UT_uint32 min,UT_uint32 mic,UT_uint32 nan)188 	UT_VersionInfo(UT_uint32 maj, UT_uint32 min, UT_uint32 mic, UT_uint32 nan)
189 		: m_iMajor(maj), m_iMinor(min), m_iMicro(mic), m_iNano(nan) {};
190 
UT_VersionInfo()191 	UT_VersionInfo()
192 		: m_iMajor(0), m_iMinor(0), m_iMicro(0), m_iNano(0) {};
193 
getMajor()194 	UT_uint32 getMajor() const {return m_iMajor;}
getMinor()195 	UT_uint32 getMinor() const {return m_iMinor;}
getMicro()196 	UT_uint32 getMicro() const {return m_iMicro;}
getNano()197 	UT_uint32 getNano()  const {return m_iNano;}
198 
set(UT_uint32 maj,UT_uint32 min,UT_uint32 mic,UT_uint32 nan)199 	void set(UT_uint32 maj, UT_uint32 min, UT_uint32 mic, UT_uint32 nan)
200     	{m_iMajor = maj; m_iMinor = min; m_iMicro = mic; m_iNano = nan;}
201 
202 	bool operator > (const UT_VersionInfo &v) const
203 	    {
204 			if(m_iMajor > v.m_iMajor)
205 				return true;
206 			if(m_iMajor < v.m_iMajor)
207 				return false;
208 
209 			if(m_iMinor > v.m_iMinor)
210 				return true;
211 			if(m_iMinor < v.m_iMinor)
212 				return false;
213 
214 			if(m_iMicro > v.m_iMicro)
215 				return true;
216 			if(m_iMicro < v.m_iMicro)
217 				return false;
218 
219 			if(m_iNano > v.m_iNano)
220 				return true;
221 			if(m_iNano < v.m_iNano)
222 				return false;
223 
224 			return false;
225 	    }
226 
227 	const UT_UTF8String & getString() const;
228 
229 
230   private:
231 	UT_uint32 m_iMajor;
232 	UT_uint32 m_iMinor;
233 	UT_uint32 m_iMicro;
234 	UT_uint32 m_iNano;
235 };
236 
237 /*
238    returns a copy of props which has all values (not names!) set to NULL; the caller must delete[] the returned pointer
239    but not the contents
240 
241    this function reuses the property names from the original string, so if those are freed, the copy
242    too becomes invalid !!!
243 */
244 const gchar ** UT_setPropsToNothing(const gchar ** props);
245 
246 const gchar ** UT_setPropsToValue(const gchar ** props, const gchar * value);
247 
248 ABI_EXPORT const gchar ** UT_splitPropsToArray(gchar * props);
249 
250 UT_uint64 UT_hash64(const char * p, UT_uint32 bytelen = 0);
251 UT_uint32 UT_hash32(const char * p, UT_uint32 bytelen = 0);
252 
253 // Hack so we get AbiNativeWidget with an xp include
254 #ifdef TOOLKIT_GTK_ALL
255 #include <gtk/gtk.h>
256 typedef GtkWidget AbiNativeWidget;
257 #else
258 // TODO maintainers please fix their platform
259 typedef void AbiNativeWidget;
260 #endif
261 
262 #endif /* UTMISC_H */
263