NetSurf
Macros | Functions
utils.c File Reference
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "utils/messages.h"
#include "utils/dirent.h"
#include "utils/inet.h"
#include "utils/utils.h"
#include <stdlib.h>
#include "utils/utsname.h"
#include "utils/regex.h"
Include dependency graph for utils.c:

Go to the source code of this file.

Macros

#define BYTESIZE_BUFFER_SIZE   32
 The size of buffers within human_friendly_bytesize. More...
 

Functions

char * squash_whitespace (const char *s)
 Replace consecutive whitespace with a single space. More...
 
char * cnv_space2nbsp (const char *s)
 Converts NUL terminated UTF-8 encoded string s containing zero or more spaces (char 32) or TABs (char 9) to non-breaking spaces (0xC2 + 0xA0 in UTF-8 encoding). More...
 
bool is_dir (const char *path)
 Check if a directory exists. More...
 
nserror vsnstrjoin (char **str, size_t *size, char sep, size_t nelm, va_list ap)
 Generate a string from one or more component elements separated with a single value. More...
 
nserror snstrjoin (char **str, size_t *size, char sep, size_t nelm,...)
 Generate a string from one or more component elements separated with a single value. More...
 
char * human_friendly_bytesize (unsigned long long int bsize)
 Create a human readable representation of a size in bytes. More...
 
unsigned long long int strtoull (const char *nptr, char **endptr, int base)
 string to unsigned long long More...
 
char * strcasestr (const char *haystack, const char *needle)
 Case insensitive strstr implementation. More...
 
char * strndup (const char *s, size_t n)
 Duplicate up to n characters of a string. More...
 
int alphasort (const struct dirent **d1, const struct dirent **d2)
 
int scandir (const char *dir, struct dirent ***namelist, int(*sel)(const struct dirent *), int(*compar)(const struct dirent **, const struct dirent **))
 
char * strchrnul (const char *s, int c_in)
 Find the first occurrence of C in S or the final NUL byte. More...
 
int uname (struct utsname *buf)
 Get the system information. More...
 
char * realpath (const char *path, char *resolved_path)
 
int inet_aton (const char *cp, struct in_addr *inp)
 
int inet_pton (int af, const char *src, void *dst)
 
int regcomp (regex_t *restrict preg, const char *restrictregex, int cflags)
 
size_t regerror (int errorcode, const regex_t *restrict preg, char *restrict errbuf, size_t errbuf_size)
 
int regexec (const regex_t *restrict preg, const char *restrict string, size_t nmatch, regmatch_t pmatch[restrict], int eflags)
 
void regfree (regex_t *preg)
 

Macro Definition Documentation

◆ BYTESIZE_BUFFER_SIZE

#define BYTESIZE_BUFFER_SIZE   32

The size of buffers within human_friendly_bytesize.

We can have a fairly good estimate of the output buffers maximum length.

The unsigned long long int can store a value representing a maximum size of 16 EiB (exibytes). Therefore the greatest space required is to represent 1023 PiB. Currently that would be represented as "1023.00PiBytes" in english giving a 15 byte length including a null terminator. Ideally we would be able to accurately know this length for other languages, in the mean time a largeish buffer size is selected and should suffice.

Definition at line 206 of file utils.c.

Function Documentation

◆ alphasort()

int alphasort ( const struct dirent **  d1,
const struct dirent **  d2 
)

Definition at line 355 of file utils.c.

◆ cnv_space2nbsp()

char * cnv_space2nbsp ( const char *  s)

Converts NUL terminated UTF-8 encoded string s containing zero or more spaces (char 32) or TABs (char 9) to non-breaking spaces (0xC2 + 0xA0 in UTF-8 encoding).

Caller needs to free() result. Returns NULL in case of error. No checking is done on validness of the UTF-8 input string.

Definition at line 67 of file utils.c.

Referenced by box_select_add_option(), and ro_gui_window_prepare_form_select_menu().

Here is the caller graph for this function:

◆ human_friendly_bytesize()

char * human_friendly_bytesize ( unsigned long long int  bytesize)

Create a human readable representation of a size in bytes.

Does a simple conversion which assumes the user speaks English. The buffer returned is one of three static ones so may change each time this call is made. Don't store the buffer for later use. It's done this way for convenience and to fight possible memory leaks, it is not necessarily pretty.

Parameters
bytesizeThe size in bytes.
Returns
A human readable string representing the size.

Definition at line 209 of file utils.c.

References BYTESIZE_BUFFER_SIZE, and messages_get().

Referenced by ami_download_check_overwrite(), fetch_curl_progress(), gui_download_window_create(), gui_download_window_data(), gui_download_window_done(), NSDownloadWindow::NSDownloadWindow(), nsgtk_download_info_to_string(), nsgtk_download_store_update_item(), nsws_download_update_label(), NSDownloadWindow::Progress(), and ro_gui_download_update_status().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ inet_aton()

int inet_aton ( const char *  cp,
struct in_addr *  inp 
)

Definition at line 489 of file utils.c.

Referenced by inet_pton(), and urldb__host_is_ip_address().

Here is the caller graph for this function:

◆ inet_pton()

int inet_pton ( int  af,
const char *  src,
void *  dst 
)

Definition at line 512 of file utils.c.

References EAFNOSUPPORT, and inet_aton().

Referenced by urldb__host_is_ip_address().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_dir()

bool is_dir ( const char *  path)

Check if a directory exists.

Definition at line 94 of file utils.c.

References path().

Referenced by filename_create_directory(), handle_filesystem_select_button(), menu_save_page(), ro_gui_choose_language(), and ro_gui_default_language().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ realpath()

char * realpath ( const char *  path,
char *  resolved_path 
)

Definition at line 473 of file utils.c.

References path().

Here is the call graph for this function:

◆ regcomp()

int regcomp ( regex_t *restrict  preg,
const char *  restrictregex,
int  cflags 
)

Definition at line 542 of file utils.c.

Referenced by regcomp_wrapper().

Here is the caller graph for this function:

◆ regerror()

size_t regerror ( int  errorcode,
const regex_t *restrict  preg,
char *restrict  errbuf,
size_t  errbuf_size 
)

Definition at line 548 of file utils.c.

Referenced by regcomp_wrapper().

Here is the caller graph for this function:

◆ regexec()

int regexec ( const regex_t *restrict  preg,
const char *restrict  string,
size_t  nmatch,
regmatch_t  pmatch[restrict],
int  eflags 
)

Definition at line 560 of file utils.c.

References REG_NOMATCH.

Referenced by snregexec().

Here is the caller graph for this function:

◆ regfree()

void regfree ( regex_t preg)

Definition at line 569 of file utils.c.

Referenced by save_complete_finalise().

Here is the caller graph for this function:

◆ scandir()

int scandir ( const char *  dir,
struct dirent ***  namelist,
int(*)(const struct dirent *)  sel,
int(*)(const struct dirent **, const struct dirent **)  compar 
)

Definition at line 361 of file utils.c.

References compar().

Here is the call graph for this function:

◆ snstrjoin()

nserror snstrjoin ( char **  str,
size_t *  size,
char  sep,
size_t  nelm,
  ... 
)

Generate a string from one or more component elements separated with a single value.

This is similar in intent to the Perl join function creating a single delimited string from an array of several.

Note
If a string is allocated it must be freed by the caller.
Parameters
[in,out]strpointer to string pointer if this is NULL enough storage will be allocated for the complete path.
[in,out]sizeThe size of the space available if str not NULL on input and if not NULL set to the total output length on output.
[in]sepThe character to separate the elements with.
[in]nelmThe number of elements up to a maximum of 16.
[in]...The elements of the path as string pointers.
Returns
NSERROR_OK and the complete path is written to str or error code on failure.

Definition at line 179 of file utils.c.

References vsnstrjoin().

Here is the call graph for this function:

◆ squash_whitespace()

char * squash_whitespace ( const char *  s)

Replace consecutive whitespace with a single space.

Todo:
determine if squash_whitespace utf-8 safe and that it needs to be
Parameters
ssource string
Returns
heap allocated result, or NULL on memory exhaustion

Definition at line 38 of file utils.c.

Referenced by box_construct_element(), box_construct_text(), box_image(), box_select_add_option(), and html_process_title().

Here is the caller graph for this function:

◆ strcasestr()

char * strcasestr ( const char *  haystack,
const char *  needle 
)

Case insensitive strstr implementation.

Parameters
haystackString to search in
needleString to look for
Returns
Pointer to start of found substring, or NULL if not found

Definition at line 310 of file utils.c.

Referenced by html_create_style_element(), html_css_process_link(), html_css_process_style(), ro_gui_wimp_desktop_font(), and treeview__search_walk_cb().

Here is the caller graph for this function:

◆ strchrnul()

char * strchrnul ( const char *  s,
int  c_in 
)

Find the first occurrence of C in S or the final NUL byte.

Definition at line 443 of file utils.c.

◆ strndup()

char * strndup ( const char *  s,
size_t  n 
)

◆ strtoull()

unsigned long long int strtoull ( const char *  nptr,
char **  endptr,
int  base 
)

string to unsigned long long

Definition at line 293 of file utils.c.

References base.

Referenced by download_context_process_headers().

Here is the caller graph for this function:

◆ uname()

int uname ( struct utsname buf)

Get the system information.

Parameters
bufthe buffer to fill with the information.
Returns
0 on sucess or -1 and errno set on faliure.

Definition at line 459 of file utils.c.

References utsname::machine, utsname::nodename, utsname::release, utsname::sysname, and utsname::version.

Referenced by nslog_init(), and user_agent_build_string().

Here is the caller graph for this function:

◆ vsnstrjoin()

nserror vsnstrjoin ( char **  str,
size_t *  size,
char  sep,
size_t  nelm,
va_list  ap 
)

Generate a string from one or more component elements separated with a single value.

This is similar in intent to the perl join function creating a single delimited string from an array of several.

Note
If a string is allocated it must be freed by the caller.
Parameters
[in,out]strpointer to string pointer if this is NULL enough storage will be allocated for the complete path.
[in,out]sizeThe size of the space available if str not NULL on input and if not NULL set to the total output length on output.
[in]sepThe character to separate the elements with.
[in]nelmThe number of elements up to a maximum of 16.
[in]apThe elements of the path as string pointers.
Returns
NSERROR_OK and the complete path is written to str or error code on failure.

Definition at line 107 of file utils.c.

References NSERROR_BAD_PARAMETER, NSERROR_NOMEM, NSERROR_NOSPACE, and NSERROR_OK.

Referenced by atari_mkpath(), posix_vmkpath(), snstrjoin(), and windows_mkpath().

Here is the caller graph for this function: