include stdlib include stdio include string int getMETAOffset FILE fil

  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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int getMETAOffset(FILE *file) {
char str[255] = "\0", *p;
fseek(file, 258, SEEK_SET);
int c, i = 0;
do {
//printf("POS %li\n", ftell(file));
c = fgetc(file);
//printf("CHAR# %i '%c'\n", c, c);
if (c > 255)
break;
//printf("POS %li ", ftell(file));
//printf("1 %s\n", str);
str[i] = c;
//printf("%s\n", str);
p = strstr(str, "META-");
//printf("P %s", p);
if (p != NULL && c == ';') {
break;
}
//printf("\n");
i++;
} while (c != EOF);
//printf("MO %s\n", p);
p += sizeof ("META-") - 1;
//printf("MO %s\n", p);
p[sizeof (p) + 2] = '\0';
//printf("MO %s\n", p);
return atoi(p);
}
char ** getData(FILE *file, int metaOffset) {
char **data = NULL;
int c, i=0, j=0, end;
if(metaOffset < 1)
return NULL;
fseek(file, metaOffset, SEEK_SET);
//data = (char **)malloc(1*sizeof(char *));
do{
char *opt;
end = (i+1)*sizeof(char *);
data = realloc(data, end+1);
if (data == NULL){
return NULL;
}
c = fgetc(file);
if(c > 255)
continue;
//printf("|%c|", c);
if(c == ','){
opt[j] = '\0';
printf("\nopt %s |i %i\n", opt, i);
j=0;
//printf("SZ data %i", sizeof(data));
//printf("SZ opt %i", sizeof(opt));
data[i] = strdup(opt);
//if(i>1){
// printf("\ndata %i %i\n", (int)data[i-1], (int)data[i]);
//} else {
printf("\ndata %i\n", (int)data[i]);
//}
//opt = data[i]+1;
i++;
}else{
opt[j] = c;
j++;
}
} while (c != EOF);
end = (i+1)*sizeof(char *);
printf("end %i", end);
data[end] = "END";
return data;
}
int main(int argc, char **argv) {
if (argc < 2) {
printf("Usage: %s filename\n", argv[0]);
return 1;
} else {
int i = 0;
char *filename;
filename = argv[1];
printf("Filename is %s\n", filename);
FILE *file;
if ((file = fopen(filename, "r")) == NULL) {
fprintf(stderr, "Cannot open %s\n", filename);
return 2;
} else {
int metaOffset = getMETAOffset(file);
printf("metaOffset %i\n", metaOffset);
//void *data;
//data = (char *)calloc(10, 255);
char **data;
data = getData(file, metaOffset);
if(data == NULL){
return 2;
}
char *c;
printf("%i\n", sizeof(data));
do{
c = (char *)data[i];
printf(">> %s\n", c);
//i++;
} while(c != "END");
fclose(file);
}
return 0;
}
}