| 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 |
#include <stdlib.h> |
| 36 |
#include <stdio.h> |
| 37 |
#include <sys/mman.h> |
| 38 |
#include <sys/stat.h> |
| 39 |
#include <sys/types.h> |
| 40 |
#include <fcntl.h> |
| 41 |
#include <unistd.h> |
| 42 |
#include <string.h> |
| 43 |
|
| 44 |
#include "ioMarkov.h" |
| 45 |
|
| 46 |
|
| 47 |
char readImprobaStructFromBinFile(markovImprobaStruct** mis, char* path, char* fileName){ |
| 48 |
struct stat sb; |
| 49 |
int binFile; |
| 50 |
char buffer[256]; |
| 51 |
|
| 52 |
if (fileName != NULL){ |
| 53 |
sprintf(buffer, "%s/%s", path, fileName); |
| 54 |
binFile = open(buffer, O_RDONLY); |
| 55 |
} |
| 56 |
else{ |
| 57 |
sprintf(buffer, "%s/%s", path, DEFAULT_MARKOV_FILE_NAME); |
| 58 |
binFile = open(buffer, O_RDONLY); |
| 59 |
} |
| 60 |
|
| 61 |
if (binFile == -1){ |
| 62 |
return 0; |
| 63 |
} |
| 64 |
|
| 65 |
if (fstat(binFile, &sb) == -1){ |
| 66 |
close(binFile); |
| 67 |
return 0; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
if (sb.st_size != sizeof(markovImprobaStruct)){ |
| 72 |
close(binFile); |
| 73 |
return 0; |
| 74 |
} |
| 75 |
|
| 76 |
*mis = (markovImprobaStruct*)mmap(NULL, sizeof(markovImprobaStruct), PROT_READ, MAP_PRIVATE, binFile, 0); |
| 77 |
if (*mis == MAP_FAILED){ |
| 78 |
close(binFile); |
| 79 |
return 0; |
| 80 |
} |
| 81 |
close(binFile); |
| 82 |
return 1; |
| 83 |
} |
| 84 |
|
| 85 |
markovType fromStringMarkovType(const char* word, const int length){ |
| 86 |
if (!memcmp(word, "NO_MARK", length)){ |
| 87 |
return NO_MARK; |
| 88 |
} |
| 89 |
if (!memcmp(word, "MARK0", length)){ |
| 90 |
return MARK0; |
| 91 |
} |
| 92 |
if (!memcmp(word, "MARK1", length)){ |
| 93 |
return MARK1; |
| 94 |
} |
| 95 |
if (!memcmp(word, "MARK2", length)){ |
| 96 |
return MARK2; |
| 97 |
} |
| 98 |
return NO_MARK; |
| 99 |
} |