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 double dc_atof (const char*);
30 char* dc_ftoa (double);
31 void dc_ltrim (char*);
32 void dc_rtrim (char*);
33 void dc_trim (char*);
34 char* dc_strlower (const char*); /* the result must be free()'d */
35 void dc_strlower_in_place (char*);
36 int dc_str_replace (char** haystack, const char* needle, const char* replacement);
37 int dc_str_contains (const char* haystack, const char* needle);
38 char* dc_null_terminate (const char*, int bytes); /* the result must be free()'d */
39 char* dc_mprintf (const char* format, ...); /* The result must be free()'d. */
40 char* dc_binary_to_uc_hex (const uint8_t* buf, size_t bytes);
41 void dc_remove_cr_chars (char*); /* remove all \r characters from string */
42 void dc_unify_lineends (char*);
43 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 */
44 size_t dc_utf8_strlen (const char*);
45 void dc_truncate_str (char*, int approx_characters);
46 void dc_truncate_n_unwrap_str (char*, int approx_characters, int do_unwrap);
47 carray* dc_split_into_lines (const char* buf_terminated); /* split string into lines*/
48 void dc_free_splitted_lines (carray* lines);
49 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 */
50 char* dc_str_from_clist (const clist*, const char* delimiter);
51 clist* dc_str_to_clist (const char*, const char* delimiter);
52 int dc_str_to_color (const char*);
53 
54 // 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)
55 char* encode_base64 (const char * in, int len);
56 
57 /* clist tools */
58 void clist_free_content (const clist*); /* calls free() for each item content */
59 int clist_search_string_nocase (const clist*, const char* str);
60 
61 /* date/time tools */
62 #define DC_INVALID_TIMESTAMP (-1)
63 #define DC_SECONDS_PER_DAY 86400
64 time_t dc_timestamp_from_date (struct mailimf_date_time * date_time); /* the result is UTC or DC_INVALID_TIMESTAMP */
65 char* dc_timestamp_to_str (time_t); /* the return value must be free()'d */
66 struct mailimap_date_time* dc_timestamp_to_mailimap_date_time (time_t);
67 long dc_gm2local_offset (void);
68 time_t mkgmtime (struct tm*);
69 
70 /* timesmearing */
71 time_t dc_smeared_time (dc_context_t*);
72 time_t dc_create_smeared_timestamp (dc_context_t*);
73 time_t dc_create_smeared_timestamps (dc_context_t*, int count);
74 
75 /* Message-ID tools */
76 #define DC_CREATE_ID_LEN 11
77 char* dc_create_id (void);
78 char* dc_create_incoming_rfc724_mid (time_t message_timestamp, uint32_t contact_id_from, dc_array_t* contact_ids_to);
79 char* dc_create_outgoing_rfc724_mid (const char* grpid, const char* addr);
80 char* dc_extract_grpid_from_rfc724_mid (const char* rfc724_mid);
81 char* dc_extract_grpid_from_rfc724_mid_list(const clist* rfc724_mid_list);
82 
83 
84 /* file tools */
85 void dc_ensure_no_slash (char* pathNfilename);
86 void dc_validate_filename (char* filename);
87 char* dc_get_filename (const char* pathNfilename);
88 void dc_split_filename (const char* pathNfilename, char** ret_basename, char** ret_all_suffixes_incl_dot); // the case of the suffix is preserved
89 char* dc_get_filesuffix_lc (const char* pathNfilename); // the returned suffix is lower-case
90 int dc_get_filemeta (const void* buf, size_t buf_bytes, uint32_t* ret_width, uint32_t *ret_height);
91 char* dc_get_abs_path (dc_context_t*, const char* pathNfilename);
92 int dc_file_exist (dc_context_t*, const char* pathNfilename);
93 uint64_t dc_get_filebytes (dc_context_t*, const char* pathNfilename);
94 int dc_delete_file (dc_context_t*, const char* pathNFilename);
95 int dc_copy_file (dc_context_t*, const char* pathNFilename, const char* dest_pathNFilename);
96 int dc_create_folder (dc_context_t*, const char* pathNfilename);
97 int dc_write_file (dc_context_t*, const char* pathNfilename, const void* buf, size_t buf_bytes);
98 int dc_read_file (dc_context_t*, const char* pathNfilename, void** buf, size_t* buf_bytes);
99 char* dc_get_fine_pathNfilename (dc_context_t*, const char* pathNfolder, const char* desired_name);
100 int dc_is_blobdir_path (dc_context_t*, const char* path);
101 void dc_make_rel_path (dc_context_t*, char** pathNfilename);
102 int dc_make_rel_and_copy (dc_context_t*, char** pathNfilename);
103 
104 /* macros */
105 #define DC_QUOTEHELPER(name) #name
106 #define DC_STRINGIFY(macro) DC_QUOTEHELPER(macro)
107 #define DC_MIN(X, Y) (((X) < (Y))? (X) : (Y))
108 #define DC_MAX(X, Y) (((X) > (Y))? (X) : (Y))
109 
110 
111 #ifdef __cplusplus
112 } /* /extern "C" */
113 #endif
114 #endif /* __DC_TOOLS_H__ */
dc_array_t
dc_context_t