main
util.h
1#ifndef UTIL_H_
2#define UTIL_H_
3
4#include <stdio.h>
5#include <time.h>
6#include "tern.h"
7
8typedef struct {
9 char *name;
10 uint8_t is_dir;
11} dir_entry;
12
13#define PATH_SEP "/"
14
15//Utility functions
16
17//Allocates a new string containing the concatenation of first and second
18char * alloc_concat(char const * first, char const * second);
19//Allocates a new string containing the concatenation of the strings pointed to by parts
20char * alloc_concat_m(int num_parts, char const ** parts);
21//Returns a newly allocated string in which all variables in based are replaced with values from vars or the environment
22char *replace_vars(char *base, tern_node *vars, uint8_t allow_env);
23//Byteswaps a ROM image in memory
24void byteswap_rom(int filesize, uint16_t *cart);
25//Returns the size of a file using fseek and ftell
26long file_size(FILE * f);
27//Strips whitespace and non-printable characters from the beginning and end of a string
28char * strip_ws(char * text);
29//Inserts a null after the first word, returns a pointer to the second word
30char * split_keyval(char * text);
31//Checks if haystack starts with prefix
32uint8_t startswith(const char *haystack, const char *prefix);
33//Takes a binary byte buffer and produces a lowercase hex string
34void bin_to_hex(uint8_t *output, uint8_t *input, uint64_t size);
35//Takes an (optionally) null-terminated UTF16-BE string and converts a maximum of max_size code-units to UTF-8
36char *utf16be_to_utf8(uint8_t *buf, uint32_t max_size);
37//Returns the next Unicode codepoint from a utf-8 string
38int utf8_codepoint(const char **text);
39//Determines whether a character is a valid path separator for the current platform
40char is_path_sep(char c);
41//Determines whether a path is considered an absolute path on the current platform
42char is_absolute_path(char *path);
43//Returns the basename of a path with th extension (if any) stripped
44char * basename_no_extension(const char *path);
45//Returns the extension from a path or NULL if there is no extension
46char *path_extension(char const *path);
47//Returns true if the given path matches one of the extensions in the list
48uint8_t path_matches_extensions(char *path, char **ext_list, uint32_t num_exts);
49//Returns the directory portion of a path or NULL if there is no directory part
50char *path_dirname(const char *path);
51//Gets the smallest power of two that is >= a certain value, won't work for values > 0x80000000
52uint32_t nearest_pow2(uint32_t val);
53//Should be called by main with the value of argv[0] for use by get_exe_dir
54void set_exe_str(char * str);
55//Returns the directory the executable is in
56char * get_exe_dir();
57//Returns the user's home directory
58char * get_home_dir();
59//Returns an appropriate path for storing config files
60char const *get_config_dir();
61//Returns an appropriate path for saving non-config data like savestates
62char const *get_userdata_dir();
63//Reads a file bundled with the executable
64char *read_bundled_file(char *name, uint32_t *sizeret);
65//Retunrs an array of normal files and directories residing in a directory
66dir_entry *get_dir_list(char *path, size_t *numret);
67//Frees a dir list returned by get_dir_list
68void free_dir_list(dir_entry *list, size_t numentries);
69//Performs a case-insensitive sort by file name on a dir list
70void sort_dir_list(dir_entry *list, size_t num_entries);
71//Gets the modification time of a file
72time_t get_modification_time(char *path);
73//Recusrively creates a directory if it does not exist
74int ensure_dir_exists(const char *path);
75//Returns the contents of a symlink in a newly allocated string
76char * readlink_alloc(char * path);
77//Prints an error message to stderr and to a message box if not in headless mode and then exits
78void fatal_error(char *format, ...);
79//Prints an information message to stdout and to a message box if not in headless mode and not attached to a console
80void info_message(char *format, ...);
81//Prints an information message to stderr and to a message box if not in headless mode and not attached to a console
82void warning(char *format, ...);
83//Prints a debug message to stdout
84void debug_message(char *format, ...);
85//Disables output of info and debug messages to stdout
86void disable_stdout_messages(void);
87
88#endif //UTIL_H_