UNIVERS  15.3
UNIVERS base processing software API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cust_stream.H
1 /* cust_stream.H */
2 /* $Id: cust_stream.H,v 1.2 2002/09/03 15:30:37 ibadm Exp $ */
3 #ifndef __cust_stream_H
4 #define __cust_stream_H
5 
6 /***********************************************************************
7  * Setup new directory for stream I/O.
8  */
9 template<class T>
10 void
11 CubeStorage<T>::stream_dir (const char* dir)
12 {
13  if(NULL != szStreamDir)
14  {
15  delete[] szStreamDir;
16  szStreamDir = NULL;
17  }
18  szStreamDir = new char[strlen(dir) + 1];
19  if(NULL == szStreamDir)
20  Throw(ERROR__ALLOC);
21  strcpy(szStreamDir, dir);
22 
23  if(0 != mkdir(szStreamDir, 0777))
24  {
25  if(EEXIST != errno)
26  Throw(ERROR__IO);
27  }
28 }
29 
30 
31 /***********************************************************************
32  * Get name of directory for stream I/O.
33  */
34 template<class T>
35 const char*
37 {
38  return szStreamDir;
39 }
40 
41 
42 /***********************************************************************
43  * Put actual contents to stream with given item index
44  */
45 template<class T>
46 void
48 {
49  if(NULL == szStreamDir)
50  Throw(ERROR__NULL);
51 
52  int fildes;
53 
54  char *szStreamFile = new char[strlen(szStreamDir) + 20 + 5];
55  sprintf(szStreamFile, "%s/%d.css", szStreamDir, ii);
56 
57  fildes = open(szStreamFile, O_CREAT|O_RDWR, 0666);
58  if(-1 == fildes)
59  {
60  delete[] szStreamFile;
61  Throw(ERROR__OPEN_FILE);
62  }
63 
64  Try{
65  int mem_size = items_number(nDim, pDim) * sizeof(T);
66 
67  stream_put_header(fildes);
68 
69  if(mem_size != write(fildes, pStorage, mem_size))
70  Throw(ERROR__WRITE);
71  }Catch{
72  close(fildes);
73  delete[] szStreamFile;
74  Throw(exCode);
75  }
76 
77  close(fildes);
78  delete[] szStreamFile;
79 }
80 
81 /***********************************************************************
82  * Replace actual contents from stream at given item index
83  */
84 template<class T>
85 void
87 {
88  if(NULL == szStreamDir)
89  Throw(ERROR__NULL);
90 
91  int fildes;
92 
93  char *szStreamFile = new char[strlen(szStreamDir) + 20 + 5];
94  sprintf(szStreamFile, "%s/%d.css", szStreamDir, ii);
95 
96  fildes = open(szStreamFile, O_RDONLY);
97  if(-1 == fildes)
98  {
99  delete[] szStreamFile;
100  Throw(ERROR__OPEN_FILE);
101  }
102 
103  Try{
104  int mem_size = items_number(nDim, pDim) * sizeof(T);
105 
106  stream_get_header(fildes);
107 
108  if(mem_size != read(fildes, pStorage, mem_size))
109  Throw(ERROR__READ);
110  }Catch{
111  close(fildes);
112  delete[] szStreamFile;
113  Throw(exCode);
114  }
115 
116  close(fildes);
117  delete[] szStreamFile;
118 }
119 
120 
121 /***********************************************************************
122  * Put header to stream
123  */
124 template<class T>
125 void
127 {
129 
130  memcpy(hdr.signature, CSS_SIGNATURE, strlen(CSS_SIGNATURE));
131  hdr.magic = 0x12345678;
132  memset(hdr.item_order, 0, 4);
133  switch(eItemOrder)
134  {
135  case AlaC:
136  hdr.item_order[0] = 'c';
137  break;
138  case AlaFortran:
139  hdr.item_order[0] = 'f';
140  break;
141  }
142  hdr.dim_num = nDim;
143  hdr.item_size = sizeof(T);
144 
145  if(sizeof(hdr) != write(fildes, &hdr, sizeof(hdr)))
146  Throw(ERROR__WRITE);
147 }
148 
149 
150 /***********************************************************************
151  * Get header from stream (simply skip it)
152  */
153 template<class T>
154 void
156 {
158  Int4 magic = 0x12345678;
159 
160  if(sizeof(hdr) != read(fildes, &hdr, sizeof(hdr)))
161  Throw(ERROR__READ);
162 
163  if(memcmp(hdr.signature, CSS_SIGNATURE, strlen(CSS_SIGNATURE)))
164  /* bad file format or unknown version */
165  Throw(ERROR__BAD_PARAMS);
166 
167  if(hdr.magic != 0x12345678)
168  /* another byte order */
169  Throw(ERROR__BAD_PARAMS);
170 
171  switch(eItemOrder)
172  {
173  case AlaC:
174  if(hdr.item_order[0] != 'c')
175  /* another item order */
176  Throw(ERROR__BAD_PARAMS);
177  break;
178  case AlaFortran:
179  if(hdr.item_order[0] != 'f')
180  /* another item order */
181  Throw(ERROR__BAD_PARAMS);
182  break;
183  }
184 
185  if(hdr.dim_num != nDim)
186  /* another number of dimensions */
187  Throw(ERROR__BAD_PARAMS);
188 
189  if(hdr.item_size != sizeof(T))
190  /* another size of item */
191  Throw(ERROR__BAD_PARAMS);
192 }
193 
194 
195 #endif /* cust_stream.H */
Definition: CubeStorage.H:48
virtual void stream_put(int ii)
Definition: cust_stream.H:47
const char * stream_dir() const
Definition: cust_stream.H:36
virtual void stream_get(int ii)
Definition: cust_stream.H:86
Definition: cust_defs.h:36