1 /*
2  * Copyright 2011 kubtek <kubtek@mail.com>
3  *
4  * This file is part of StarDict.
5  *
6  * StarDict is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * StarDict is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with StarDict.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef UTILS_H
21 #define UTILS_H
22 
23 #include <glib.h>
24 #include <string>
25 #include <vector>
26 #include <cstring>
27 #include <gdk-pixbuf/gdk-pixbuf.h>
28 #ifdef _WIN32
29 #include <windows.h>
30 #endif
31 
32 
33 #if defined(ARM) || defined(__sparc__)
get_uint32(const gchar * addr)34 static inline guint32 get_uint32(const gchar *addr)
35 {
36 	guint32 result;
37 	memcpy(&result, addr, sizeof(guint32));
38 	return result;
39 }
40 #else
41 #define get_uint32(x) *reinterpret_cast<const guint32 *>(x)
42 #endif
43 
44 extern void ProcessGtkEvent();
45 
46 extern std::string combnum2str(gint comb_code);
47 extern std::vector<std::string> split(const std::string& str, char sep);
48 extern GdkPixbuf *load_image_from_file(const std::string& filename);
49 extern char *common_encode_uri_string(const char *string);
50 
51 /* Create a new temporary file. Return file name in file name encoding.
52 Return an empty string if file cannot be created. */
53 std::string create_temp_file(void);
54 /* stardict_mkstemp_full, stardict_mkstemp, stardict_file_open_tmp
55 functions were created after g_mkstemp_full, g_mkstemp, g_file_open_tmp
56 respectively. Implementation is identical to glib functions besides
57 on Windows platform new functions use native Windows API.
58 In particular _wsopen_s is called instead of g_open on Windows.
59 g_open works on Windows, but we cannot close the returned handle properly.
60 The handle must be closed with close() function from GTK+ CRL,
61 but we have no access to it. We only have _close() from MS CRL,
62 it cannot close the handler returned from other CRL.
63 When StarDict is build with Visual Studio we have two copies of CRL:
64 one from Microsoft and one from GTK+ runtime. */
65 gint
66 stardict_mkstemp_full (gchar *tmpl,
67 	int    flags,
68 	int    mode);
69 gint
70 stardict_mkstemp (gchar *tmpl);
71 gint
72 stardict_file_open_tmp (const gchar  *tmpl,
73 				 gchar       **name_used,
74 				 GError      **error);
stardict_datadup(gconstpointer mem)75 inline gchar* stardict_datadup(gconstpointer mem)
76 {
77 	return (gchar *)g_memdup(mem, sizeof(guint32) + *reinterpret_cast<const guint32 *>(mem));
78 }
79 
80 typedef enum {
81 	qtSIMPLE, qtPATTERN, qtFUZZY, qtREGEX, qtFULLTEXT
82 } query_t;
83 
84 extern query_t analyse_query(const char *s, std::string& res);
85 extern void stardict_input_escape(const char *text, std::string &res);
86 
87 
88 void html_decode(const char *str, std::string& decoded);
89 void GetPureEnglishAlpha(char *dst, const char *src); // not used
90 bool IsASCII(const char *str);
91 const char* skip_spaces(const char *str);
92 char* copy_normalize_spaces(char *dst, const char *src);
93 void copy_normalize_trim_spaces(char *dst, const char *src);
94 char* delete_trailing_spaces_ASCII(const char *begin, char *end);
95 char* delete_trailing_word_ASCII(const char *begin, char *end);
96 char* delete_trailing_char(char *begin, char *end);
97 void extract_word(char *dst, const char* src, int BeginPos, gboolean (*is_splitter)(gunichar c));
98 void extract_word_in_place(const char **begin, const char **end,
99 	const char* src, int BeginPos, gboolean (*is_splitter)(gunichar c));
100 void extract_capitalized_word(char *dst, const char* src, int BeginPos,
101 	gboolean (*is_first_letter)(gunichar c), gboolean (*is_second_letter)(gunichar c));
102 void extract_capitalized_word_in_place(const char **begin, const char **end,
103 	const char* src, int BeginPos,
104 	gboolean (*is_first_letter)(gunichar c), gboolean (*is_second_letter)(gunichar c));
105 const char* find_first(const char* src, gboolean (*isfunc)(gunichar c));
106 const char* find_first_not(const char* src, gboolean (*isfunc)(gunichar c));
107 gboolean is_space_or_punct(gunichar c);
108 gboolean is_not_alpha(gunichar c);
109 gboolean is_not_upper(gunichar c);
110 gboolean is_not_lower(gunichar c);
111 
112 #ifdef _WIN32
113 #define bzero(p, l) memset(p, 0, l)
114 #endif
115 
116 typedef unsigned int uint;
117 typedef unsigned short  uint16; /* Short for unsigned integer >= 16 bits */
118 typedef unsigned long   ulong;            /* Short for unsigned long */
119 typedef unsigned long long int ulonglong; /* ulong or unsigned long long */
120 typedef long long int   longlong;
121 typedef char    pchar;          /* Mixed prototypes can take char */
122 typedef char    pbool;          /* Mixed prototypes can take char */
123 typedef unsigned char   uchar;  /* Short for unsigned char */
124 typedef char            my_bool; /* Small bool */
125 
126 #endif/*UTILS_H*/
127