utils.cpp File Reference

#include <cassert>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <iconv.h>
#include "utils.h"
#include "log.h"
Include dependency graph for utils.cpp:

Go to the source code of this file.

Functions

int get_free_memory ()
 Get free memory.
unsigned int make_hash_code (const char *buffer, unsigned int len)
 Generate hash code given by a buffe with fixed length.
const char * utf8_strichr (const char *str, int len, gunichar c)
 Finds the leftmost occurrence of the given unicode character in a UTF-8 encoded string, while limiting the search to len bytes. Ignore case.
const char * utf8_strirchr (const char *str, int len, gunichar c)
 Finds the rightmost occurrence of the given unicode character in a UTF-8 encoded string, while limiting the search to len bytes. Ignore case.
const char * utf8_strstr (const char *str, const char *sub, bool case_sensitive)
 Finds the leftmost occurrence of substring in a string.
const char * utf8_strrstr (const char *str, int len, const char *sub, bool case_sensitive)
 Finds the rightmost occurrence of substring in a string.
bool is_whole_word (const char *str, const char *sub, size_t sub_len)
 Check if the sub string is a whole word.
bool get_dir_path (const char *filename, char *dir, int dir_len)
 Get the dir path of 'filename'.
bool get_file_name (const char *filename, char *name, int name_len)
 Get the name of 'filename'.
bool get_ext_name (const char *filename, char *ext, int ext_len)
 Get the extension name from a filename.
bool ucs2utf8 (char *ucs_str, size_t input_len, char *utf_str, size_t output_len)
 Convert the unicode string to utf-8 string.

Variables

static iconv_t conv = (iconv_t) -1

Function Documentation

bool get_dir_path ( const char *  filename,
char *  dir,
int  dir_len 
)

Get the dir path of 'filename'.

Parameters:
filename e.g. /data/wa/sample/rome.jpg
dir dir path e.g. /data/wa/sample
dir_len the max len of 'dir'
Returns:
true has dir, false no dir or error

Definition at line 251 of file utils.cpp.

References LOGPRINTF.

00252 {
00253     bool has_dir = false;
00254 
00255     if (filename && dir)
00256     {
00257        char * ptr = g_path_get_dirname(filename);
00258        if (ptr)
00259        {
00260            has_dir = true;
00261            
00262            g_snprintf(dir, dir_len, "%s", ptr);
00263            g_free(ptr);
00264        }
00265     }
00266 
00267     LOGPRINTF("%s %s %d", filename, dir, has_dir);
00268 
00269     return has_dir;
00270 }

bool get_ext_name ( const char *  filename,
char *  ext,
int  ext_len 
)

Get the extension name from a filename.

Parameters:
filename e.g. /data/wa/sample/rome.jpg, or rome.jpg
ext extension name e.g. jpg
ext_len the max len of 'ext'
Returns:
true has extension, false no extension or error

Definition at line 299 of file utils.cpp.

References get_file_name(), LOGPRINTF, MAX_PATH_LEN, and name.

00300 {
00301     bool has_ext = false;
00302   
00303     if (filename && ext)
00304     {
00305         char name[MAX_PATH_LEN];
00306         
00307         if (get_file_name(filename, name, MAX_PATH_LEN))
00308         {
00309             char * ptr = g_strrstr(name, ".");
00310             if (ptr)
00311             {
00312                 has_ext = true;
00313 
00314                 ptr++;
00315                 g_snprintf(ext, ext_len, "%s", ptr);
00316             }
00317         }
00318     }
00319 
00320     LOGPRINTF("%s %s %d", filename, ext, has_ext);
00321 
00322     return has_ext;
00323 }

Here is the call graph for this function:

bool get_file_name ( const char *  filename,
char *  name,
int  name_len 
)

Get the name of 'filename'.

Parameters:
filename e.g. /data/wa/sample/rome.jpg
name name e.g. rome.jpg
name_len the max len of 'name'
Returns:
true has name, false no name or error

Definition at line 272 of file utils.cpp.

References LOGPRINTF.

Referenced by get_ext_name(), and images::ImagesDocument::get_page_name().

00273 {
00274     bool has_name = false;
00275 
00276     if (filename && name)
00277     {
00278         if (filename[strlen(filename)-1] != G_DIR_SEPARATOR)
00279         {
00280             char * ptr = g_path_get_basename(filename);
00281             if (ptr)
00282             {
00283                 if (strcmp(ptr, G_DIR_SEPARATOR_S)
00284                    && strcmp(ptr, "."))
00285                 {
00286                     has_name = true;
00287                     g_snprintf(name, name_len, "%s", ptr);
00288                 }
00289                 g_free(ptr);
00290             }
00291         }
00292     }
00293 
00294     LOGPRINTF("%s %s %d", filename, name, has_name);
00295 
00296     return has_name;
00297 }

Here is the caller graph for this function:

int get_free_memory (  ) 

Get free memory.

Copyright (C) 2008 iRex Technologies B.V. All rights reserved.

Definition at line 38 of file utils.cpp.

00039 {
00040     /*
00041     struct sysinfo info;
00042     
00043     if (0 == sysinfo(&info))
00044     {
00045         return info.freeram;
00046     }
00047     */
00048     return 0;
00049 }

bool is_whole_word ( const char *  str,
const char *  sub,
size_t  sub_len 
)

Check if the sub string is a whole word.

Parameters:
str The UTF-8 encoded string.
sub The sub string to be checked.
sub_len The length of the sub string.
Returns:
true if sub is a whole word.

Definition at line 210 of file utils.cpp.

Referenced by text::TextModel::search().

00211 {
00212     // Check the input parameters.
00213     if (   !str
00214         || !sub
00215         || sub_len == 0)
00216     {
00217         return false;
00218     }
00219 
00220     // Check the first char before 'sub'.
00221     const char *p = g_utf8_find_prev_char(str, sub);
00222     if (p)
00223     {
00224         gunichar prev_char = g_utf8_get_char(p);
00225         if (   g_unichar_isalpha(prev_char) 
00226             || g_unichar_isdigit(prev_char) 
00227             || prev_char == '_'   )
00228         {
00229             return false;
00230         }
00231     }
00232 
00233     // Check the first char after 'sub'.
00234     const char *n = sub + sub_len;
00235     if (*n)
00236     {
00237         gunichar next_char = g_utf8_get_char_validated(n, -1);
00238         if (   next_char > 0
00239             && (   g_unichar_isalpha(next_char)
00240                 || g_unichar_isdigit(next_char)
00241                 || next_char == '_'             ) )
00242         {
00243             return false;
00244         }
00245     }
00246 
00247     // Otherwise 'sub' is a whole word.
00248     return true;
00249 }

Here is the caller graph for this function:

unsigned int make_hash_code ( const char *  buffer,
unsigned int  len 
)

Generate hash code given by a buffe with fixed length.

Definition at line 53 of file utils.cpp.

Referenced by images::ImagePage::calc_key(), and text::TextView::set_font_size().

00054 {
00055     assert(buffer);
00056     assert(len != 0);
00057 
00058     const char *p = buffer;
00059     unsigned int h = 0;
00060 
00061     while (len--)
00062     {
00063         h = (h << 5) - h + *p;
00064         p++;
00065     }
00066 
00067     return h;
00068 }

Here is the caller graph for this function:

bool ucs2utf8 ( char *  ucs_str,
size_t  input_len,
char *  utf_str,
size_t  output_len 
)

Convert the unicode string to utf-8 string.

Parameters:
ucs_str The input unicode string.
input_len The length of input buffer.
utf_str The output utf-8 string buffer.
output_len The buffer length of output buffer.
Returns:
true if conversion succeed, otherwise false is returned.

Definition at line 326 of file utils.cpp.

References conv.

00327 {
00328     if (conv == (iconv_t) -1)
00329     {
00330         conv = iconv_open("UTF-8", "Unicode");
00331     }
00332 
00333     assert(conv != (iconv_t) -1);
00334 
00335     char* in_buf  = ucs_str;
00336     char* out_buf = utf_str;
00337     size_t in_bytes_left  = input_len;
00338     size_t out_bytes_left = output_len;
00339 
00340 #ifdef WIN32
00341     if (-1 == iconv(conv, const_cast<const char **>(&in_buf), &in_bytes_left, &out_buf, &out_bytes_left))
00342 #else
00343     if ((size_t)-1 == iconv(conv, &in_buf, &in_bytes_left, &out_buf, &out_bytes_left))
00344 #endif
00345     {
00346         return false;
00347     }
00348 
00349     if (in_bytes_left != 0)
00350     {
00351         // Either we encountered an error, or the output buffer is too small.
00352         return false;
00353     }
00354 
00355     // Add '\0' terminator.
00356     *out_buf = 0;
00357     return true;
00358 }

const char* utf8_strichr ( const char *  str,
int  len,
gunichar  c 
)

Finds the leftmost occurrence of the given unicode character in a UTF-8 encoded string, while limiting the search to len bytes. Ignore case.

Definition at line 70 of file utils.cpp.

Referenced by utf8_strstr().

00071 {
00072     if (g_unichar_isalpha(c))
00073     {
00074         // Only alphabetic character is case related.
00075         c = g_unichar_toupper(c);
00076         for (const char* p=str; (len == -1 && *p != 0) || (p-str<len); p = g_utf8_next_char(p))
00077         {
00078             gunichar i = g_utf8_get_char(p);
00079             if (i == c || i-c == 32)
00080             {
00081                 return p;
00082             }
00083         }
00084         return NULL;
00085     }
00086     else
00087     {
00088         return g_utf8_strchr(str, len, c);
00089     }
00090 }

Here is the caller graph for this function:

const char* utf8_strirchr ( const char *  str,
int  len,
gunichar  c 
)

Finds the rightmost occurrence of the given unicode character in a UTF-8 encoded string, while limiting the search to len bytes. Ignore case.

Definition at line 92 of file utils.cpp.

Referenced by utf8_strrstr().

00093 {
00094     if (g_unichar_isalpha(c))
00095     {
00096         // Only alphabetic character is case related.
00097         c = g_unichar_toupper(c);
00098         const char *p = str + len;
00099         while (true)
00100         {
00101             // Try previous character.
00102             p = g_utf8_find_prev_char(str, p);
00103             if (p == NULL)
00104             {
00105                 return NULL;
00106             }
00107             
00108             gunichar i = g_utf8_get_char(p);
00109             if (i == c || i-c == 32)
00110             {
00111                 return p;
00112             }
00113         }
00114     }
00115     else
00116     {
00117         return g_utf8_strrchr(str, len, c);
00118     }
00119 }

Here is the caller graph for this function:

const char* utf8_strrstr ( const char *  str,
int  len,
const char *  sub,
bool  case_sensitive 
)

Finds the rightmost occurrence of substring in a string.

Definition at line 161 of file utils.cpp.

References utf8_strirchr().

Referenced by text::TextModel::search().

00162 {
00163     gunichar first_char = g_utf8_get_char(sub);
00164     size_t sub_len = strlen(sub);
00165 
00166     while (len > 0)
00167     {
00168         const char *p = NULL;
00169         if (case_sensitive)
00170         {
00171             p = g_utf8_strrchr(str, len, first_char);
00172         }
00173         else
00174         {
00175             // Ignore case.
00176             p = utf8_strirchr(str, len, first_char);
00177         }
00178 
00179         if (p == NULL)
00180         {
00181             return NULL;
00182         }
00183 
00184         // The first char of sub string is found.
00185         int ret;
00186         if (case_sensitive)
00187         {
00188             ret = strncmp(p, sub, sub_len);
00189         }
00190         else
00191         {
00192 #ifdef WIN32
00193             ret = _strnicmp(p, sub, sub_len);
00194 #else
00195             ret = strncasecmp(p, sub, sub_len);
00196 #endif
00197         }
00198         
00199         if (ret == 0)
00200         {
00201             // Pattern found
00202             return p;
00203         }
00204         len = static_cast<int>(p - str);
00205     }
00206 
00207     return NULL;
00208 }

Here is the call graph for this function:

Here is the caller graph for this function:

const char* utf8_strstr ( const char *  str,
const char *  sub,
bool  case_sensitive 
)

Finds the leftmost occurrence of substring in a string.

Definition at line 121 of file utils.cpp.

References utf8_strichr().

Referenced by text::TextModel::search().

00122 {
00123     if (case_sensitive)
00124     {
00125         return strstr(str, sub);
00126     }
00127     else
00128     {
00129         // Ignore case.
00130         gunichar first_char = g_utf8_get_char(sub);
00131         size_t sub_len = strlen(sub);
00132 
00133         for (const char *p=str; *p != 0;)
00134         {
00135             const char *find = utf8_strichr(p, -1, first_char);
00136             if (find != NULL)
00137             {
00138 #ifdef WIN32
00139                 if (_strnicmp(find, sub, sub_len) == 0)
00140 #else
00141                 if (strncasecmp(find, sub, sub_len) == 0)
00142 #endif
00143                 {
00144                     return find;
00145                 }
00146                 else
00147                 {
00148                     p = g_utf8_next_char(find);
00149                 }
00150             }
00151             else
00152             {
00153                 return NULL;
00154             }
00155         }
00156 
00157         return NULL;
00158     }
00159 }

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

iconv_t conv = (iconv_t) -1 [static]

Definition at line 325 of file utils.cpp.

Referenced by main(), and ucs2utf8().

Generated by  doxygen 1.6.2-20100208