-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrap.h
More file actions
97 lines (82 loc) · 4.06 KB
/
strap.h
File metadata and controls
97 lines (82 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* strap.h - initial version */
#ifndef STRAP_H
#define STRAP_H
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <time.h>
#if defined(_WIN32)
# include <winsock2.h>
#else
# include <sys/time.h>
#endif
typedef enum
{
STRAP_OK = 0,
STRAP_ERR_INVALID_ARGUMENT,
STRAP_ERR_ALLOC,
STRAP_ERR_IO,
STRAP_ERR_OVERFLOW
} strap_error_t;
typedef struct strap_arena strap_arena_t;
strap_error_t strap_last_error(void);
const char *strap_error_string(strap_error_t err);
void strap_clear_error(void);
/* Safe reading */
char *afgets(FILE *f); /* reads a complete line, returns malloc() buffer or NULL */
char *afread(FILE *f, size_t *out_len); /* reads entire file into heap, returns buffer and length */
typedef struct
{
char *data;
size_t capacity;
} strap_line_buffer_t;
void strap_line_buffer_init(strap_line_buffer_t *buffer);
void strap_line_buffer_free(strap_line_buffer_t *buffer);
char *strap_line_buffer_read(FILE *f, strap_line_buffer_t *buffer); /* returns internal buffer */
/* String manipulation */
char *strjoin(const char **parts, size_t nparts, const char *sep); /* returns malloc() */
char *strjoin_va(const char *sep, ...); /* varargs, ends with NULL */
bool strstartswith(const char *s, const char *prefix);
bool strendswith(const char *s, const char *suffix);
char *strreplace(const char *s, const char *search, const char *replacement);
char *strtolower_locale(const char *s, const char *locale_name); /* malloc(), optional locale */
char *strtoupper_locale(const char *s, const char *locale_name); /* malloc(), optional locale */
int strcoll_locale(const char *a, const char *b, const char *locale_name);
int strcasecmp_locale(const char *a, const char *b, const char *locale_name);
int strap_strcasecmp(const char *a, const char *b); /* ASCII-only, portable */
bool strcaseeq(const char *a, const char *b);
typedef bool (*strap_split_predicate_fn)(unsigned char ch, void *userdata);
char **strsplit_limit(const char *s, const char *delim, size_t max_splits, size_t *out_count);
char **strsplit_predicate(const char *s,
strap_split_predicate_fn predicate,
void *userdata,
size_t max_splits,
size_t *out_count);
void strsplit_free(char **tokens);
/* Trim (inplace or return new) */
char *strtrim(const char *s); /* returns new malloc() without spaces at start/end */
void strtrim_inplace(char *s); /* modifies buffer in-place */
char *strtrim_arena(strap_arena_t *arena, const char *s);
/* Arena allocator */
strap_arena_t *strap_arena_create(size_t block_size);
void strap_arena_destroy(strap_arena_t *arena);
void strap_arena_clear(strap_arena_t *arena);
void *strap_arena_alloc(strap_arena_t *arena, size_t size);
char *strap_arena_strdup(strap_arena_t *arena, const char *s);
char *strap_arena_strndup(strap_arena_t *arena, const char *s, size_t n);
char *strjoin_arena(strap_arena_t *arena, const char **parts, size_t nparts, const char *sep);
char *strreplace_arena(strap_arena_t *arena, const char *s, const char *search, const char *replacement);
char *strtolower_locale_arena(strap_arena_t *arena, const char *s, const char *locale_name);
char *strtoupper_locale_arena(strap_arena_t *arena, const char *s, const char *locale_name);
/* Time utilities (struct timeval) */
struct timeval timeval_add(struct timeval a, struct timeval b);
struct timeval timeval_sub(struct timeval a, struct timeval b);
double timeval_to_seconds(struct timeval t);
struct timeval timeval_add_minutes(struct timeval t, int minutes);
int strap_time_offset_to_string(int offset_minutes, char *buf, size_t bufsize);
int strap_time_parse_tz_offset(const char *str, int *offset_minutes);
int strap_time_format_iso8601(struct timeval t, int offset_minutes, char *buf, size_t bufsize);
int strap_time_parse_iso8601(const char *str, struct timeval *out, int *offset_minutes);
int strap_time_local_offset(time_t when, int *offset_minutes);
int strap_time_format_iso8601_local(struct timeval t, char *buf, size_t bufsize);
#endif /* STRAP_H */