-
Notifications
You must be signed in to change notification settings - Fork 0
/
fqGetIdsStructs.c
633 lines (524 loc) · 24.8 KB
/
fqGetIdsStructs.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/*##############################################################################
# Name:
# Use: Holds structers needed to make a tree of read names
##############################################################################*/
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
' SOF: fqGetsIdsStructs
' fun-1 makeReadInfoStruct:
' o Makes a readInfo struction with default values
' fun-2 freeReadInfoStruct:
' o Frees a readInfo structer
' fun-3 pushReadNodeStack:
' o Pushes a readNodeStack structer onto stack
' fun-4 popReadNodeStack:
' o Frees readNodeStack & returns next node in stack
' fun-5 makeBigNumStruct:
' o Converts hex elements in c-string to big number
' fun-6 strToBackwardsBigNum:
' o Filps c-string & converts to big number
' fun-7 freeBigNumStruct:
' o Frees bigNum structer
' fun-8 cmpBigNums:
' o Compare two big numbers
' fun-9 cnvtIdToBigNum:
' o Read in read id line & convert to big num
\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include "fqGetIdsStructs.h"
/*Make look up table to look if character is valid hex character
64 is invisivle character
32 is non-hex character (printable)
*/
char hexTblCharAry[] =
{
/*0-32 (invisible)*/
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64,
/*Special characters ! " # $ % & ' ( ) & * + ' - . / (33 to 47)*/
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
/*Numbers: 48 (0) to 57 (9)*/
16, 1, 2, 3, 4, 5, 6, 7, 8, 9, /*48-57 Numbers*/
/*Special characters : ; < = > ? @ (58 to 64)*/
0, 32, 32, 32, 32, 32, 32,
//32, 32, 32, 32, 32, 32, 32,
// Treating : as 0 so that it is kept in the
// 4 bit setup
/*Hext A-F (65-70)*/
10, 11, 12, 13, 14, 15,
/*5 bit G-U (71 to 85)*/
//32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
/*W,X,Y,Z (86 to 90) [this would need to be 6 bit*/
//32,32,32,32,32,
0,0,0,0,0,
/*specialcharacters [ \ ] ^ _ ` (91 to 96)*/
32,32,32,32,32,32,
/*a-f (97 to 102)*/
10, 11, 12, 13, 14, 15,
/*5 bit g-u (103 to 117)*/
//32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
/*w,x,y,z 98-122 (this would need to be 6 bit)*/
//32,32,32,32,32,
0,0,0,0,0,
/*special characters { | } ~ (123 to 126)*/
32, 32, 32, 32,
/*127 del*/
32,
/*unsigned extended range (just in case)*/
32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32
};
/*---------------------------------------------------------------------\
| Output: Modifies: readInfoStruct to have default values (all 0's)
\---------------------------------------------------------------------*/
struct readInfo * makeReadInfoStruct(
char *readIdCStr, /*c-string with read name to copy*/
const unsigned int *lenCStrUInt /*Length of cString to convert*/
) /*Allocates memomory & makes a readInfo struct (variables set to 0)*/
{ /*makeReadInfoStruct*/
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
' Fun-1 TOC: Make a readInfo structer
' fun-1 sec-1: Variable declerations
' fun-1 sec-2: Copy the input string
' fun-1 sec-3: Set other variables to 0
\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Fun-1 Sec-1: Variable declerations
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
struct readInfo
*readInfoStruct = malloc(sizeof(readInfo));
if(readInfoStruct == 0)
{ /*If mallac failed to allocate memory*/
fprintf(
stderr,
"Failed to allocate memory: Fun-1 makeReadInfoStruct %s",
"fastqGrepStructs.c Line 40\n"
); /*Let user know that memory allocation failed*/
return 0;
} /*If mallac failed to allocate memory*/
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Fun-1 Sec-2: Get numeric id of intput string
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
/*Convert hex elements in read id to big number*/
readInfoStruct->idBigNum = makeBigNumStruct(readIdCStr,lenCStrUInt);
if(readInfoStruct->idBigNum == 0)
{ /*If mallac failed to allocate memory*/
fprintf(
stderr,
"Failed to allocate memory for big number: Fun-1 %s",
"makeReadInfoStruct fastqGrepStructs.c Line 57\n"
); /*Let user know that memory allocation failed*/
return 0;
} /*If mallac failed to allocate memory*/
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Fun-1 Sec-3: Set other variables to 0
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
readInfoStruct->balanceChar = 0;
readInfoStruct->leftChild = 0;
readInfoStruct->rightChild = 0;
return readInfoStruct;
} /*makeReadInfoStruct*/
/*---------------------------------------------------------------------\
| Output: frees readInforStruct and sets pointer to 0
| Note: Does not free nodeInGraph.
\---------------------------------------------------------------------*/
void freeReadInfoStruct(
struct readInfo **readInfoStruct /*struct to free*/
) /*frees a readInfo structer*/
{ /*freeReadInfoStruct*/
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Fun-2 Sec-1 TOC: free readInfoStruct
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
freeBigNumStruct(&((*readInfoStruct)->idBigNum));
free(*readInfoStruct); /*User handles nodeInGraph*/
*readInfoStruct = 0;
return;
} /*freeReadInfoStruct*/
/*---------------------------------------------------------------------\
| Output:
| Modifes readStack to piont to last element
\---------------------------------------------------------------------*/
void pushReadNodeStack(
struct readNodeStack **readStackAry, /*Array of read info nodes*/
struct readInfo *readNode /*readInfo to assing to next node*/
) /*pushes a readNodeStack structer onto a readNodeStack stack*/
{ /*makeReadNodeStack*/
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Fun-3 Sec-1 TOC: makes a readNodeStack structer
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
/*Move to next node*/
*readStackAry = (*readStackAry) + 1; /*+ sizeof(readNodeStack);*/
(*readStackAry)->readNode = readNode;
return;
} /*makeReadNodeStack*/
/*---------------------------------------------------------------------\
| Output: Modifies: readNodeStack to point to next node in stack
\---------------------------------------------------------------------*/
void popReadNodeStack(
struct readNodeStack **readStackAry /*readInfo Array (stack) to pop*/
) /*Sets readNodeStack to next readInfo node in stack*/
{ /*popReadNodeStack*/
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Fun-4 Sec-1 TOC: frees readNodeStack & returns next node in stack
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
*readStackAry = (*readStackAry) - 1;/* - sizeof(readNodeStack);*/
return;
} /*popReadNodeStack*/
/*---------------------------------------------------------------------\
| Output:
| returns: bigNum structer with the converted big number
| returns: 0 if memory alloaction failed
\---------------------------------------------------------------------*/
struct bigNum * makeBigNumStruct(
char *cnvtCStr,/*C-string to convert hex elements to big number*/
const unsigned int *lenCStrUInt /*Length of cString to convert*/
) /*Converts hex characters in c-string to a bigNum struct*/
{ /*makeBigNumStruct*/
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Fun-5 Sec-1 Sub-1 TOC: makeBigNumStruct
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
struct bigNum *idBigNum = malloc(sizeof(struct bigNum));
if(idBigNum == 0) /*Memory allocation error*/
return 0;
idBigNum->lenAllElmChar = 0; /*tell to reallocate memory*/
idBigNum->bigNumAryIOrL = 0; /*Just in case realloc needs*/
/*Convert the c-string to a big number*/
strToBackwardsBigNum(idBigNum, cnvtCStr, lenCStrUInt);
if(idBigNum->lenUsedElmChar == 0)
{ /*If memory allocation failed*/
freeBigNumStruct(&idBigNum); /*Errored out, so no longer need*/
return 0;
} /*If memory allocation failed*/
return idBigNum;
} /*makeBigNumStruct*/
/*---------------------------------------------------------------------\
| Output:
| Modifies: idBigNum to hold the converted big number.
| - Sets idBigNum->lenUsedULng to 0 if memory reallocation failed
\---------------------------------------------------------------------*/
void strToBackwardsBigNum(
struct bigNum *idBigNum, /*Holds the output big number*/
char *cnvtCStr, /*C-string to convert to large number*/
const unsigned int *lenCStrUInt
) /*Flips c-string & converts to big number*/
{ /*strToBackwardsBigNum*/
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
' Fun-6 TOC: strToGackwardsBigNum
' fun-6 sec-1: Variable declerations
' fun-6 sec-2: Resize big number array if needed
' fun-6 sec-3: Convert string to big number (work backwards)
\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Fun-6 Sec-1: Variable declerations
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
char charBit = 0; /*Number bits to shift*/
long *elmILPtr = 0;
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Fun-6 Sec-2: Resize big number array if needed
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
/*Get the number of limbs needed to store the number*/
idBigNum->lenUsedElmChar = 1 + (*lenCStrUInt / defBitsPerLimb);
/*+ 1 to handle truncation by division*/
/*Make sure have an array large enough to store number*/
if(idBigNum->lenAllElmChar < idBigNum->lenUsedElmChar)
{ /*If I need to make the array biger*/
if(idBigNum->bigNumAryIOrL == 0)
idBigNum->bigNumAryIOrL =
malloc(sizeof(long) *idBigNum->lenUsedElmChar);
else
idBigNum->bigNumAryIOrL =
realloc(
idBigNum->bigNumAryIOrL,
sizeof(long) * idBigNum->lenUsedElmChar
); /*Need to reallocate memory*/
if(idBigNum->bigNumAryIOrL == 0)
{ /*If memory reallocation failed*/
idBigNum->lenUsedElmChar = 0;
return;
} /*If memory reallocation failed*/
idBigNum->lenAllElmChar = idBigNum->lenUsedElmChar;
} /*If I need to make the array biger*/
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Fun-6 Sec-3: Convert string to big number (work backwards)
\>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
idBigNum->lenUsedElmChar = 0;
idBigNum->totalL = 0;
do { /*While there are array elements to fill*/
/*Graph unsigned long element working on*/
elmILPtr =idBigNum->bigNumAryIOrL+ idBigNum->lenUsedElmChar;
(idBigNum->lenUsedElmChar)++;/*Track number ULngs used*/
*elmILPtr = 0;
charBit = 0;
while(charBit < defMaxDigPerLimb)
{ /*while their are empty bits in the current big number limb*/
if(hexTblCharAry[(unsigned char) *cnvtCStr] & 64)
break; /*If have finshed converting the hex string*/
if(!(hexTblCharAry[(unsigned char) *cnvtCStr] & 32))
{ /*If is a hex character*/
*elmILPtr = *elmILPtr << defBitsPerChar;
*elmILPtr+=hexTblCharAry[(unsigned char) *cnvtCStr];
++charBit;
} /*If is a hex character*/
++cnvtCStr; /*move to next character in id*/
} /*while their are empty bits in the current big number limb*/
/*Only used in speed setting*/
#if defOSBit == 64
idBigNum->totalL +=
*((int*) elmILPtr) + *(((int*) elmILPtr) + 1);
#elif defOSBit == 32
idBigNum->totalL +=
*((short*) elmILPtr)+ *(((short*) elmILPtr)+1);
#elif defOSBit == 16
idBigNum->totalL +=
*((char*) elmILPtr) + *(((char*) elmILPtr) +1);
#else
#error System does not support 16 bit integers
#endif
} while((unsigned char) *cnvtCStr > 32);
/*While still on the read name part of header*/
return;
} /*strToBackwardsBigNum*/
/*---------------------------------------------------------------------\
| Output:
| frees: idBigNum & sets pointer to 0
\---------------------------------------------------------------------*/
void freeBigNumStruct(
struct bigNum **idBigNum /*Address to bigNum structer to free*/
) /*Frees a bigNum struct from memory*/
{ /*freeBigNumStruct*/
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Fun-7 Sec-1 Sub-1 TOC: freeBigNumStruct
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
free((*idBigNum)->bigNumAryIOrL); /*Should be 0 or on heap*/
free(*idBigNum);
*idBigNum = 0; /*Make sure user can never use again*/
return;
} /*freeBigNumStruct*/
/*---------------------------------------------------------------------\
| Output:
| Returns:
| - 0 if both equal,
| - < 0 if query is smaller than subject
| - > 0 if qeury is bigger than subject
\---------------------------------------------------------------------*/
long cmpBigNums(
struct bigNum *bigNumQ, /*Query to compare against*/
struct bigNum *bigNumS /*Subject to compare to*/
) /*Compares bigNumQ to bigNumS to see if equal, >, or <*/
{ /*cmpBigNums*/
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Fun-8 Sec-1 Sub-1 TOC: cmpBigNums
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
if(bigNumQ->totalL != bigNumS->totalL)
return bigNumQ->totalL - bigNumS->totalL;
/*If one stuct has more elements*/
if(bigNumQ->lenUsedElmChar != bigNumS->lenUsedElmChar)
return bigNumQ->lenUsedElmChar - bigNumS->lenUsedElmChar;
for(short sElm = bigNumQ->lenUsedElmChar - 1; sElm > -1; --sElm)
{ /*For all unsinged longs in the big number*/
if(*(bigNumQ->bigNumAryIOrL +sElm)
!= *(bigNumS->bigNumAryIOrL +sElm)
) return
*(bigNumQ->bigNumAryIOrL + sElm) -
*(bigNumS->bigNumAryIOrL + sElm);
} /*For all unsinged longs in the big number*/
return 0;
} /*cmpBigNums*/
/*---------------------------------------------------------------------\
| Output:
| Modifies:
| - EndNameCStr to point to '\n', ' ', & '\t' at end of read name
| - LenIdULng to hold the length of the read id
| - LenInputInt to hold length of input buffer
| Returns:
| - 0 if fails or end of file (lenIdULng < buffSizeInt)
| - pointer to struct with bigNum struct having converted read id
\---------------------------------------------------------------------*/
struct readInfo * cnvtIdToBigNum(
char *bufferCStr, /*buffer to hold fread input (can have data)*/
uint32_t buffSizeInt, /*Size of buffer to work on*/
char **endCStr, /*Points to start of id, will point to end*/
uint64_t *lenInputULng, /*Length of input from fread*/
unsigned char *lenBigNumChar, /*Holds size to make bigNumber*/
FILE *idFILE /*Fastq file to get data from*/
) /*Converts read id to bigNum read id, will grab new file input*/
{ /*cnvtIdToBigNum*/
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
' Fun-9 cnvtIdToBigNum TOC:
' fun-9 sec-1: Variable declerations
' fun-1 sec-2: Initalize readInfo & bigNum structs
' fun-1 sec-3: Move to first character on header
\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Fun-9 Sec-1: Variable declerations
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
unsigned char charBit = 0;
struct bigNum *idBigNum = malloc(sizeof(struct bigNum));
struct readInfo *readNode = malloc(sizeof(struct readInfo));
long *elmILPtr = 0;
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Fun-9 Sec-2: Initalize readInfo & bigNum structs
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
if(idBigNum == 0 || readNode == 0)
{ /*If memory allocation failed*/
if(idBigNum != 0) free(idBigNum);
if(readNode != 0) free(readNode);
*lenInputULng = 0; /*Make sure user detects failure*/
return 0;
} /*If memory allocation failed*/
readNode->balanceChar = 0;
readNode->leftChild = 0;
readNode->rightChild = 0;
readNode->idBigNum = idBigNum;
idBigNum->lenUsedElmChar = 0;
idBigNum->totalL = 0;
idBigNum->bigNumAryIOrL = malloc(sizeof(long)*(*lenBigNumChar));
idBigNum->lenAllElmChar = *lenBigNumChar;
if(idBigNum->bigNumAryIOrL == 0)
{ /*If memory reallocation failed*/
*lenInputULng = 0; /*Make sure user detects failure*/
free(idBigNum);
free(readNode);
return 0;
} /*If memory reallocation failed*/
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Fun-9 Sec-3: Move to first character on header
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
if(**endCStr == '\n') (*endCStr)++;
if(**endCStr == '\0')
{ /*If at the end of the buffer, but not at start of read*/
if(*lenInputULng < buffSizeInt)
{ /*If at end of file*/
free(idBigNum->bigNumAryIOrL);
free(idBigNum);
free(readNode);
return 0; /*Done with file*/
} /*If at end of file*/
*lenInputULng = fread(
bufferCStr,
sizeof(char),
buffSizeInt,
idFILE
); /*Read in more of the file*/
*endCStr = bufferCStr;
} /*If at the end of the buffer, but not at start of read*/
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Fun-9 Sec-4: Convert string to big number
^ fun-9 sec-4 sub-1: Check if need to resize the long array
^ fun-9 sec-4 sub-2: Convert characters until long is full
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
/******************************************************************\
* Fun-9 Sec-4 Sub-1: Check if need to resize the long array
\******************************************************************/
do { /*While still on the read name part of header*/
if(idBigNum->lenUsedElmChar >= idBigNum->lenAllElmChar)
{ /*If need to reallocate memory*/
(idBigNum->lenAllElmChar)++;
(*lenBigNumChar)++;
idBigNum->bigNumAryIOrL =
realloc(
idBigNum->bigNumAryIOrL,
sizeof(long) * idBigNum->lenAllElmChar
); /*Rellocate memory for the array*/
if(idBigNum == 0)
{ /*If memory allocation failed*/
free(idBigNum->bigNumAryIOrL);
free(idBigNum);
free(readNode);
*lenInputULng = 0; /*Make sure user detects failure*/
return 0;
} /*If memory allocation failed*/
} /*If need to reallocate memory*/
elmILPtr =
idBigNum->bigNumAryIOrL
+ idBigNum->lenUsedElmChar;
/*Graph unsigned long element working on*/
(idBigNum->lenUsedElmChar)++; /*Track number of used longs*/
*elmILPtr = 0;
charBit = 0;
/***************************************************************\
* Fun-9 Sec-4 Sub-1: Convert characters until long is full
\***************************************************************/
while(charBit < defMaxDigPerLimb)
{ /*while their are empty bits in the current big number limb*/
if(hexTblCharAry[(unsigned char) **endCStr] & 64)
break; /*If finshed converting the c-string*/
if(!(hexTblCharAry[(unsigned char) **endCStr] & 32))
{ /*If was a hex character*/
*elmILPtr = *elmILPtr << defBitsPerChar;
*elmILPtr += hexTblCharAry[(unsigned char) **endCStr];
++charBit;
} /*If was a hex character*/
++(*endCStr);
if(**endCStr == '\0')
{ /*If ran out of buffer & need to read in more of file*/
if(*lenInputULng < buffSizeInt)
break; /*at end of file*/
*lenInputULng = fread(bufferCStr,
sizeof(char),
buffSizeInt,
idFILE
); /*Read in more of the file*/
*(bufferCStr + *lenInputULng) = '\0';/*make a c-string*/
*endCStr = bufferCStr;
continue;
} /*If ran out of buffer & need to read in more of file*/
} /*while their are empty bits in the current big number limb*/
/*Only used in speed setting*/
#if defOSBit == 64
idBigNum->totalL +=
*((int*) elmILPtr) + *(((int*) elmILPtr) + 1);
#elif defOSBit == 32
idBigNum->totalL +=
*((short*) elmILPtr)+ *(((short*) elmILPtr)+1);
#elif defOSBit == 16
idBigNum->totalL +=
*((char*) elmILPtr) + *(((char*) elmILPtr) +1);
#else
#error System does not support 16 bit integers
#endif
} while((unsigned char) **endCStr > 32);
/*While still on the read name part of header*/
return readNode; /*Copied name sucessfully*/
} /*cnvtIdToBigNum*/
/* Ascii table
Dec Char Dec Char Dec Char Dec Char
--------- --------- --------- --------
0 NUL (null) 32 SPACE 64 @ 96 `
1 SOH (start of heading) 33 ! 65 A 97 a
2 STX (start of text) 34 " 66 B 98 b
3 ETX (end of text) 35 # 67 C 99 c
4 EOT (end of transmission) 36 $ 68 D 100 d
5 ENQ (enquiry) 37 % 69 E 101 e
6 ACK (acknowledge) 38 & 70 F 102 f
7 BEL (bell) 39 ' 71 G 103 g
8 BS (backspace) 40 ( 72 H 104 h
9 TAB (horizontal tab) 41 ) 73 I 105 i
10 LF (NL line feed, new line) 42 * 74 J 106 j
11 VT (vertical tab) 43 + 75 K 107 k
12 FF (NP form feed, new page) 44 , 76 L 108 l
13 CR (carriage return) 45 - 77 M 109 m
14 SO (shift out) 46 . 78 N 110 n
15 SI (shift in) 47 / 79 O 111 o
16 DLE (data link escape) 48 0 80 P 112 p
17 DC1 (device control 1) 49 1 81 Q 113 q
18 DC2 (device control 2) 50 2 82 R 114 r
19 DC3 (device control 3) 51 3 83 S 115 s
20 DC4 (device control 4) 52 4 84 T 116 t
21 NAK (negative acknowledge) 53 5 85 U 117 u
22 SYN (synchronous idle) 54 6 86 V 118 v
23 ETB (end of trans. block) 55 7 87 W 119 w
24 CAN (cancel) 56 8 88 X 120 x
25 EM (end of medium) 57 9 89 Y 121 y
26 SUB (substitute) 58 : 90 Z 122 z
27 ESC (escape) 59 ; 91 [ 123 {
28 FS (file separator) 60 < 92 \ 124 |
29 GS (group separator) 61 = 93 ] 125 }
30 RS (record separator) 62 > 94 ^ 126 ~
31 US (unit separator) 63 ? 95 _ 127 DEL
*/