das2C
das core C utilities (v3)
Loading...
Searching...
No Matches
spice.h
Go to the documentation of this file.
1/* Copyright (C) 2015-2026 Chris Piker <chris-piker@uiowa.edu>
2 *
3 * This file is part of das2C, the Core Das C Library.
4 *
5 * Das2C is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License version 2.1 as published
7 * by the Free Software Foundation.
8 *
9 * Das2C is distributed in the hope that it will be useful, but WITHOUT ANY
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * version 2.1 along with das2C; if not, see <http://www.gnu.org/licenses/>.
16 */
17
36#ifndef _das2_spice_H_
37#define _das2_spice_H_
38
39#include <stdio.h>
40#include <string.h>
41
42#include <SpiceUsr.h>
43
44#include <das3/util.h> /* das_error */
45#include <das3/send.h> /* das_send_except, das_except_t */
46
51/* Read the current SPICE error into sBuf as one line: the short message, a
52 space and the long message. The short message is at most 25 characters
53 and the long one at most 1840, so 2048 bytes is plenty. Gives an empty
54 string if there is no current error. */
55static inline void _das_spice_getmsg(char* sBuf, int nLen)
56{
57 int n;
58
59 sBuf[0] = '\0';
60 getmsg_c("SHORT", (SpiceInt)nLen, sBuf);
61 n = (int)strlen(sBuf);
62 while((n > 0)&&(sBuf[n-1] == ' ')) sBuf[--n] = '\0';
63
64 if(n > 0){
65 if(n + 2 >= nLen) return;
66 sBuf[n++] = ' ';
67 sBuf[n] = '\0';
68 }
69
70 getmsg_c("LONG", (SpiceInt)(nLen - n), sBuf + n);
71 n += (int)strlen(sBuf + n);
72 while((n > 0)&&(sBuf[n-1] == ' ')) sBuf[--n] = '\0';
73}
74
81static inline void das_spice_err_setup(void)
82{
83 errprt_c("SET", 4, "ALL"); /* Want all error text */
84 erract_c("SET", 7, "RETURN"); /* Just return on error, we'll check for it */
85 errdev_c("SET", 4, "NULL"); /* Don't go printing to the screen on error */
86}
87
97static inline const char* das_get_spice_error(void)
98{
99 static char sBuf[2048];
100 _das_spice_getmsg(sBuf, (int)sizeof(sBuf));
101 return sBuf;
102}
103
119static inline int das_send_spice_err(int nDasVer, das_except_t nType)
120{
121 char sMsg[2048];
122 _das_spice_getmsg(sMsg, (int)sizeof(sMsg));
123
124 fprintf(stderr, "ERROR: %s\n", sMsg);
125
126 if((nDasVer < 1)||(nDasVer > 3))
127 das_error(DASERR_SPICE, "Unknown stream version %d", nDasVer);
128 else
129 das_send_except(nDasVer, nType, sMsg); /* nothing for das1 */
130
131 return DASERR_SPICE;
132}
133
141static inline int das_print_spice_error(const char* sProgName)
142{
143 char sMsg[2048];
144 _das_spice_getmsg(sMsg, (int)sizeof(sMsg));
145
146 if(sProgName) fprintf(stderr, "ERROR (%s): %s\n", sProgName, sMsg);
147 else fprintf(stderr, "ERROR: %s\n", sMsg);
148
149 return DASERR_SPICE;
150}
151
154#endif /* _das2_spice_H_ */
Das Reader output formatting helpers
DAS_API void das_send_except(int nDasVer, das_except_t nType, const char *sMsg)
Output an exception of any type.
#define das_error(nErrCode,...)
Signal an error condition.
Definition util.h:195