aboutsummaryrefslogtreecommitdiff
path: root/vendor/voclient/libvoclient/vocUtil_spp.c
blob: ec07386ddde79c56aaf3012df7a2270a3ed68c64 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
 *  VOCUTIL_SPP.C -- Utility routines for the SPP proceduers.
 *
 *  Michael Fitzpatrick, NOAO, Jul 2006
 *
 *  @file       vocUtil_spp.c
 *  @author     Michael Fitzpatrick
 *  @version    June 2006
 *
 *************************************************************************
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>

#define _VOCLIENT_LIB_
#include "VOClient.h"


/**
 *  SPP Name mapping macros.  SPP procedure names are mappad as the first-5
 *  plus the last character of a name minus any underscores.  This should
 *  be done such that a unique 6-character name is produced for each SPP
 *  symbol.  In these definitions the SPP code may use the long form of the
 *  name in the code, the mapping is done automatically and so we need the
 *  macros here so the symbol entered in the library is actually the short
 *  name.
 */

/**
 *  SPP Type definitions.
 */
#define XCHAR		short
#define PKCHAR		char
#define XINT		int
#define XEOS		VOC_NULL


/**
 *  Public interface procedures.
 *
 */
PKCHAR *spp2c (XCHAR *instr,  int maxch);
int     c2spp (PKCHAR *instr, XCHAR *outstr, int maxch);
int    spplen (XCHAR *str);
int  dal_typecode (char *typestr);
int  out_typecode (char *typestr);



/**
 *  SPP2C -- Convert an SPP string to a C string.
 *
 *  @brief      Convert an SPP string to a C string.
 *  @fn         cstr = spp2c (XCHAR *sppstr, int maxch)
 *
 *  @param  sppstr      input SPP string
 *  @param  maxch       max length of SPP string
 *  @returns            converted C string
 */
char *
spp2c (XCHAR *instr, int maxch)
{
    XCHAR  *ip = instr;
    char   *outstr = (char *) calloc (1, maxch+1);
    char   *op = (char *) outstr;
    int      n = maxch;

    while ((*op++ = (char)*ip++) != (char) XEOS && --n >= 0)
        ;
    *--op = (char) XEOS;

    return (outstr);
}


/**
 *  C2SPP2C -- Convert a C string to an SPP string.
 *
 *  @brief      Convert a C string to an SPP string.
 *  @fn         sppstr = c2spp (PKCHAR *instr, int maxch)
 *
 *  @param  sppstr      input SPP string
 *  @param  maxch       max length of SPP string
 *  @returns            converted C string
 */
int c2spp (PKCHAR *instr, XCHAR *outstr, int maxch)
{
    char   *ip = (char *)instr;
    XCHAR  *op = outstr;
    int      len= 0, n = 0;

    /* Is is necessary to determine the length of the string in order to
     * be able to unpack the string in place, i.e., from right to left.
     */
    for (n=0;  n  < maxch;  n++)
        outstr[n] = (XCHAR) XEOS;
    for (n=0;  *ip;  n++)
        ip++;
    n -= 1;
    len = (n < maxch) ? n : maxch;
    op[n] = (XCHAR) XEOS;

    for (ip = (char *)instr;  --n >= 0;  )
        op[n] = ip[n];
/*
    op[maxch] = (XCHAR) XEOS;
*/
    op[maxch] = (XCHAR) VOC_NULL;

    return (len);
}


/**
 *  SPPLEN -- Get the length of an SPP string.
 *
 *  @brief      Get the length of an SPP string.
 *  @fn         len = spplen (XCHAR *str)
 *
 *  @param  str         input SPP string
 *  @returns            length of string
 */
int spplen (XCHAR *str)
{
    int len = 0;

    for (len=0; str[len] != (XCHAR) XEOS; len++)
	;

    return (len);
}


/**
 *  DAL_TYPECODE -- Convert a DAL service type string to a code.
 *
 *  @brief      Convert a DAL service type string to a code
 *  @fn         code = dal_typecode (char *typestr)
 *
 *  @param  typestr     input DAL string
 *  @returns            type code
 */

#define SZ_TYPECODE     32

int dal_typecode (char *typestr) 
{
    char type[SZ_TYPECODE];
    int  i = 0;

    memset (type, 0, SZ_TYPECODE);
    for (i=0; typestr[i] && i < SZ_TYPECODE; i++)
        type[i] = tolower (typestr[i]);

    if (strcmp (type, "dal") == 0)  return (DAL_CONN);
    if (strcmp (type, "cone") == 0) return (CONE_CONN);
    if (strcmp (type, "siap") == 0) return (SIAP_CONN);
    if (strcmp (type, "ssap") == 0) return (SSAP_CONN);
    if (strcmp (type, "slap") == 0) return (SLAP_CONN);
    if (strcmp (type, "stap") == 0) return (STAP_CONN);

    return (ERR);
}


/**
 *  OUT_TYPECODE -- Convert an output type string to a code.
 *
 *  @brief      Convert an output type string to a code
 *  @fn         code = out_typecode (char *typestr)
 *
 *  @param  typestr     input type string
 *  @returns            type code
 */
int out_typecode (char *typestr) 
{
    char type[SZ_TYPECODE];
    int  i = 0;

    memset (type, 0, SZ_TYPECODE);
    for (i=0; typestr[i] && i < SZ_TYPECODE; i++)
        type[i] = tolower (typestr[i]);

    if (strcmp (type, "raw") == 0)  	return (VOC_RAW);
    if (strcmp (type, "csv") == 0) 	return (VOC_CSV);
    if (strcmp (type, "votable") == 0) 	return (VOC_VOTABLE);

    return (ERR);
}