-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.c
executable file
·140 lines (101 loc) · 3.51 KB
/
array.c
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
/*
MIT License
Copyright (c) 2017 Dennis Addo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
//
// Created by Addo Dennis on 22/07/2017.
//
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include "array.h"
//static size_t initsize = 5;
sds_array init_sds_array(size_t size){
sds_array sda;
sda.mem = malloc(size * sizeof(char *));
if(sda.mem == NULL){
fprintf(stderr,"malloc: %s\n",strerror(errno));
exit(EXIT_FAILURE);
}
memset(sda.mem,0,size);
sda.capacity = size;
sda.size = 0;
return sda;
}
void add_str(sds_array *sdsa,const char *s){
if(sdsa->mem == NULL){
fprintf(stderr,"add_str: %s\n","Please init sdsa first");
return;
}
static size_t l = 0; // using l is not classic but just for easy readability as sda->mem[sda->size]
if(l == sdsa->capacity){
sdsa->capacity *= 2;
char **mem= realloc(sdsa->mem,sdsa->capacity * sizeof(char *));
if(mem == NULL){
fprintf(stderr,"realloc: %s\n",strerror(errno));
exit(EXIT_FAILURE);
}
sdsa->mem = mem;
}
size_t len = strlen(s)+1;
sdsa->mem[l] = malloc(len * sizeof(char));
if(sdsa->mem[l] == NULL){
fprintf(stderr,"malloc: %s\n",strerror(errno));
exit(EXIT_FAILURE);
}
snprintf(sdsa->mem[l],len,"%s",s);
sdsa->size = ++l;
}
inline int search_indx(size_t *ind, const sds_array *sda, const char *path){
int found = -1;
//Will be replaced with binarySearch for better process time
for (size_t i = 0; i < sda->size ; ++i) {
if(strcmp(sda->mem[i],path) == 0){
found = 0;
*ind = i;
break;
}
}
return found;
}
void delete_str(sds_array *array, const size_t index){
if(array->mem == NULL){
fprintf(stderr,"delete_str: %s\n"," sds_array is empty.!");
exit(EXIT_FAILURE);
}
if(index < array->size){
free(array->mem[index]);
array->mem[index] = NULL;
} else{
exit(EXIT_FAILURE);
}
//resizing the elements is Big O n O(n) when element is index = 0
// is a pain but will keep things in order.
for (size_t i = index; i < array->size ; ++i) {
array->mem[i] = (i != array->size-1) ? array->mem[i+1]: NULL;
}
array->size -= 1;
}
void freeall(sds_array *sds,size_t len){
for (size_t i = 0; sds->mem[i] != NULL && i <len ; ++i) {
free(sds->mem[i]);
sds->mem[i] = NULL;
}
free(sds->mem);
}