summaryrefslogtreecommitdiff
path: root/src/persistence_client_library_tree_helper.c
blob: 393817e6a36474b7777cbabfc1c8956eb201745a (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/******************************************************************************
 * Project         Persistency
 * (c) copyright   2015
 * Company         XS Embedded GmbH
 *****************************************************************************/
/******************************************************************************
 * This Source Code Form is subject to the terms of the
 * Mozilla Public License, v. 2.0. If a  copy of the MPL was not distributed
 * with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
******************************************************************************/
 /**
 * @file           persistence_client_library_tree_helper.c
 * @ingroup        Persistence client library
 * @author         Ingo Huerner
 * @brief          Implementation of persistence client library tree helper functions
 * @see
 */

#include "persistence_client_library_tree_helper.h"


#if 0
void debugFileItem(const char* prefix, FileHandleTreeItem_s* item)
{
   printf("-----------------------------------------\n");
   printf("   prefix        : %s\n", prefix);
   printf("   key           : %d\n", item->key );
   printf("   backupCreated : %d\n", item->value.fileHandle.backupCreated );
   printf("   backupPath    : %s\n", item->value.fileHandle.backupPath );
   printf("   csumPath      : %s\n", item->value.fileHandle.csumPath );
   printf("   filePath      : %s\n", item->value.fileHandle.filePath );
   printf("   cacheStatus   : %d\n", item->value.fileHandle.cacheStatus );
   printf("   permission    : %d\n", item->value.fileHandle.permission );
   printf("   userId        : %d\n", item->value.fileHandle.userId );
   printf("-----------------------------------------\n");
}
#endif
/**
 * File handle helper functions
 */

// compare function for tree item
int fh_key_val_cmp(const void *p1, const void *p2)
{
   int rval = -1;

   FileHandleTreeItem_s* first;
   FileHandleTreeItem_s* second;

   first  = (FileHandleTreeItem_s*)p1;
   second = (FileHandleTreeItem_s*)p2;

   if(second->key == first->key)
   {
      rval = 0;
   }
   else if(second->key < first->key)
   {
      rval = -1;
   }
   else
   {
      rval = 1;
   }

   return rval;
 }



/// duplicate function for tree item
void* fh_key_val_dup(void *p)
{
   FileHandleTreeItem_s* dst = NULL;
   FileHandleTreeItem_s* src = (FileHandleTreeItem_s*)p;

   // allocate memory for node
   dst = malloc(sizeof(FileHandleTreeItem_s));

   if(dst != NULL)
   {
     dst->key = src->key;               // duplicate hash key

     memcpy(dst->value.payload , src->value.payload, sizeof(FileHandleData_u) ); // duplicate value
   }

   return dst;
}

/// release function for tree item
void  fh_key_val_rel(void *p)
{
   FileHandleTreeItem_s* rel = (FileHandleTreeItem_s*)p;

   if(rel != NULL)
      free(rel);
}




/**
 * Key handle helper functions
 */

/// compare function for tree item
int kh_key_val_cmp(const void *p1, const void *p2)
{
   int rval = -1;

   KeyHandleTreeItem_s* first;
   KeyHandleTreeItem_s* second;

   first  = (KeyHandleTreeItem_s*)p1;
   second = (KeyHandleTreeItem_s*)p2;

   if(second->key == first->key)
   {
      rval = 0;
   }
   else if(second->key < first->key)
   {
      rval = -1;
   }
   else
   {
      rval = 1;
   }
   return rval;
 }



/// duplicate function for tree item
void* kh_key_val_dup(void *p)
{
   KeyHandleTreeItem_s* dst = NULL;
   KeyHandleTreeItem_s* src = (KeyHandleTreeItem_s*)p;

   // allocate memory for node
   dst = malloc(sizeof(KeyHandleTreeItem_s));

   if(dst != NULL)
   {
     dst->key = src->key;               // duplicate hash key

     memcpy(dst->value.payload , src->value.payload, sizeof(KeyHandleData_u) ); // duplicate value
   }
   return dst;
}



/// release function for tree item
void  kh_key_val_rel(void *p)
{
   KeyHandleTreeItem_s* rel = (KeyHandleTreeItem_s*)p;

   if(rel != NULL)
      free(rel);
}


/// compare function for tree key_value_s item
int key_val_cmp(const void *p1, const void *p2 )
{
   int rval = -1;
   key_value_s* first;
   key_value_s* second;

   first  = (key_value_s*)p1;
   second = (key_value_s*)p2;

   if(second->key == first->key)
   {
      rval = 0;
   }
   else if(second->key < first->key)
   {
      rval = -1;
   }
   else
   {
      rval = 1;
   }

   return rval;
 }

/// duplicate function for key_value_s item
void* key_val_dup(void *p)
{
   int value_size = 0;
   key_value_s* src = NULL;
   key_value_s* dst = NULL;

   src = (key_value_s*)p;
   value_size = strlen(src->value)+1;

   // allocate memory for node
   dst = malloc(sizeof(key_value_s));
   if(dst != NULL)
   {
     dst->key = src->key;               // duplicate hash key

     dst->value = malloc(value_size);  // duplicate value
     if(dst->value != NULL)
        strncpy(dst->value, src->value, value_size);
   }

   return dst;
}

/// release function for key_value_s item
void  key_val_rel(void *p )
{
   key_value_s* rel = NULL;
   rel = (key_value_s*)p;

   if(rel != NULL)
   {
      if(rel->value != NULL)
         free(rel->value);

      free(rel);
   }
}