Delta Chat Core C-API
dc_tools.h
1 /* Some tools and enhancements to the used libraries, there should be
2 no references to dc_context_t and other "larger" classes here. */
3 
4 
5 #ifndef __DC_TOOLS_H__
6 #define __DC_TOOLS_H__
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10 
11 
12 #include <libetpan/libetpan.h> // for carray etc.
13 
14 
15 /*** library-private **********************************************************/
16 
17 /* math tools */
18 int dc_exactly_one_bit_set (int v);
19 
20 /* string tools */
21 #define DC_EDITORIAL_OPEN "["
22 #define DC_EDITORIAL_CLOSE "]"
23 #define DC_EDITORIAL_ELLIPSE DC_EDITORIAL_OPEN "..." DC_EDITORIAL_CLOSE
24 #define DC_NDASH "\xE2\x80\x93"
25 
26 char* dc_strdup (const char*); /* dc_strdup() returns empty string if NULL is given, never returns NULL (exits on errors) */
27 char* dc_strdup_keep_null (const char*); /* strdup(NULL) is undefined, safe_strdup_keep_null(NULL) returns NULL in this case */
28 int dc_atoi_null_is_0 (const char*);
29 void dc_ltrim (char*);
30 void dc_rtrim (char*);
31 void dc_trim (char*);
32 char* dc_strlower (const char*); /* the result must be free()'d */
33 void dc_strlower_in_place (char*);
34 int dc_str_replace (char** haystack, const char* needle, const char* replacement);
35 int dc_str_contains (const char* haystack, const char* needle);
36 char* dc_null_terminate (const char*, int bytes); /* the result must be free()'d */
37 char* dc_mprintf (const char* format, ...); /* The result must be free()'d. */
38 char* dc_binary_to_uc_hex (const uint8_t* buf, size_t bytes);
39 void dc_remove_cr_chars (char*); /* remove all \r characters from string */
40 void dc_unify_lineends (char*);
41 void dc_replace_bad_utf8_chars (char*); /* replace bad UTF-8 characters by sequences of `_` (to avoid problems in filenames, we do not use eg. `?`) the function is useful if strings are unexpectingly encoded eg. as ISO-8859-1 */
42 void dc_truncate_str (char*, int approx_characters);
43 void dc_truncate_n_unwrap_str (char*, int approx_characters, int do_unwrap);
44 carray* dc_split_into_lines (const char* buf_terminated); /* split string into lines*/
45 void dc_free_splitted_lines (carray* lines);
46 char* dc_insert_breaks (const char*, int break_every, const char* break_chars); /* insert a break every n characters, the return must be free()'d */
47 char* dc_str_from_clist (const clist*, const char* delimiter);
48 clist* dc_str_to_clist (const char*, const char* delimiter);
49 int dc_str_to_color (const char*);
50 
51 // from libetpan/src/data-types/base64.h (which cannot be included without adding libetpan/src/... to the include-search-paths, which would result in double-file-name-errors, so, for now, we use this hack)
52 char* encode_base64 (const char * in, int len);
53 
54 /* clist tools */
55 void clist_free_content (const clist*); /* calls free() for each item content */
56 int clist_search_string_nocase (const clist*, const char* str);
57 
58 /* date/time tools */
59 #define DC_INVALID_TIMESTAMP (-1)
60 #define DC_SECONDS_PER_DAY 86400
61 time_t dc_timestamp_from_date (struct mailimf_date_time * date_time); /* the result is UTC or DC_INVALID_TIMESTAMP */
62 char* dc_timestamp_to_str (time_t); /* the return value must be free()'d */
63 struct mailimap_date_time* dc_timestamp_to_mailimap_date_time (time_t);
64 long dc_gm2local_offset (void);
65 
66 /* timesmearing */
67 time_t dc_smeared_time (dc_context_t*);
68 time_t dc_create_smeared_timestamp (dc_context_t*);
69 time_t dc_create_smeared_timestamps (dc_context_t*, int count);
70 
71 /* Message-ID tools */
72 #define DC_CREATE_ID_LEN 11
73 char* dc_create_id (void);
74 char* dc_create_incoming_rfc724_mid (time_t message_timestamp, uint32_t contact_id_from, dc_array_t* contact_ids_to);
75 char* dc_create_outgoing_rfc724_mid (const char* grpid, const char* addr);
76 char* dc_extract_grpid_from_rfc724_mid (const char* rfc724_mid);
77 char* dc_extract_grpid_from_rfc724_mid_list(const clist* rfc724_mid_list);
78 
79 
80 /* file tools */
81 void dc_ensure_no_slash (char* pathNfilename);
82 void dc_validate_filename (char* filename);
83 char* dc_get_filename (const char* pathNfilename);
84 void dc_split_filename (const char* pathNfilename, char** ret_basename, char** ret_all_suffixes_incl_dot); // the case of the suffix is preserved
85 char* dc_get_filesuffix_lc (const char* pathNfilename); // the returned suffix is lower-case
86 int dc_get_filemeta (const void* buf, size_t buf_bytes, uint32_t* ret_width, uint32_t *ret_height);
87 char* dc_get_abs_path (dc_context_t*, const char* pathNfilename);
88 int dc_file_exist (dc_context_t*, const char* pathNfilename);
89 uint64_t dc_get_filebytes (dc_context_t*, const char* pathNfilename);
90 int dc_delete_file (dc_context_t*, const char* pathNFilename);
91 int dc_copy_file (dc_context_t*, const char* pathNFilename, const char* dest_pathNFilename);
92 int dc_create_folder (dc_context_t*, const char* pathNfilename);
93 int dc_write_file (dc_context_t*, const char* pathNfilename, const void* buf, size_t buf_bytes);
94 int dc_read_file (dc_context_t*, const char* pathNfilename, void** buf, size_t* buf_bytes);
95 char* dc_get_fine_pathNfilename (dc_context_t*, const char* pathNfolder, const char* desired_name);
96 int dc_is_blobdir_path (dc_context_t*, const char* path);
97 void dc_make_rel_path (dc_context_t*, char** pathNfilename);
98 int dc_make_rel_and_copy (dc_context_t*, char** pathNfilename);
99 
100 /* macros */
101 #define DC_QUOTEHELPER(name) #name
102 #define DC_STRINGIFY(macro) DC_QUOTEHELPER(macro)
103 #define DC_MIN(X, Y) (((X) < (Y))? (X) : (Y))
104 #define DC_MAX(X, Y) (((X) > (Y))? (X) : (Y))
105 
106 
107 #ifdef __cplusplus
108 } /* /extern "C" */
109 #endif
110 #endif /* __DC_TOOLS_H__ */
An object representing a single account.
An object containing a simple array.