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
|
/**********************************/
/* C API for kdi file support */
/* author: A. Hebert (30/04/2002) */
/**********************************/
/*
Copyright (C) 2002 Ecole Polytechnique de Montreal
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
*/
#if defined(CRAY)
#define lnword 8
#else
#define lnword 4
#endif
#if !defined(MSDOS)
#include <unistd.h>
#endif
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include "kdi.h"
long long offset;
kdi_file * kdiop_c(char *nomC,int_32 iactio)
{
kdi_file *my_file;
my_file = (kdi_file *) malloc(sizeof(*my_file));
strcpy(my_file->nom,nomC);
my_file->fd = NULL;
if (iactio == 0) {
FILE *file ;
long fd;
if ( ( file = fopen(nomC,"rb") ) != NULL ) {
fclose(file) ;
perror ("open error 0 in kdiop_c ");
return NULL;
}
fd = creat(nomC,0600);
close(fd);
my_file->fd = fopen(nomC,"r+b");
} else if (iactio == 1) {
FILE *file ;
if ( ( file = fopen(nomC,"rb") ) == NULL ) {
perror ("open error 1 in kdiop_c ");
return NULL;
}
fclose(file) ;
my_file->fd = fopen(nomC,"r+b");
} else if (iactio == 2) {
FILE *file ;
if ( ( file = fopen(nomC,"rb") ) == NULL ) {
perror ("open error 2 in kdiop_c ");
return NULL;
}
my_file->fd = file;
} else {
return NULL;
}
if ( my_file->fd == NULL ) {
perror ("open error 3 in kdiop_c ");
return NULL;
}
return my_file;
}
int_32 kdiput_c(kdi_file *my_file,int_32 *data,int_32 iofset,int_32 length)
{
int_32 irc=0;
offset=(long long)iofset*lnword;
if (my_file == NULL) {
irc = -1;
} else if (fseek(my_file->fd,offset,0) >= 0) {
long long n, iof=0;
while ((n = fwrite(&data[iof],lnword,length,my_file->fd)) < length-iof) {
if (n < 0) return n-1;
iof+=n;
}
} else {
irc = -3;
}
return irc;
}
int_32 kdiget_c(kdi_file *my_file,int_32 *data,int_32 iofset,int_32 length)
{
int_32 irc=0;
offset=(long long)iofset*lnword;
if (my_file == NULL) {
irc = -1;
} else if (fseek(my_file->fd,offset,0) >= 0) {
long long n, iof=0;
while ((n = fread(&data[iof],lnword,length,my_file->fd)) < length-iof) {
if (n == 0) return -4;
if (n < 0) return n-1;
iof+=n;
}
} else {
irc = -3;
}
return irc;
}
int_32 kdicl_c(kdi_file *my_file,int_32 istatu)
{
long irc;
if (my_file == NULL) {
irc = -1;
} else if (istatu == 1) {
irc = fclose(my_file->fd);
free(my_file);
} else if (istatu == 2) {
irc = fclose(my_file->fd);
if (irc != 0 ) {
perror ("close error 1 in kdicl_c ");
return irc;
}
irc = remove(my_file->nom);
free(my_file);
} else {
irc = -999;
}
my_file = NULL;
if (irc != 0) perror ("close error 2 in kdicl_c ");
return irc;
}
|