Delta Chat Core C-API
mrtools.h
1 /*******************************************************************************
2  *
3  * Delta Chat Core
4  * Copyright (C) 2017 Björn Petersen
5  * Contact: r10s@b44t.com, http://b44t.com
6  *
7  * This program is free software: you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later
10  * version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program. If not, see http://www.gnu.org/licenses/ .
19  *
20  ******************************************************************************/
21 
22 
23 /* Some tools and enhancements to the used libraries, there should be
24 no references to mrmailbox_t and other "larger" classes here. */
25 
26 
27 #ifndef __MRTOOLS_H__
28 #define __MRTOOLS_H__
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 
34 /*** library-private **********************************************************/
35 
36 /* math tools */
37 int mr_exactly_one_bit_set (int v);
38 
39 /* string tools */
40 char* safe_strdup (const char*); /* safe_strdup() returns empty string if NULL is given, never returns NULL (exists on errors) */
41 char* strdup_keep_null (const char*); /* strdup(NULL) is undefined, safe_strdup_keep_null(NULL) returns NULL in this case */
42 int atoi_null_is_0 (const char*);
43 void mr_ltrim (char*);
44 void mr_rtrim (char*);
45 void mr_trim (char*);
46 char* mr_strlower (const char*); /* the result must be free()'d */
47 void mr_strlower_in_place (char*);
48 int mr_str_replace (char** haystack, const char* needle, const char* replacement);
49 int mr_str_contains (const char* haystack, const char* needle);
50 char* mr_null_terminate (const char*, int bytes); /* the result must be free()'d */
51 char* mr_mprintf (const char* format, ...); /* The result must be free()'d. */
52 void mr_remove_cr_chars (char*); /* remove all \r characters from string */
53 void mr_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 */
54 void mr_truncate_n_unwrap_str (char*, int approx_characters, int do_unwrap);
55 carray* mr_split_into_lines (const char* buf_terminated); /* split string into lines*/
56 void mr_free_splitted_lines (carray* lines);
57 char* mr_insert_breaks (const char*, int break_every, const char* break_chars); /* insert a break every n characters, the return must be free()'d */
58 char* mr_arr_to_string (const uint32_t*, int cnt);
59 char* mr_decode_header_string (const char*); /* the result must be free()'d */
60 char* mr_encode_header_string (const char*); /* the result must be free()'d */
61 char* imap_modified_utf7_to_utf8 (const char *mbox, int change_spaces);
62 char* imap_utf8_to_modified_utf7 (const char *src, int change_spaces);
63 char* mr_url_encode (const char*); /* the result must be free()'d */
64 char* encode_base64 (const char * in, int len); /* prototype, 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 */
65 
66 /* string builder */
67 typedef struct mrstrbuilder_t
68 {
69  char* m_buf;
70  int m_allocated;
71  int m_free;
72  char* m_eos;
73 } mrstrbuilder_t;
74 void mrstrbuilder_init (mrstrbuilder_t* ths);
75 char* mrstrbuilder_cat (mrstrbuilder_t* ths, const char* text);
76 void mrstrbuilder_empty(mrstrbuilder_t* ths); /* set the string to a lenght of 0, does not free the buffer */
77 
78 
79 /* clist tools */
80 void clist_free_content (const clist*); /* calls free() for each item content */
81 int clist_search_string_nocase (const clist*, const char* str);
82 
83 /* date/time tools */
84 #define MR_INVALID_TIMESTAMP (-1)
85 time_t mr_timestamp_from_date (struct mailimf_date_time * date_time); /* the result is UTC or MR_INVALID_TIMESTAMP */
86 char* mr_timestamp_to_str (time_t); /* the return value must be free()'d */
87 struct mailimap_date_time* mr_timestamp_to_mailimap_date_time (time_t);
88 long mr_gm2local_offset (void);
89 
90 /* timesmearing */
91 time_t mr_smeared_time__ (void);
92 time_t mr_create_smeared_timestamp__ (void);
93 time_t mr_create_smeared_timestamps__(int count);
94 
95 /* Message-ID tools */
96 #define MR_VALID_ID_LEN 11
97 char* mr_create_id (void);
98 char* mr_create_dummy_references_mid (void);
99 char* mr_create_incoming_rfc724_mid (time_t message_timestamp, uint32_t contact_id_from, mrarray_t* contact_ids_to);
100 char* mr_create_outgoing_rfc724_mid (const char* grpid, const char* addr);
101 char* mr_extract_grpid_from_rfc724_mid (const char* rfc724_mid);
102 char* mr_extract_grpid_from_rfc724_mid_list(const clist* rfc724_mid_list);
103 
104 
105 /* file tools */
106 int mr_file_exist (const char* pathNfilename);
107 uint64_t mr_get_filebytes (const char* pathNfilename);
108 char* mr_get_filename (const char* pathNfilename); /* the return value must be free()'d */
109 int mr_delete_file (const char* pathNFilename, mrmailbox_t* log);
110 int mr_copy_file (const char* src_pathNFilename, const char* dest_pathNFilename, mrmailbox_t* log);
111 int mr_create_folder (const char* pathNfilename, mrmailbox_t* log);
112 int mr_write_file (const char* pathNfilename, const void* buf, size_t buf_bytes, mrmailbox_t* log);
113 int mr_read_file (const char* pathNfilename, void** buf, size_t* buf_bytes, mrmailbox_t* log);
114 char* mr_get_filesuffix_lc (const char* pathNfilename); /* the returned suffix is lower-case */
115 void mr_split_filename (const char* pathNfilename, char** ret_basename, char** ret_all_suffixes_incl_dot); /* the case of the suffix is preserved! */
116 int mr_get_filemeta (const void* buf, size_t buf_bytes, uint32_t* ret_width, uint32_t *ret_height);
117 char* mr_get_fine_pathNfilename (const char* folder, const char* desired_name);
118 
119 
120 /* macros */
121 #define MR_QUOTEHELPER(name) #name
122 #define MR_STRINGIFY(macro) MR_QUOTEHELPER(macro)
123 #define MR_MIN(X, Y) (((X) < (Y))? (X) : (Y))
124 #define MR_MAX(X, Y) (((X) > (Y))? (X) : (Y))
125 
126 
127 #ifdef __cplusplus
128 } /* /extern "C" */
129 #endif
130 #endif /* __MRTOOLS_H__ */
An object containing a simple array.
Definition: mrarray.h:39
An object representing a single mailbox.
Definition: mrmailbox.h:194