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
|
/* Le code manquant est ici :
http://delahaye.emmanuel.free.fr/clib/
Notamment ed/src/sys.c pour SYS_dump()
*/
#include "ed/inc/sys.h"
#include<stdio.h>
typedef unsigned long pos_t;
int get_tag (char const *fname, char const *tag, pos_t * p_pos)
{
int found = 0;
FILE *fp = fopen (fname, "rb");
if (fp != NULL)
{
enum
{ STS_INIT, STS_IN, STS_OUT }
sts = STS_INIT;
size_t const tag_len = strlen (tag);
unsigned long n = 0;
int c;
size_t i_tag = 0;
pos_t pos = 0;
while (!found && (c = fgetc (fp)) != EOF)
{
switch (sts)
{
case STS_INIT:
if (c == tag[i_tag])
{
pos = n;
i_tag++;
sts = STS_IN;
}
break;
case STS_IN:
if (c == tag[i_tag])
{
i_tag++;
if (i_tag == tag_len)
{
found = 1;
if (p_pos != NULL)
{
*p_pos = pos;
}
}
}
else
{
i_tag = 0;
sts = STS_OUT;
}
break;
case STS_OUT:
if (c == tag[i_tag])
{
pos = n;
i_tag++;
sts = STS_IN;
}
break;
}
n++;
}
fclose (fp);
printf ("%lu bytes read\n", n);
}
else
{
perror (fname);
}
return found;
}
void fdump (char const *fname, pos_t pos, size_t len)
{
FILE *fp = fopen (fname, "rb");
if (fp != NULL)
{
char *data = malloc (len);
if (data != NULL)
{
fseek (fp, pos, SEEK_SET);
fread (data, len, 1, fp);
fclose (fp);
SYS_dump (data, len);
free (data);
}
}
else
{
perror (fname);
}
}
int main (void)
{
char const *fname = "fichier.emi";
char const *tag = "Acquire HAADF";
pos_t pos;
int found = get_tag (fname, tag, &pos);
if (found)
{
fdump (fname, pos, 32);
}
else
{
printf ("The '%s' tag was not found\n", tag);
}
return 0;
} |