source: util/ReadSrcIon.c@ 725869

Last change on this file since 725869 was a0bcf1, checked in by Frederik Heber <heber@…>, 17 years ago

-initial commit
-Minimum set of files needed from ESPACK SVN repository
-Switch to three tantamount package parts instead of all relating to pcp (as at some time Ralf's might find inclusion as well)

  • Property mode set to 100644
File size: 5.0 KB
Line 
1/*
2 Project: CP
3 Jan Hamaekers
4 1999
5
6 File: CreateH2.c
7
8*/
9/*$Id: ReadSrcIon.c,v 1.2 2007-09-03 16:28:58 heber Exp $*/
10
11#include<stdlib.h>
12#include<stdio.h>
13#include<math.h>
14#include<string.h>
15
16#define NDIM (3)
17#define NDIM_NDIM (NDIM*NDIM)
18
19#define MAXDUMMYSTRING (199)
20
21/* Verschiedene Fehlertypen */
22enum Errors { SomeError, FileOpenParams, InitReading, MallocError};
23/* SomeError: Falls man noch zu faul ist */
24
25/* Behandelt aufgetretene Fehler. error ist der Fehlertyp(enum Errors)
26 void *SpecialData ist ein untypisierter Zeiger auf Spezielle Daten zur Fehlerbehandlung.
27 Man koennte auch noch einen Zeiger auf eine Funktion uebergeben */
28void Error(enum Errors error, const void *SpecialData);
29
30
31/* Behandelt aufgetretene Fehler. error ist der Fehlertyp(enum Errors)
32 void *SpecialData ist ein untypisierter Zeiger auf Spezielle Daten zur Fehlerbehandlung.
33 Man koennte auch noch einen Zeiger auf eine Funktion uebergeben */
34void Error(enum Errors error, const void *SpecialData)
35{
36 const char *const error_msg[] = {
37 "SomeError",
38 "FileOpenParams\nUnable to open parameter file",
39 "InitReading\nUnable to interpret the parameter file",
40 "MallocError\nUnable to allocate memory!"
41 };
42 fprintf(stderr, "It has occured an error (%i): %s\n", error, error_msg[error]);
43 switch (error) {
44 case SomeError:
45 case MallocError:
46 if (SpecialData) fprintf(stderr, "%s\n", (const char*)SpecialData);
47 break;
48 default:
49 break;
50 }
51
52 exit(EXIT_FAILURE); /* exit schreibt sowieso alle Dateipuffer aus */
53}
54
55/* Eigene malloc, die bei einem Fehler an Error output uebergibt.
56 */
57void* Malloc(size_t size, const char* output)
58{
59 void* dummy = malloc(size);
60 if (!dummy)
61 Error(MallocError, output);
62 return dummy;
63}
64
65/* Eigene malloc, die bei einem Fehler erzeugten (ein int rein) output an Error uebergibt */
66void* Malloci(size_t size, const char* output, int i)
67{
68 char dummyoutput[MAXDUMMYSTRING];
69 void* dummy = malloc(size);
70 if (!dummy) {
71 sprintf(dummyoutput,output,i);
72 Error(MallocError, dummyoutput);
73 }
74 return dummy;
75}
76
77/* Eigene malloc, die bei einem Fehler erzeugten (zwei int rein) output an Error uebergibt */
78void* Mallocii(size_t size, const char* output, int i, int j)
79{
80 char dummyoutput[MAXDUMMYSTRING];
81 void* dummy = malloc(size);
82 if (!dummy) {
83 sprintf(dummyoutput,output,i,j);
84 Error(MallocError, dummyoutput);
85 }
86 return dummy;
87}
88
89void* Realloc(void* pointer, size_t size, const char* output)
90{
91 void *dummy = realloc(pointer, size);
92 if (!dummy)
93 Error(MallocError, output);
94 return dummy;
95}
96
97void* Realloci(void* pointer, size_t size, const char* output, int i)
98{
99 char dummyoutput[MAXDUMMYSTRING];
100 void *dummy = realloc(pointer, size);
101 if (!dummy) {
102 sprintf(dummyoutput, output, i);
103 Error(MallocError, dummyoutput);
104 }
105 return dummy;
106}
107
108void* Reallocii(void* pointer, size_t size, const char* output, int i, int j)
109{
110 char dummyoutput[MAXDUMMYSTRING];
111 void *dummy = realloc(pointer, size);
112 if (!dummy) {
113 sprintf(dummyoutput,output,i,j);
114 Error(MallocError, dummyoutput);
115 }
116 return dummy;
117}
118
119void Free (void *ptr) {
120 if (ptr) free(ptr);
121}
122
123int OpenFile(FILE** file, const char* suffix, const char* what)
124{
125 char* name; /* Zu erzeugender Dateiname */
126 name = (char*)
127 Malloc(3 + strlen(suffix) + 1,"OpenFile");
128 sprintf(name, "pcp%s", suffix);
129 *file = fopen(name, what);
130 if (*file == 0) {
131 fprintf(stderr,"\nError: Cannot open file: %s\n",name);
132 Free(name);
133 return(0);
134 } else {
135 fprintf(stderr,"File is open: %s\n",name);
136 Free(name);
137 return(1);
138 }
139}
140
141static const char suffixsrciondoc[] = ".srcion.doc";
142static const char suffixsrciondat[] = ".srcion.data";
143
144int main(int argc, char** argv) {
145 double data[2*NDIM];
146 int is,ia,i;
147 int Max_Types;
148 int *Max_IonsOfType;
149 double RealBasis[NDIM_NDIM];
150 FILE *SrcIonDoc, *SrcIonData;
151
152 OpenFile(&SrcIonDoc, suffixsrciondoc, "r");
153 if (fscanf(SrcIonDoc,"%i", &Max_Types) != 1)
154 Error(SomeError, "ReadSrcIons: read error");
155 fprintf(stdout, "Max_Types:\t%i\n",Max_Types);
156 Max_IonsOfType = Malloc(Max_Types*sizeof(int), "ReadSrcIons: Max_IonsOfType");
157 for (is=0; is < Max_Types; is++) {
158 if (fscanf(SrcIonDoc,"%i", &Max_IonsOfType[is]) != 1)
159 Error(SomeError, "ReadSrcIons: read error");
160 fprintf(stdout, "Max_IonsOfType[%i]:\t%i\n",is,Max_IonsOfType[is]);
161 }
162 fclose(SrcIonDoc);
163
164 OpenFile(&SrcIonData, suffixsrciondat, "rb");
165 if (fread(RealBasis, sizeof(double), (size_t)(NDIM_NDIM), SrcIonData) != NDIM_NDIM)
166 Error(SomeError, "ReadSrcIons: read error");
167 for (i=0; i < NDIM_NDIM; i++)
168 fprintf(stdout, "RealBasis[%i] = %e\n", i, RealBasis[i]);
169 for (is=0; is < Max_Types; is++) {
170 for (ia=0; ia < Max_IonsOfType[is]; ia++) {
171 if (fread(&data, sizeof(double), (size_t)(2*NDIM), SrcIonData) != 2*NDIM)
172 Error(SomeError, "ReadSrcIons: read error");
173 fprintf(stdout,"%i\t%i\t%e\t%e\t%e\t%e\t%e\t%e\n",is,ia,data[0],data[1],data[2],data[3],data[4],data[5]);
174 }
175 }
176 fclose(SrcIonData);
177 Free(Max_IonsOfType);
178 return(0);
179}
Note: See TracBrowser for help on using the repository browser.