Skip to content
This repository was archived by the owner on Sep 21, 2021. It is now read-only.

Commit ddaccd3

Browse files
committed
refactor: minor formatting
1 parent 8973d7c commit ddaccd3

File tree

1 file changed

+72
-88
lines changed

1 file changed

+72
-88
lines changed

main.c

Lines changed: 72 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ enum gentype {
1313
File=4,
1414

1515
TotalGens=3,
16-
Error=-1
1716
};
1817

1918
enum methodtype {
@@ -22,7 +21,7 @@ enum methodtype {
2221
Scl=4,
2322

2423
TotalMethods=3,
25-
Undef=-1
24+
Undef=0
2625
};
2726

2827
t_list *init_list();
@@ -63,13 +62,13 @@ int main(int argc, char *argv[])
6362
}
6463
}
6564

66-
t_colgen **colgen = start_arrcolgen(info, amt_cols+amt_link);
65+
t_colgen **arrcolgen = start_arrcolgen(info, amt_cols+amt_link);
6766

6867
print_colinfo(info,amt_cols);
6968
clean_colinfo(info,amt_cols);
7069

71-
print_arrcolgen(colgen, amt_cols+amt_link);
72-
destroy_arrcolgen(colgen, amt_cols+amt_link);
70+
print_arrcolgen(arrcolgen, amt_cols+amt_link);
71+
destroy_arrcolgen(arrcolgen, amt_cols+amt_link);
7372
return 0;
7473
}
7574

@@ -79,14 +78,36 @@ t_list *init_list()
7978
assert(new_list);
8079

8180
new_list->svalue = NULL;
82-
8381
new_list->root = NULL;
84-
8582
new_list->tree_size = 0;
8683

8784
return new_list;
8885
}
8986

87+
void start_arrlist(t_colgen *colgen)
88+
{
89+
assert(!colgen->_list);
90+
assert(colgen->amt_row);
91+
92+
t_list **new_arrlist = malloc(colgen->amt_row * sizeof(t_list*));
93+
assert(new_arrlist);
94+
95+
for ( int i = 0; i < colgen->amt_row; ++i )
96+
new_arrlist[i] = init_list();
97+
98+
if ( colgen->gentype & List ){
99+
if ( colgen->gentype & File ){
100+
FILE *f_read = fopen("content/nomes.txt", "r");
101+
file_to_arrlist(f_read, new_arrlist, colgen);
102+
fclose(f_read);
103+
} else { // normal list
104+
nums_to_arrlist(new_arrlist, colgen);
105+
}
106+
}
107+
108+
colgen->_list = new_arrlist;
109+
}
110+
90111
void destroy_arrlist(short gentype, t_list** list, int amt_row)
91112
{
92113
assert(list);
@@ -100,12 +121,11 @@ void destroy_list(t_list* list, short gentype)
100121
{
101122
assert(list);
102123

103-
if (( list->svalue ) && ( gentype == ( File | List ) ))
124+
if (( gentype == ( File | List ) ) && ( list->svalue ))
104125
free(list->svalue);
105-
//implement free traversing tree
106126
if ( list->root )
107127
free(list->root);
108-
free(list);
128+
free(list);
109129
}
110130

111131

@@ -120,23 +140,23 @@ t_templ *init_templ()
120140
return new_templ;
121141
}
122142

123-
void start_templ(t_colgen* colgen, t_templ* templ)
143+
void start_templ(t_colgen* colgen, t_templ* new_templ)
124144
{
125145
assert(colgen);
126-
assert(templ);
146+
assert(new_templ);
127147
assert(!colgen->_template);
128148

129-
templ->dvalue = colgen->rwall - colgen->lwall;
149+
new_templ->dvalue = colgen->rwall - colgen->lwall;
130150

131-
enum methodtype methods = Rnd;
132-
for ( short i = 0; i < TotalMethods; ++i ){
133-
if ( colgen->method & ( methods << i ) ){
134-
//templ->fn = templ_fns[i];
135-
break;
151+
if (colgen->gentype & Template){
152+
if (colgen->gentype & File){
153+
//filesetter_templ(new_templ, colgen);
154+
} else {
155+
numsetter_templ(new_templ, colgen);
136156
}
137157
}
138158

139-
colgen->_template = templ;
159+
colgen->_template = new_templ;
140160
}
141161

142162
void destroy_templ(t_templ *templ)
@@ -152,9 +172,10 @@ void def_typeof(t_colinfo *info, t_colgen *colgen)
152172

153173
char letter[3] = {'r','u','s'}; //this needs to be made global
154174

155-
short new_method = 0;
156-
enum methodtype methods = Rnd;
175+
short new_method = Undef;
176+
short new_gentype = Undef;
157177

178+
enum methodtype methods = Rnd;
158179
for ( short i = 0; i < TotalMethods; ++i ){
159180
if ( strchr(info->option, letter[i]) ){
160181
new_method |= ( methods << i );
@@ -163,27 +184,28 @@ void def_typeof(t_colinfo *info, t_colgen *colgen)
163184

164185
//if new_method is random and also unique or scalable
165186
if (( new_method & Rnd ) && ( new_method & ( Unq | Scl ) )){
166-
colgen->gentype = List; //then it must be a list
187+
new_gentype = List; //then it must be a list
167188
} else {
168-
colgen->gentype = Template; //otherwise it's a template
169-
} colgen->method = new_method;
189+
new_gentype = Template; //otherwise it's a template
190+
}
170191

171192
if ( info->file ){
172-
if ( colgen->method & Rnd ){
173-
colgen->gentype = File | List;
193+
if ( new_method & Rnd ){
194+
new_gentype = List;
174195
}
175-
else {
176-
colgen->gentype = File | Template;
177-
}
196+
new_gentype |= File;
178197
}
198+
199+
colgen->method = new_method;
200+
colgen->gentype = new_gentype;
179201
}
180202

181203
t_colgen *init_colgen()
182204
{
183205
t_colgen *new_colgen = malloc(sizeof(t_colgen));
184206

185-
new_colgen->method = Undef; //-1
186-
new_colgen->gentype = Error; //-1
207+
new_colgen->method = Undef;
208+
new_colgen->gentype = Undef;
187209
new_colgen->_list = NULL;
188210
new_colgen->amt_row = DEFAULT_DBSIZE;
189211
new_colgen->lwall = 0.0;
@@ -197,94 +219,58 @@ t_colgen *init_colgen()
197219

198220
t_colgen *start_colgen(t_colinfo* info, t_colgen* colgen)
199221
{
200-
if ( info->lwall ){
222+
if ( info->lwall )
201223
colgen->lwall = strtod(info->lwall, NULL);
202-
}
203-
204-
if ( info->rwall ){
224+
if ( info->rwall )
205225
colgen->rwall = strtod(info->rwall, NULL);
206-
}
226+
if ( info->delim )
227+
colgen->delim = info->delim;
228+
if ( info->decimals )
229+
colgen->decimals = info->decimals;
207230

208-
if (( info->amount ) && ( atoi(info->amount) < DEFAULT_DBSIZE )){
231+
if (( info->amount ) && ( atoi(info->amount) < DEFAULT_DBSIZE ))
209232
colgen->amt_row = atoi(info->amount);
210-
}
211-
else if (( colgen->rwall - colgen->lwall ) < DEFAULT_DBSIZE ){
233+
else if (( colgen->rwall - colgen->lwall ) < DEFAULT_DBSIZE )
212234
colgen->amt_row = colgen->rwall - colgen->lwall;
213-
}
214-
215-
if ( info->delim != '\0' ){ // if specified delim in info
216-
colgen->delim = info->delim; // col will input such delim
217-
}
218235

219-
if ( info->decimals > 0){
220-
colgen->decimals = info->decimals;
221-
}
222236

223237
def_typeof(info, colgen);
224-
short gentype = colgen->gentype;
225-
if ( gentype & List ){
238+
if ( colgen->gentype & List )
226239
start_arrlist(colgen);
227-
}
228-
else if ( gentype & Template ){
240+
else if ( colgen->gentype & Template )
229241
start_templ(colgen, init_templ());
230-
}
231242

232243
return colgen;
233244
}
234245

235-
void start_arrlist(t_colgen *colgen)
236-
{
237-
assert(!colgen->_list);
238-
assert(colgen->amt_row);
239-
240-
t_list **new_arrlist = malloc(colgen->amt_row * sizeof(t_list*));
241-
assert(new_arrlist);
242-
243-
for ( int i = 0; i < colgen->amt_row; ++i ){
244-
new_arrlist[i] = init_list();
245-
}
246-
if ( colgen->gentype == List ){
247-
nums_to_arrlist(new_arrlist, colgen);
248-
}
249-
else if ( colgen->gentype == ( File | List ) ){
250-
FILE *f_read = fopen("content/nomes.txt", "r");
251-
file_to_arrlist(f_read, new_arrlist, colgen);
252-
fclose(f_read);
253-
}
254-
255-
colgen->_list = new_arrlist;
256-
}
257-
258246
t_colgen **start_arrcolgen(t_colinfo *info, int amt_cols)
259247
{
260248
assert(info);
261249

262250
t_colgen **new_arrcolgen = malloc(amt_cols * sizeof(t_colgen*));
263251
assert(new_arrcolgen);
264252

265-
int j = 0; //iterate through colgen index
253+
int j = 0; //iterate through, and add to, colgen index
266254
int i = 0; //iterate through colinfo index
267-
while ( j < (amt_cols) ){
255+
while ( j < amt_cols ){
268256
new_arrcolgen[j] = start_colgen(info+i, init_colgen());
269257

270258
if ( (info+i)->link ){
271259
int linker_index = j; //hold onto the linker index
272260
t_colinfo *ptr_link = (info+i)->link;
273261
while ( ptr_link ){
274-
++j;
262+
++j;
275263
new_arrcolgen[j] = start_colgen(ptr_link, init_colgen());
276264
new_arrcolgen[j]->_linker = new_arrcolgen[linker_index];
277265

278266
ptr_link = ptr_link->link;
279267
}
280-
} else {
281-
new_arrcolgen[j]->_linker = NULL;
282268
}
283269

270+
++j;
284271
if ( i < amt_cols ){
285272
++i;
286273
}
287-
++j;
288274
}
289275

290276
return new_arrcolgen;
@@ -298,12 +284,13 @@ void print_list(size_t amt_rows, t_list **list, short gentype)
298284
if ( gentype == List ){
299285
fprintf(stderr, "\t# %f\n", list[i]->dvalue);
300286
}
301-
else if (( list[i]->svalue ) && ( gentype == ( File | List ) )){
287+
else if (( gentype == ( File | List ) ) && ( list[i]->svalue )){
302288
fprintf(stderr, "\t# %s\n", list[i]->svalue);
303289
}
304290

305291
if ( list[i]->tree_size ){
306-
fprintf(stderr, "\t#%d tree_size: %ld", i+1, list[i]->tree_size);
292+
fprintf(stderr, "\t#%d tree_size: %ld", i+1,
293+
list[i]->tree_size);
307294
}
308295
} fputc('\n', stderr);
309296
}
@@ -337,9 +324,6 @@ void print_arrcolgen(t_colgen **colgen, int amt_cols)
337324
fprintf(stderr, "gentype: list\n");
338325
}
339326
print_list( colgen[i]->amt_row, colgen[i]->_list, colgen[i]->gentype );
340-
} else {
341-
_error(ERR_READ, "Nil gentype");
342-
exit(EXIT_FAILURE);
343327
}
344328
}
345329
}
@@ -360,7 +344,7 @@ void destroy_arrcolgen(t_colgen **arrcolgen, int amt_cols)
360344
{
361345
assert(arrcolgen);
362346

363-
for ( int i = 0; i < amt_cols; ++i ){
347+
for ( int i = 0; i < amt_cols; ++i )
364348
destroy_colgen(arrcolgen[i]);
365-
} free (arrcolgen);
349+
free (arrcolgen);
366350
}

0 commit comments

Comments
 (0)