navit  0.5.3-trunk
util.c File Reference
#include <stdlib.h>
#include <glib.h>
#include <ctype.h>
#include <stdarg.h>
#include <time.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include "util.h"
#include "debug.h"
#include "config.h"

Data Structures

struct  spawn_process_info
 

Enumerations

enum  parse_state { parse_state_whitespace , parse_state_numeric , parse_state_alpha }
 Parser states for parse_for_systematic_comparison(). More...
 

Functions

void strtoupper (char *dest, const char *src)
 
void strtolower (char *dest, const char *src)
 
unsigned int uint_sqrt (unsigned int n)
 Fast compute of square root for unsigned ints. More...
 
int navit_utf8_strcasecmp (const char *s1, const char *s2)
 
static void strtrim (char *s)
 Trims all leading and trailing whitespace characters from a string. More...
 
char * str_escape (enum escape_mode mode, const char *in)
 Escape special characters from a string. More...
 
char * strncpy_unescape (char *dest, const char *src, size_t n)
 Copy a string from src to dest, unescaping characters. More...
 
static char * parse_for_systematic_comparison (const char *s)
 Parses a string for systematic comparison. More...
 
int compare_name_systematic (const char *s1, const char *s2)
 Compares two name_systematic strings. More...
 
static void hash_callback (gpointer key, gpointer value, gpointer user_data)
 
GListg_hash_to_list (GHashTable *h)
 
static void hash_callback_key (gpointer key, gpointer value, gpointer user_data)
 
GListg_hash_to_list_keys (GHashTable *h)
 
gchar * g_strconcat_printf (gchar *buffer, gchar *fmt,...)
 Appends a formatted string and appends it to an existing one. More...
 
int g_utf8_strlen_force_link (gchar *buffer, int max)
 
void square_shape_str (char *s)
 Optimizes the format of a string, adding carriage returns so that when displayed, the result text zone is roughly as wide as high. More...
 
unsigned int iso8601_to_secs (char *iso8601)
 Converts an ISO 8601-style time string into epoch time. More...
 
time_t mkgmtime (struct tm *pt)
 Converts a tm structure to time_t More...
 
time_t iso8601_to_time (char *iso8601)
 Converts an ISO 8601-style time string into time_t. More...
 
char * time_to_iso8601 (time_t time)
 Converts time to ISO8601 format. More...
 
char * current_to_iso8601 (void)
 Outputs local system time in ISO 8601 format. More...
 
char * shell_escape (char *arg)
 
static char * spawn_process_compose_cmdline (char **argv)
 
struct spawn_process_infospawn_process (char **argv)
 
int spawn_process_check_status (struct spawn_process_info *pi, int block)
 
void spawn_process_info_free (struct spawn_process_info *pi)
 
void spawn_process_init ()
 
void get_compass_direction (char *buffer, int angle, int mode)
 Get printable compass direction from an angle. More...
 

Enumeration Type Documentation

◆ parse_state

Parser states for parse_for_systematic_comparison().

Enumerator
parse_state_whitespace 
parse_state_numeric 
parse_state_alpha 

Function Documentation

◆ compare_name_systematic()

int compare_name_systematic ( const char *  s1,
const char *  s2 
)

Compares two name_systematic strings.

A name_systematic string is typically used for road reference numbers (A 4, I-51, SP526). This function performs a fuzzy comparison: Each string is broken down into numeric and non-numeric parts. Then both strings are compared part by part. The following rules apply:

  • Semicolons denote sequences of strings, and the best match between any pair of strings from s1 and s2 is returned.
  • Whitespace bordering on a number is discarded.
  • Whitespace surrounded by string characters is treated as one string with the surrounding characters.
  • If one string has more parts than the other, the shorter string is padded with null parts.
  • null equals null.
  • null does not equal non-null.
  • Numeric parts are compared as integers, hence ‘'042’equals'42'`.
  • Comparison of string parts is case-insensitive.

Partial matches are currently determined by determining each part of one string with each part of the other. Each part of one string that is matched by at least one part of the other increases the score. Order is currently not taken into account, i.e. ‘'42A’and'A-42A'are both considered full (not partial) matches for'A42'`. Future versions may change this.

Parameters
s1The first string
s2The second string
Returns
0 if both strings match, nonzero if they do not. MAX_MISMATCH indicates a complete mismatch; values in between indicate partial matches (lower values correspond to better matches).

References dbg, elements, lvl_debug, MAX_MISMATCH, parse_for_systematic_comparison(), and s1.

Referenced by traffic_location_match_attributes(), traffic_point_match_attributes(), and traffic_point_match_segment_attributes().

◆ current_to_iso8601()

char* current_to_iso8601 ( void  )

Outputs local system time in ISO 8601 format.

Returns
Time in ISO 8601 format

References time_to_iso8601().

Referenced by main(), vehicle_demo_position_attr_get(), and vehicle_log_gpx().

◆ g_hash_to_list()

GList* g_hash_to_list ( GHashTable *  h)

References hash_callback().

Referenced by graphics_free().

◆ g_hash_to_list_keys()

GList* g_hash_to_list_keys ( GHashTable *  h)

References hash_callback_key().

◆ g_strconcat_printf()

gchar* g_strconcat_printf ( gchar *  buffer,
gchar *  fmt,
  ... 
)

Appends a formatted string and appends it to an existing one.

Usage is similar to the familiar C functions that take a format string and a variable argument list.

Return value is a concatenation of buffer (unless it is NULL) and fmt, with the remaining arguments inserted into fmt.

Parameters
bufferAn existing string, can be null and will be freed by this function
fmtA format string (will not be altered)
Returns
A newly allocated string, see description. The caller is responsible for freeing the returned string.

Referenced by attr_to_text_ext(), flags_to_text(), generate_navitintrospectxml(), gui_internal_append_attr(), gui_internal_cmd_img(), gui_internal_onclick(), request_navit_route_export_geojson(), traffic_traff_android_set_selection(), and vehicle_log_gpx().

◆ g_utf8_strlen_force_link()

int g_utf8_strlen_force_link ( gchar *  buffer,
int  max 
)

References max.

◆ get_compass_direction()

void get_compass_direction ( char *  buffer,
int  angle,
int  mode 
)

Get printable compass direction from an angle.

This function supports three different modes:

In mode 0, the angle in degrees is output as a string.

In mode 1, the angle is output as a cardinal direction (N, SE etc.).

In mode 2, the angle is output in analog clock notation (6 o'clock).

Parameters
bufferBuffer to hold the result string (up to 5 characters, including the terminating null character, may be required)
angleThe angle to convert
modeThe conversion mode, see description

Referenced by gui_internal_cmd_pois_item(), and model_poi().

◆ hash_callback()

static void hash_callback ( gpointer  key,
gpointer  value,
gpointer  user_data 
)
static

References value.

Referenced by g_hash_to_list().

◆ hash_callback_key()

static void hash_callback_key ( gpointer  key,
gpointer  value,
gpointer  user_data 
)
static

References key.

Referenced by g_hash_to_list_keys().

◆ iso8601_to_secs()

unsigned int iso8601_to_secs ( char *  iso8601)

Converts an ISO 8601-style time string into epoch time.

Parameters
iso8601Time in ISO 8601 format.
Returns
The number of seconds elapsed since January 1, 1970, 00:00:00 UTC.

References startup::pos.

Referenced by navit_layout_switch(), osd_text_format_attr(), and tracking_update().

◆ iso8601_to_time()

time_t iso8601_to_time ( char *  iso8601)

Converts an ISO 8601-style time string into time_t.

References dbg, lvl_debug, mkgmtime(), and startup::pos.

Referenced by time_new().

◆ mkgmtime()

time_t mkgmtime ( struct tm *  pt)

Converts a tm structure to time_t

Returns the value of type time_t that represents the UTC time described by the tm structure pointed to by pt (which may be modified).

This function performs the reverse translation that gmtime() does. As this functionality is absent in the standard library, it is emulated by calling mktime(), converting its output into both GMT and local time, comparing the results and calling mktime() again with an input adjusted for the offset in the opposite direction. This ensures maximum portability.

The values of the tm_wday and tm_yday members of pt are ignored, and the values of the other members are interpreted even if out of their valid ranges (see struct tm). For example, tm_mday may contain values above 31, which are interpreted accordingly as the days that follow the last day of the selected month.

A call to this function automatically adjusts the values of the members of pt if they are off-range or—in the case of tm_wday and tm_yday—if their values are inconsistent with the other members.

References dbg, and lvl_debug.

Referenced by iso8601_to_time().

◆ navit_utf8_strcasecmp()

int navit_utf8_strcasecmp ( const char *  s1,
const char *  s2 
)

References dbg, lvl_debug, and s1.

Referenced by contains_suffix(), and search_list_get_result().

◆ parse_for_systematic_comparison()

static char* parse_for_systematic_comparison ( const char *  s)
static

Parses a string for systematic comparison.

This is a helper function for compare_name_systematic().

The string is broken down into numeric and non-numeric parts. Whitespace characters are discarded unless they are surrounded by string characters, in which case the whole unit is treated as one string part. All strings are converted to lowercase and leading zeroes stripped from numbers.

Parameters
sThe string to parse
Returns
A buffer containing the parsed string, parts delimited by a null character, the last part followed by a double null character.

References c, dbg, lvl_debug, parse_state_alpha, parse_state_numeric, parse_state_whitespace, part, and strtrim().

Referenced by compare_name_systematic().

◆ shell_escape()

char* shell_escape ( char *  arg)

Escape and quote string for shell

Parameters
inarg string to escape
Returns
escaped string

Referenced by spawn_process_compose_cmdline().

◆ spawn_process()

struct spawn_process_info* spawn_process ( char **  argv)

Call external program

Parameters
inargv NULL terminated list of parameters, zeroeth argument is program name
Returns
0 - success, >0 - return code, -1 - error

References argv, dbg, exit, lvl_debug, lvl_error, spawn_process_info::pid, spawn_process_compose_cmdline(), and spawn_process_info::status.

Referenced by speechd_say().

◆ spawn_process_check_status()

int spawn_process_check_status ( struct spawn_process_info pi,
int  block 
)

Check external program status

Parameters
in*pi pointer to spawn_process_info structure
inblock =0 do not block =1 block until child terminated
Returns
-1 - still running, >=0 program exited, =255 trminated abnormally or wasn't run at all.

References dbg, lvl_debug, lvl_error, spawn_process_info::pid, and spawn_process_info::status.

Referenced by speechd_say().

◆ spawn_process_compose_cmdline()

static char* spawn_process_compose_cmdline ( char **  argv)
static

References argv, and shell_escape().

Referenced by spawn_process().

◆ spawn_process_info_free()

void spawn_process_info_free ( struct spawn_process_info pi)

Referenced by speechd_destroy(), and speechd_say().

◆ spawn_process_init()

void spawn_process_init ( void  )

Referenced by main_init().

◆ square_shape_str()

void square_shape_str ( char *  s)

Optimizes the format of a string, adding carriage returns so that when displayed, the result text zone is roughly as wide as high.

Parameters
[in,out]sThe string to proces (will be modified by this function, but length will be unchanged)

References c, dbg, lvl_debug, and uint_sqrt().

Referenced by navit_populate_search_results_map().

◆ str_escape()

char* str_escape ( enum escape_mode  mode,
const char *  in 
)

Escape special characters from a string.

Parameters
modeThe escape mode that needs to be enabled (see enum escape_mode)
inThe string to escape
Returns
The escaped string
Note
In html escape mode (escape_mode_html), we will only process HTML escape sequence, and string quoting, but we won't escape backslashes or double quotes
Warning
The returned string has been allocated and g_free() must thus be called on this string

References dbg, escape_mode_html, escape_mode_html_amp, escape_mode_html_apos, escape_mode_html_gt, escape_mode_html_lt, escape_mode_html_quote, escape_mode_quote, escape_mode_string, and lvl_debug.

Referenced by gui_internal_append_attr(), gui_internal_cmd_escape(), gui_internal_cmd_img(), gui_internal_onclick(), and write_former_destinations().

◆ strncpy_unescape()

char* strncpy_unescape ( char *  dest,
const char *  src,
size_t  n 
)

Copy a string from src to dest, unescaping characters.

Note
Escaped characters are "\\\\" (double backslash) resulting in '\' (single backslash) and "\\\"" (backslash followed by double quote), resulting in '"' (double quote) but we will escape any other character, for example "\\ " will result in ' ' (space) This is the reverse of function str_escape, except that we assume (and only support) unescaping mode escape_mode_quote here
Parameters
[out]destThe location where to store the unescaped string
[in]srcThe source string to copy (and to unescape)
nThe maximum amount of bytes copied into dest. Warning: If there is no null byte among the n bytes written to dest, the string placed in dest will not be null-terminated.
Returns
A pointer to the destination string dest

References startup::dest.

Referenced by attr_from_line().

◆ strtolower()

void strtolower ( char *  dest,
const char *  src 
)

◆ strtoupper()

void strtoupper ( char *  dest,
const char *  src 
)

Navit, a modular navigation system. Copyright (C) 2005-2008 Navit Team

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

References startup::dest.

◆ strtrim()

static void strtrim ( char *  s)
static

Trims all leading and trailing whitespace characters from a string.

Whitespace characters are all up to and including 0x20.

This function operates in-place, i.e. s will be modified.

Parameters
sThe string to trim

Referenced by parse_for_systematic_comparison().

◆ time_to_iso8601()

char* time_to_iso8601 ( time_t  time)

Converts time to ISO8601 format.

The caller is responsible for freeing the return value of this function when it is no longer needed.

Parameters
timeThe time, as returned by time() and related functions
Returns
Time in ISO8601 format

Referenced by current_to_iso8601(), traffic_dump_messages_to_xml(), and traffic_message_dump_to_stderr().

◆ uint_sqrt()

unsigned int uint_sqrt ( unsigned int  n)

Fast compute of square root for unsigned ints.

Parameters
nThe input number
Returns
sqrt(n)

References p.

Referenced by draw_shape(), and square_shape_str().