forked from jhamman/DHSVM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Files.c
executable file
·136 lines (116 loc) · 3.42 KB
/
Files.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
/*
* SUMMARY: Files.c - File functions
* USAGE: Part of DHSVM
*
* AUTHOR: Bart Nijssen
* ORG: University of Washington, Department of Civil Engineering
* E-MAIL: [email protected]
* ORIG-DATE: Apr-96
* DESCRIPTION: File and I/O functions
* DESCRIP-END.
* FUNCTIONS: OpenFile()
* ScanInts()
* ScanFloats()
* SkipLines()
* SkipHeader()
* ScanDoubles()
* ScanUChars()
* COMMENTS:
* $Id: Files.c,v 1.4 2003/07/01 21:26:14 olivier Exp $
*/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "settings.h"
#include "data.h"
#include "DHSVMerror.h"
#include "functions.h"
#include "constants.h"
#include "fileio.h"
/*****************************************************************************
OpenFile()
*****************************************************************************/
void OpenFile(FILE ** FilePtr, char *FileName, char *Mode, uchar OverWrite)
{
struct stat FileInfo;
/* if OverWrite is FALSE and the Mode is not read, check whether the file
already exists */
if (!OverWrite && strstr(Mode, "w")) {
if (!(stat(FileName, &FileInfo)))
ReportError(FileName, 4);
}
if (!(*FilePtr = fopen(FileName, Mode)))
ReportError(FileName, 3);
}
/*****************************************************************************
ScanUChars()
*****************************************************************************/
uchar ScanUChars(FILE * FilePtr, uchar * X, int N)
{
int Ctr;
char Str[2];
for (Ctr = 0; Ctr < N; Ctr++) {
if (fscanf(FilePtr, "%1s", Str) != 1)
break;
X[Ctr] = Str[0];
}
return Ctr;
}
/*****************************************************************************
ScanInts()
*****************************************************************************/
int ScanInts(FILE * FilePtr, int *X, int N)
{
int Ctr;
for (Ctr = 0; Ctr < N; Ctr++) {
if (fscanf(FilePtr, "%d", &X[Ctr]) != 1)
break;
}
return Ctr;
}
/*****************************************************************************
ScanFloats()
*****************************************************************************/
int ScanFloats(FILE * FilePtr, float *X, int N)
{
int Ctr;
for (Ctr = 0; Ctr < N; Ctr++) {
if (fscanf(FilePtr, "%f", &X[Ctr]) != 1)
break;
}
return Ctr;
}
/*****************************************************************************
ScanDoubles()
*****************************************************************************/
int ScanDoubles(FILE * FilePtr, double *X, int N)
{
int Ctr;
for (Ctr = 0; Ctr < N; Ctr++) {
if (fscanf(FilePtr, "%lf", &X[Ctr]) != 1)
break;
}
return Ctr;
}
/*****************************************************************************
SkipLines()
*****************************************************************************/
void SkipLines(FILES * InFile, int NLines)
{
int Ctr;
char str[BUFSIZE + 1];
for (Ctr = 0; Ctr < NLines; Ctr++) {
if (!fgets(str, BUFSIZE, InFile->FilePtr))
ReportError(InFile->FileName, 5);
}
}
/*****************************************************************************
SkipHeader()
*****************************************************************************/
void SkipHeader(FILES * InFile, int NLines)
{
SkipLines(InFile, NLines);
}