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