Line data Source code
1 : #include "mem.h"
2 :
3 : /**
4 : * @brief Shared zero-terminated buffer used when descriptors are unavailable.
5 : */
6 : static char empty_string[] = "";
7 :
8 985 : const char *memory_getcstring(const memory *memory_object)
9 : {
10 985 : if(memory_object == NULL)
11 : {
12 0 : return empty_string;
13 : }
14 :
15 985 : const char *text = (const char *)memory_const_data_checked(memory_object,sizeof(char));
16 :
17 985 : if(text == NULL)
18 : {
19 330 : return empty_string;
20 : }
21 :
22 655 : if(memory_object->length == 0)
23 : {
24 0 : return empty_string;
25 : }
26 :
27 655 : size_t visible_length = 0;
28 :
29 655 : if(memory_string_length(memory_object,&visible_length) != SUCCESS)
30 : {
31 0 : return empty_string;
32 : }
33 :
34 655 : if(visible_length >= memory_object->length)
35 : {
36 0 : return empty_string;
37 : }
38 :
39 655 : return text;
40 : }
41 :
42 161 : char *memory_getstring(memory *memory_object)
43 : {
44 161 : if(memory_object == NULL)
45 : {
46 0 : return empty_string;
47 : }
48 :
49 161 : char *text = (char *)memory_data_checked(memory_object,sizeof(char));
50 :
51 161 : if(text == NULL)
52 : {
53 0 : return empty_string;
54 : }
55 :
56 161 : if(memory_object->length == 0)
57 : {
58 0 : if(memory_resize(memory_object,1,UCHAR_MAX) != SUCCESS)
59 : {
60 0 : return empty_string;
61 : }
62 :
63 0 : text = (char *)memory_data_checked(memory_object,sizeof(char));
64 :
65 0 : if(text == NULL)
66 : {
67 0 : return empty_string;
68 : }
69 :
70 0 : text[0] = '\0';
71 0 : return text;
72 : }
73 :
74 161 : size_t visible_length = 0;
75 :
76 161 : if(memory_string_length(memory_object,&visible_length) != SUCCESS)
77 : {
78 0 : return empty_string;
79 : }
80 :
81 161 : if(visible_length >= memory_object->length)
82 : {
83 0 : text[0] = '\0';
84 0 : return text;
85 : }
86 :
87 161 : return text;
88 : }
|