basic.C
2.6 KB
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
/*******************************************************************/
/* */
/* FILE basic.C */
/* MODULE basic */
/* PROGRAM SFST */
/* AUTHOR Helmut Schmid, IMS, University of Stuttgart */
/* */
/* PURPOSE */
/* */
/*******************************************************************/
#include <stdlib.h>
#include <string.h>
#include "basic.h"
bool Switch_Bytes=false;
/*******************************************************************/
/* */
/* fst_strdup */
/* */
/*******************************************************************/
char* fst_strdup(const char* pString)
{
char* pStringCopy = (char*)malloc(strlen(pString) + 1);
if (pStringCopy == NULL) {
fprintf(stderr, "\nError: out of memory (malloc failed)\naborted.\n");
exit(1);
}
strcpy(pStringCopy, pString);
return pStringCopy;
}
/*******************************************************************/
/* */
/* read_string */
/* */
/*******************************************************************/
int read_string( char *buffer, int size, FILE *file )
{
for( int i=0; i<size; i++ ) {
int c=fgetc(file);
if (c == EOF || c == 0) {
buffer[i] = 0;
return (c==0);
}
buffer[i] = (char)c;
}
buffer[size-1] = 0;
return 0;
}
/*******************************************************************/
/* */
/* read_num */
/* */
/*******************************************************************/
size_t read_num( void *p, size_t n, FILE *file )
{
char *pp=(char*)p;
size_t result=fread( pp, 1, n, file );
if (Switch_Bytes) {
size_t e=n/2;
for( size_t i=0; i<e; i++ ) {
char tmp=pp[i];
pp[i] = pp[--n];
pp[n] = tmp;
}
}
return result;
}