Pointwise Plugin SDK
apiCAEPUtils.cxx
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Pointwise Plugin utility functions
4  *
5  * Proprietary software product of Pointwise, Inc.
6  * Copyright (c) 1995-2018 Pointwise, Inc.
7  * All rights reserved.
8  *
9  ***************************************************************************/
10 
11 #include <string.h>
12 
13 #include "apiPWP.h"
14 #include "apiCAEPUtils.h"
15 #include "apiGridModel.h"
16 #include "pwpPlatform.h"
17 
18 
19 
22 {
23  CAEP_RTITEM *ret = 0;
24  PWP_UINT32 ii;
25  for (ii=0; ii < caepFormatCnt; ++ii) {
26  if (id == caepRtItem[ii].FormatInfo.id) {
27  ret = &(caepRtItem[ii]);
28  break;
29  }
30  }
31  return ret;
32 }
33 
34 
36 caeuFindFormatByName (const char name[])
37 {
38  CAEP_RTITEM *ret = 0;
39  PWP_UINT32 ii;
40  for (ii=0; ii < caepFormatCnt; ++ii) {
41  if (0 == strcmp(caepRtItem[ii].FormatInfo.name, name)) {
42  ret = &(caepRtItem[ii]);
43  break;
44  }
45  }
46  return ret;
47 }
48 
49 
52 {
53  PWP_BOOL ret = PWP_FALSE;
54  if (pRti) {
55  pRti->progTotal = 0;
56  pRti->progComplete = 0;
57  pRti->clocks[CAEPU_CLKS_PROGUPDATE] =
58  pRti->clocks[CAEPU_CLKS_PROGINIT] = clock();
59  pRti->opAborted = PWP_FALSE;
60  ret = PwuProgressBegin(pRti->pApiData->apiInfo.name, cnt);
61  pRti->opAborted |= !ret;
62  }
63  return ret;
64 }
65 
66 
69 {
70  PWP_BOOL ret = PWP_FALSE;
71  if (pRti && (total > 0)) {
72  pRti->progComplete = 0;
73  pRti->progTotal = total;
74  ret = !pRti->opAborted;
75  pRti->clocks[CAEPU_CLKS_BEGSTEP] = clock();
76  }
77  return ret;
78 }
79 
80 
83 {
84  PWP_BOOL ret = PWP_FALSE;
85  if (pRti && !pRti->opAborted) {
86  // send update a max of 2 times per second
87  const clock_t DELAY = (CLOCKS_PER_SEC / 2);
88  pRti->clocks[CAEPU_CLKS_PROGINCR] = clock();
89  ++pRti->progComplete;
90  if (DELAY <= CAEPU_RT_CLKS_DIFF(pRti, CAEPU_CLKS_PROGUPDATE,
92  ret = PwuProgressStatus (pRti->pApiData->apiInfo.name,
93  pRti->progComplete, pRti->progTotal);
94  pRti->clocks[CAEPU_CLKS_PROGUPDATE] =
95  pRti->clocks[CAEPU_CLKS_PROGINCR];
96  pRti->opAborted |= !ret;
97  }
98  else {
99  ret = PWP_TRUE;
100  }
101  }
102  return ret;
103 }
104 
105 
106 static void
107 sendDiffTimeMsg(CAEP_RTITEM *pRti, const char txt[], clock_t diff)
108 {
109  if (pRti) {
110  PWP_INT32 hours;
111  PWP_INT32 minutes;
112  PWP_INT32 seconds;
113  char msg[512];
114  char *p = msg;
115  if (txt && txt[0]) {
116  strcpy(p, txt);
117  strcat(p, ": ");
118  p += strlen(p);
119  }
120  CAEPU_CLKS_TO_HMS(diff, hours, minutes, seconds);
121  sprintf(p, "%ld:%02ld:%02ld (h:mm:ss) / %ld msec", (long)hours,
122  (long)minutes, (long)seconds, (long)CAEPU_CLKS_TO_MSECS(diff));
123  caeuSendDebugMsg(pRti, msg, 0);
124  }
125 }
126 
127 
128 static void
129 sendClkDiffTimeMsg(CAEP_RTITEM *pRti, const char txt[],
130  CAEPU_ENUM_CLOCKS startId, CAEPU_ENUM_CLOCKS endId)
131 {
132  if (pRti) {
133  sendDiffTimeMsg(pRti, txt,
134  CAEPU_RT_CLKS_DIFF(pRti, startId, endId));
135  }
136 }
137 
138 
139 PWP_BOOL
141 {
142  PWP_BOOL ret = PWP_FALSE;
143  if (pRti) {
144  pRti->clocks[CAEPU_CLKS_ENDSTEP] = clock();
145  pRti->progComplete = 0;
146  pRti->progTotal = 0;
148  pRti->opAborted |= !ret;
149  sendClkDiffTimeMsg(pRti, "Step time", CAEPU_CLKS_BEGSTEP,
151  }
152  return ret;
153 }
154 
155 
156 void
158 {
159  if (pRti) {
160  pRti->clocks[CAEPU_CLKS_PROGEND] = clock();
161  pRti->progTotal = 0;
162  pRti->progComplete = 0;
163  pRti->clocks[CAEPU_CLKS_PROGUPDATE] = 0;
164  PwuProgressEnd(pRti->pApiData->apiInfo.name, ok);
165  sendClkDiffTimeMsg(pRti, "Export time", CAEPU_CLKS_PROGINIT,
167  }
168 }
169 
170 
171 int
172 caeuFileClose(CAEP_RTITEM* pRti, const CAEP_WRITEINFO *pWriteInfo)
173 {
174  int ret = 0;
175  if (pRti) {
176  switch (pRti->FormatInfo.fileDest) {
178  if (pRti->fp) {
179  if (0 == pwpFileClose(pRti->fp)) {
180  if (pRti->opAborted) {
181  pwpFileDelete(pWriteInfo->fileDest);
182  }
183  else {
184  ret = 1;
185  }
186  }
187  pRti->fp = 0;
188  if (0 != pwpCwdPop()) {
189  ret = 0;
190  }
191  }
192  break;
193 
195  case PWP_FILEDEST_FOLDER:
196  /* restore cwd to the original folder.
197  */
198  if (0 == pwpCwdPop()) {
199  ret = 1;
200  }
201  break;
202  case PWP_FILEDEST_SIZE: // silence compiler warning
203  break;
204  }
205  }
206  return ret;
207 }
208 
209 
210 void
211 caeuSendDebugMsg(CAEP_RTITEM *pRti, const char txt[], PWP_UINT32 code)
212 {
213  if (0 != pRti) {
214  PwuSendDebugMsg(pRti->pApiData->apiInfo.name, txt, code);
215  }
216 }
217 
218 
219 void
220 caeuSendInfoMsg(CAEP_RTITEM *pRti, const char txt[], PWP_UINT32 code)
221 {
222  if (0 != pRti) {
223  PwuSendInfoMsg(pRti->pApiData->apiInfo.name, txt, code);
224  }
225 }
226 
227 
228 void
229 caeuSendWarningMsg(CAEP_RTITEM *pRti, const char txt[], PWP_UINT32 code)
230 {
231  if (0 != pRti) {
232  PwuSendWarningMsg(pRti->pApiData->apiInfo.name, txt, code);
233  }
234 }
235 
236 
237 void
238 caeuSendErrorMsg(CAEP_RTITEM *pRti, const char txt[], PWP_UINT32 code)
239 {
240  if (0 != pRti) {
241  PwuSendErrorMsg(pRti->pApiData->apiInfo.name, txt, code);
242  }
243 }
244 
245 
246 
247 PWP_BOOL
248 caeuPublishValueDefinition(const char key[], PWP_ENUM_VALTYPE type, const char value[],
249  const char access[], const char desc[], const char range[])
250 {
251  PWP_BOOL ret = PWP_FALSE;
252  ret = PwuPublishValueDefinition(CAEP_VALUE_GROUP, key, type, value, access, desc,
253  range);
254  return ret;
255 }
256 
257 
258 PWP_BOOL
259 caeuAssignInfoValue(const char key[], const char value[],
260  bool createIfNotExists)
261 {
262  return PwuAssignValueEnum(CAEP_INFO_GROUP, key, value,
263  createIfNotExists);
264 }
265 
266 
267 
270 {
271  PWP_ENDIANNESS ret = PwuGetOsEndianness(); // assume Native
272  const char *str = 0;
273  if (PwModGetAttributeString(model, "FileByteOrder", &str)) {
274  // one of: Native|Swap|LittleEndian|BigEndian
275  if (0 == strcmp(str, "Swap")) {
277  }
278  else if (0 == strcmp(str, "LittleEndian")) {
279  ret = PWP_ENDIAN_LITTLE;
280  }
281  else if (0 == strcmp(str, "BigEndian")) {
282  ret = PWP_ENDIAN_BIG;
283  }
284  }
285  return ret;
286 }
287 
288 
289 PWP_BOOL
291 {
292  PWP_BOOL ret;
293  return PwModGetAttributeBOOL(model, "BoundaryConditionsOnly", &ret) ? ret :
294  PWP_FALSE;
295 }
296 
297 
300 {
301  PWP_ENUM_ENCODING ret = PWP_ENCODING_SIZE;
302  const char *str = 0;
303  if (PwModGetAttributeString(model, "FileFormat", &str)) {
304  // one of: ASCII|Binary|Unformatted
305  if (0 == strcmp(str, "ASCII")) {
306  ret = PWP_ENCODING_ASCII;
307  }
308  else if (0 == strcmp(str, "Binary")) {
309  ret = PWP_ENCODING_BINARY;
310  }
311  else if (0 == strcmp(str, "Unformatted")) {
313  }
314  // else something very wrong
315  }
316  return ret;
317 }
318 
319 
322 {
323  PWP_ENUM_PRECISION ret = PWP_PRECISION_SIZE;
324  const char *str = 0;
325  if (PwModGetAttributeString(model, "FilePrecision", &str)) {
326  // one of: Single|Double
327  if (0 == strcmp(str, "Single")) {
328  ret = PWP_PRECISION_SINGLE;
329  }
330  else if (0 == strcmp(str, "Double")) {
331  ret = PWP_PRECISION_DOUBLE;
332  }
333  // else something very wrong
334  }
335  return ret;
336 }
337 
338 
339 PWP_BOOL
341 {
342  PWP_BOOL ret;
343  return PwModGetAttributeBOOL(model, "GridStructuredAsUnstructured", &ret)
344  ? ret : PWP_FALSE;
345 }
346 
347 
348 
349 static int
350 openFileName(CAEP_RTITEM* pRti, const CAEP_WRITEINFO *pWriteInfo)
351 {
352  int ret = 0;
353  if (pRti && pWriteInfo && pWriteInfo->fileDest && pWriteInfo->fileDest[0]) {
354  caeuFileClose(pRti, pWriteInfo); // sanity check
355  switch (pWriteInfo->encoding) {
356  case PWP_ENCODING_ASCII:
357  pRti->fp = pwpFileOpen(pWriteInfo->fileDest, (sysFILEMODE)(pwpWrite | pwpAscii));
358  break;
359  case PWP_ENCODING_BINARY:
360  pRti->fp = pwpFileOpen(pWriteInfo->fileDest, (sysFILEMODE)(pwpWrite | pwpBinary));
361  break;
363  pRti->fp = pwpFileOpen(pWriteInfo->fileDest, (sysFILEMODE)(pwpWrite | pwpUnformatted));
364  break;
365  case PWP_ENCODING_SIZE: // silence compiler warning
366  break;
367  }
368  ret = (0 != pRti->fp);
369  }
370  return ret;
371 }
372 
373 
374 static int
375 fileDestCwdPush(const char *fileDest)
376 {
377  int ret = 0;
378  if (fileDest && fileDest[0]) {
379  char dir[FILENAME_MAX];
380  char *p;
381  strcpy(dir, fileDest);
382  p = strrchr(dir, '/');
383  if (p) {
384  // strip file/base name from end of path
385  *p = '\0';
386  if (0 == pwpCwdPush(dir)) {
387  ret = 1;
388  }
389  }
390  }
391  return ret;
392 }
393 
394 
395 int
396 caeuFileOpen(CAEP_RTITEM* pRti, const CAEP_WRITEINFO *pWriteInfo)
397 {
398  int ret = 0;
399  if (pRti) {
400  switch (pRti->FormatInfo.fileDest) {
401  /* Plugin wants a full "/path/to/filename.ext"
402  */
404  if (fileDestCwdPush(pWriteInfo->fileDest)) {
405  if (!openFileName(pRti, pWriteInfo)) {
406  pwpCwdPop();
407  }
408  else {
409  ret = 1;
410  }
411  }
412  break;
413 
414  /* Plugin wants a base "/path/to/basefilename" (no ext)
415  */
417  /* Set the cwd to the given folder.
418  It is the plugin's job to open the appropriate files.
419  */
420  ret = fileDestCwdPush(pWriteInfo->fileDest);
421  break;
422 
423  /* Plugin wants a folder "/path/to/folder/" (no file or ext)
424  */
425  case PWP_FILEDEST_FOLDER:
426  /* Set the cwd to the given folder.
427  It is the plugin's job to open the appropriate files.
428  */
429  if (0 == pwpCwdPush(pWriteInfo->fileDest)) {
430  ret = 1;
431  }
432  break;
433  case PWP_FILEDEST_SIZE: // silence compiler warning
434  break;
435  }
436  }
437  return ret;
438 }
439 
440 static const char *invalid = "!invalid";
441 
442 
443 const char *
445 {
446  switch (enc) {
447  case PWP_ENCODING_ASCII: return "ascii";
448  case PWP_ENCODING_BINARY: return "binary";
449  case PWP_ENCODING_UNFORMATTED: return "unformatted";
450  case PWP_ENCODING_SIZE: // silence compiler warning
451  break;
452  }
453  return invalid;
454 }
455 
456 
457 const char *
459 {
460  switch (prec) {
461  case PWP_PRECISION_SINGLE: return "single";
462  case PWP_PRECISION_DOUBLE: return "double";
463  case PWP_PRECISION_SIZE: // silence compiler warning
464  break;
465  }
466  return invalid;
467 }
468 
469 
470 const char *
472 {
473  switch (dim) {
474  case PWP_DIMENSION_2D: return "2D";
475  case PWP_DIMENSION_3D: return "3D";
476  case PWP_DIMENSION_SIZE: // silence compiler warning
477  break;
478  }
479  return invalid;
480 }
#define CAEPU_CLKS_TO_HMS(c, h, m, s)
Returns the clock value c decomposed into hours, minutes, and seconds (PWP_INT32). That is, c == (h * 3600 + m * 60 + s) * CLOCKS_PER_SEC.
Definition: apiCAEPUtils.h:976
static const char * invalid
PWP_ENUM_VALTYPE
Supported PWP-API getValue() transfer types.
Definition: apiPWP.h:612
static void sendClkDiffTimeMsg(CAEP_RTITEM *pRti, const char txt[], CAEPU_ENUM_CLOCKS startId, CAEPU_ENUM_CLOCKS endId)
#define CAEP_INFO_GROUP
Definition: apiCAEP.h:55
PWP_ENUM_ENCODING encoding
export file encoding.
Definition: apiCAEP.h:197
unsigned int PWP_UINT32
32-bit unsigned integer
Definition: apiPWP.h:194
CAEP_RTITEM * caeuFindFormatById(PWP_UINT32 id)
Find an item in caepRtItem[] by it&#39;s id.
PWU_RTITEM * pApiData
Pointer to the associated PWU_RTITEM structure.
Definition: apiCAEPUtils.h:135
Pointwise Plugin API (PWP-API)
PWP_BOOL PwuPublishValueDefinition(const char group[], const char name[], PWP_ENUM_VALTYPE type, const char value[], const char access[], const char desc[], const char range[])
Create a value.
sysFILEMODE
Bit flags used for pwpFileOpen(mode)
Definition: pwpPlatform.h:62
void PwuSendInfoMsg(const char api[], const char txt[], PWP_UINT32 code)
Send an info text message (PWP_MSGID_INFO) to the framework.
const char * caeuEncodeToText(PWP_ENUM_ENCODING enc)
Converts a PWP_ENUM_ENCODING value to a text string representation.
int caeuFileOpen(CAEP_RTITEM *pRti, const CAEP_WRITEINFO *pWriteInfo)
Prepare pRti for file I/O as specified by pWriteInfo.
#define CAEP_VALUE_GROUP
Definition: apiCAEP.h:51
PWP_BOOL PwuProgressBegin(const char api[], PWP_UINT32 totalSteps)
Send a progress begin message (PWP_MSGID_PROGBEGIN) to the framework.
int pwpFileClose(FILE *fp)
Closes a file opened with pwpFileOpen().
Pointwise Grid Model API Specification (PWGM-API)
#define CAEPU_RT_CLKS_DIFF(rti, startId, endId)
Returns the clock time difference between startId and endId as clocks[endId] - clocks[startId]. The ids are validated.
Definition: apiCAEPUtils.h:908
PWP_ENUM_PRECISION
File precision values.
Definition: apiPWP.h:718
CAEP_FORMATINFO FormatInfo
The CAE Plugin format data.
Definition: apiCAEPUtils.h:131
const char * caeuDimensionToText(PWP_ENUM_DIMENSION dim)
Converts a PWP_ENUM_DIMENSION value to a text string representation.
FILE * pwpFileOpen(const char *filename, int mode)
Opens a file for I/O.
CAEPU_ENUM_CLOCKS
Supported CAEPU clock id values.
Definition: apiCAEPUtils.h:104
#define PWP_TRUE
PWP_BOOL logical "true" value.
Definition: apiPWP.h:227
void caeuSendErrorMsg(CAEP_RTITEM *pRti, const char txt[], PWP_UINT32 code)
Send an error text message (PWP_MSGID_ERROR) to the framework.
const char * fileDest
requested file destination.
Definition: apiCAEP.h:189
PWP_BOOL caeuProgressIncr(CAEP_RTITEM *pRti)
Completes a progress tracking sub-step.
PWP_BOOL caeuProgressEndStep(CAEP_RTITEM *pRti)
Completes a progress tracking major step.
Cross Platform Functions.
void caeuSendWarningMsg(CAEP_RTITEM *pRti, const char txt[], PWP_UINT32 code)
Send a warning text message (PWP_MSGID_WARNING) to the framework.
static int openFileName(CAEP_RTITEM *pRti, const CAEP_WRITEINFO *pWriteInfo)
#define CAEPU_CLKS_TO_MSECS(c)
Returns the clock value c as milli seconds (PWP_INT32). Only whole ms values are possible.
Definition: apiCAEPUtils.h:951
PWP_BOOL caeuProgressBeginStep(CAEP_RTITEM *pRti, PWP_UINT32 total)
Begins a progress tracking step.
PWP_UINT32 caepFormatCnt
The number of entries in caepRtItem[] array.
Definition: apiCAEP.cxx:38
CAEP_RTITEM caepRtItem[]
The runtime array of CAEP_RTITEM items.
Definition: apiCAEP.cxx:32
FILE * fp
Runtime FILE pointer.
Definition: apiCAEPUtils.h:201
CAEP utilities.
int caeuFileClose(CAEP_RTITEM *pRti, const CAEP_WRITEINFO *pWriteInfo)
Closes pRti for file I/O as specified by pWriteInfo.
PWP_BOOL PwuAssignValueEnum(const char group[], const char name[], const char value[], bool createIfNotExists)
void PwuSendWarningMsg(const char api[], const char txt[], PWP_UINT32 code)
Send a warning text message (PWP_MSGID_WARNING) to the framework.
PWP_ENUM_DIMENSION
Supported dimensionality values.
Definition: apiPWP.h:679
int pwpCwdPop(void)
Restore the current directory.
PWP_BOOL caeuProgressInit(CAEP_RTITEM *pRti, PWP_UINT32 cnt)
Initializes a progress tracking session.
PWP_BOOL caeuAssignInfoValue(const char key[], const char value[], bool createIfNotExists)
Creates a key/value pair that defines a CAE info attribute.
PWP_BOOL PwModGetAttributeString(PWGM_HGRIDMODEL model, const char *name, const char **val)
Get an attribute value as a specific type.
int PWP_BOOL
logical value
Definition: apiPWP.h:221
PWP_ENUM_ENCODING caeuFileEncoding(PWGM_HGRIDMODEL model)
Get the user-requested file encoding.
PWP_ENUM_ENCODING
File encoding values.
Definition: apiPWP.h:698
CAE export write control information.
Definition: apiCAEP.h:186
void caeuSendDebugMsg(CAEP_RTITEM *pRti, const char txt[], PWP_UINT32 code)
Send a debug text message (PWP_MSGID_DEBUG) to the framework.
void PwuSendDebugMsg(const char api[], const char txt[], PWP_UINT32 code)
Send a debug text message (PWP_MSGID_DEBUG) to the framework.
void PwuSendErrorMsg(const char api[], const char txt[], PWP_UINT32 code)
Send an error text message (PWP_MSGID_ERROR) to the framework.
CAEP_RTITEM * caeuFindFormatByName(const char name[])
Find an item in caepRtItem[] by it&#39;s name.
const char * caeuPrecisionToText(PWP_ENUM_PRECISION prec)
Converts a PWP_ENUM_PRECISION value to a text string representation.
int pwpFileDelete(const char *filename)
Delete a file.
PWP_ENDIANNESS caeuFileByteOrder(PWGM_HGRIDMODEL model)
Get the user-requested file byte order.
PWP_BOOL caeuPublishValueDefinition(const char key[], PWP_ENUM_VALTYPE type, const char value[], const char access[], const char desc[], const char range[])
Creates a collection of key/value pairs that represent a published CAE attribute definition.
void caeuSendInfoMsg(CAEP_RTITEM *pRti, const char txt[], PWP_UINT32 code)
Send an info text message (PWP_MSGID_INFO) to the framework.
void PwuProgressEnd(const char api[], PWP_BOOL ok)
Send a progress end message (PWP_MSGID_PROGEND) to the framework.
const char * name
full API spec name
Definition: apiPWP.h:571
static int fileDestCwdPush(const char *fileDest)
PWP_ENUM_PRECISION caeuFilePrecision(PWGM_HGRIDMODEL model)
Get the user-requested file precision.
PWP_ENDIANNESS
Flags used to indicate endianness or control endian behaviors in functions.
Definition: apiPWP.h:738
The data representing a CAE exporter instance.
Definition: apiCAEPUtils.h:126
PWP_ENUM_FILEDEST fileDest
Specifies the desired output destination type.
Definition: apiCAEP.h:123
#define FILENAME_MAX
Definition: pwpPlatform.cxx:45
void caeuProgressEnd(CAEP_RTITEM *pRti, PWP_BOOL ok)
Ends all progress tracking.
An opaque handle to a grid model.
Definition: apiGridModel.h:327
int pwpCwdPush(const char *dir)
Change the current directory.
PWP_BOOL PwuProgressStatus(const char api[], PWP_UINT32 complete, PWP_UINT32 total)
Send a progress status message (PWP_MSGID_PROGSTATUS, value >= 0) to the framework.
PWP_BOOL PwModGetAttributeBOOL(PWGM_HGRIDMODEL model, const char *name, PWP_BOOL *val)
PWP_ENDIANNESS PwuGetOsEndianness(void)
Query the OS&#39;s native endianness.
PWP_BOOL PwuProgressNextStep(const char api[])
Send a progress "next step" message (PWP_MSGID_PROGSTATUS, value = -1) to the framework.
PWP_APIINFO apiInfo
The PWP-API instance information.
Definition: apiPWPUtils.h:50
PWP_BOOL caeuExportStructuredAsUnstructured(PWGM_HGRIDMODEL model)
Get the user-requested structured grid handling.
#define PWP_FALSE
PWP_BOOL logical "false" value.
Definition: apiPWP.h:224
int PWP_INT32
32-bit integer
Definition: apiPWP.h:191
PWP_BOOL caeuExportBndryConditionsOnly(PWGM_HGRIDMODEL model)
Get the user-requested bc export option.
static void sendDiffTimeMsg(CAEP_RTITEM *pRti, const char txt[], clock_t diff)