00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "drms.h"
00032 #include "jsoc_main.h"
00033 #include "json.h"
00034 #include "qDecoder.h"
00035
00036
00037 char *module_name = "drms_parserecset";
00038
00039 typedef enum
00040 {
00041 PRSSTAT_SUCCESS = 0,
00042 PRSSTAT_MISSINGARG,
00043 PRSSTAT_NOMEM,
00044 PRSSTAT_CANTPARSE,
00045 PRSSTAT_JSONELEM
00046 } PRSSTAT_t;
00047
00048 #define SPEC "spec"
00049 #define NOT_SPECIFIED "NOT_SPECIFIED"
00050
00051 ModuleArgs_t module_args[] =
00052 {
00053 {ARG_STRING, SPEC, NOT_SPECIFIED, "The record-set specification to be parsed into parts."},
00054 {ARG_END}
00055 };
00056
00057
00058 int DoIt(void)
00059 {
00060 PRSSTAT_t rv = PRSSTAT_SUCCESS;
00061
00062 const char *specArg = NULL;
00063 char *spec = NULL;
00064 char *allvers = NULL;
00065 char **sets = NULL;
00066 DRMS_RecordSetType_t *settypes = NULL;
00067 char **snames = NULL;
00068 char **filts = NULL;
00069 int nsets = 0;
00070 DRMS_RecQueryInfo_t rsinfo;
00071
00072 const char *errMsg = NULL;
00073 json_t *root = NULL;
00074 json_t *subsets = NULL;
00075 json_t *elem = NULL;
00076
00077 Q_ENTRY *req = NULL;
00078
00079 char *escapedStr = NULL;
00080
00081 root = json_new_object();
00082
00083 if (root)
00084 {
00085 specArg = cmdparams_get_str(&cmdparams, SPEC, NULL);
00086 if (strcmp(specArg, NOT_SPECIFIED) == 0)
00087 {
00088
00089
00090
00091 req = qCgiRequestParseQueries(NULL, NULL);
00092 if (req)
00093 {
00094 char *value = NULL;
00095
00096 value = (char *)qEntryGetStr(req, SPEC);
00097 if (value)
00098 {
00099 spec = strdup(value);
00100 if (!spec)
00101 {
00102 rv = PRSSTAT_NOMEM;
00103 errMsg = "Not enough memory to create spec string.";
00104 }
00105 }
00106 else
00107 {
00108 rv = PRSSTAT_MISSINGARG;
00109 errMsg = "Missing required argument 'spec'.";
00110 }
00111 }
00112 }
00113 else
00114 {
00115 spec = strdup(specArg);
00116 if (!spec)
00117 {
00118 rv = PRSSTAT_NOMEM;
00119 errMsg = "Not enough memory to create spec string.";
00120 }
00121 }
00122
00123 if (rv == PRSSTAT_SUCCESS)
00124 {
00125 rv = (drms_record_parserecsetspec(spec, &allvers, &sets, &settypes, &snames, &filts, &nsets, &rsinfo) == 0) ? PRSSTAT_SUCCESS : PRSSTAT_CANTPARSE;
00126 errMsg = "Unable to parse specification.";
00127 }
00128
00129 if (rv == PRSSTAT_SUCCESS)
00130 {
00131
00132 escapedStr = json_escape(spec);
00133 if (!escapedStr)
00134 {
00135 rv = PRSSTAT_NOMEM;
00136 errMsg = "Not enough memory to create spec string.";
00137 }
00138
00139 if (rv == PRSSTAT_SUCCESS)
00140 {
00141 rv = (json_insert_pair_into_object(root, "spec", json_new_string(escapedStr)) == JSON_OK) ? PRSSTAT_SUCCESS : PRSSTAT_JSONELEM;
00142 free(escapedStr);
00143 }
00144
00145 if (rv == PRSSTAT_SUCCESS)
00146 {
00147
00148 rv = (json_insert_pair_into_object(root, "atfile", ((rsinfo & kAtFile) != 0) ? json_new_true() : json_new_false()) == JSON_OK) ? PRSSTAT_SUCCESS : PRSSTAT_JSONELEM;
00149 errMsg = "Unable to create atfile property.";
00150 }
00151
00152 if (rv == PRSSTAT_SUCCESS)
00153 {
00154
00155 rv = (json_insert_pair_into_object(root, "hasfilts", ((rsinfo & kFilters) != 0) ? json_new_true() : json_new_false())) ? PRSSTAT_SUCCESS : PRSSTAT_JSONELEM;
00156 errMsg = "Unable to create hasfilts property.";
00157 }
00158
00159 if (rv == PRSSTAT_SUCCESS)
00160 {
00161
00162 char numBuf[64];
00163
00164 snprintf(numBuf, sizeof(numBuf), "%d", nsets);
00165 rv = (json_insert_pair_into_object(root, "nsubsets", json_new_number(numBuf)) == JSON_OK) ? PRSSTAT_SUCCESS : PRSSTAT_JSONELEM;
00166 errMsg = "Unable to create nsubsets property.";
00167 }
00168
00169 if (rv == PRSSTAT_SUCCESS)
00170 {
00171 subsets = json_new_array();
00172 }
00173
00174 if (subsets)
00175 {
00176 int iSet;
00177
00178 for (iSet = 0; iSet < nsets; iSet++)
00179 {
00180 elem = json_new_object();
00181
00182 if (!elem)
00183 {
00184 rv = PRSSTAT_NOMEM;
00185 errMsg = "Not enough memory to create JSON subset object.";
00186 break;
00187 }
00188
00189 escapedStr = json_escape(sets[iSet]);
00190 if (!escapedStr)
00191 {
00192 rv = PRSSTAT_NOMEM;
00193 errMsg = "Not enough memory to create spec string.";
00194 }
00195
00196 if (rv == PRSSTAT_SUCCESS)
00197 {
00198 rv = (json_insert_pair_into_object(elem, "spec", json_new_string(escapedStr)) == JSON_OK) ? PRSSTAT_SUCCESS : PRSSTAT_JSONELEM;
00199 free(escapedStr);
00200 }
00201
00202 if (rv != PRSSTAT_SUCCESS)
00203 {
00204 errMsg = "Unable to create spec property in subset object.";
00205 break;
00206 }
00207
00208 escapedStr = json_escape(drms_type_recsetnames[settypes[iSet]]);
00209 if (!escapedStr)
00210 {
00211 rv = PRSSTAT_NOMEM;
00212 errMsg = "Not enough memory to create set-type string.";
00213 }
00214
00215 if (rv == PRSSTAT_SUCCESS)
00216 {
00217 rv = (json_insert_pair_into_object(elem, "settype", json_new_string(escapedStr)) == JSON_OK) ? PRSSTAT_SUCCESS : PRSSTAT_JSONELEM;
00218 free(escapedStr);
00219 }
00220
00221 if (rv != PRSSTAT_SUCCESS)
00222 {
00223 errMsg = "Unable to create settype property in subset object.";
00224 break;
00225 }
00226
00227 escapedStr = json_escape(snames[iSet]);
00228 if (!escapedStr)
00229 {
00230 rv = PRSSTAT_NOMEM;
00231 errMsg = "Not enough memory to create series-name string.";
00232 }
00233
00234 if (rv == PRSSTAT_SUCCESS)
00235 {
00236 rv = (json_insert_pair_into_object(elem, "seriesname", json_new_string(escapedStr)) == JSON_OK) ? PRSSTAT_SUCCESS : PRSSTAT_JSONELEM;
00237 free(escapedStr);
00238 }
00239
00240 if (rv != PRSSTAT_SUCCESS)
00241 {
00242 errMsg = "Unable to create seriesname property in subset object.";
00243 break;
00244 }
00245
00246 if (filts[iSet])
00247 {
00248 escapedStr = json_escape(filts[iSet]);
00249
00250 if (!escapedStr)
00251 {
00252 rv = PRSSTAT_NOMEM;
00253 errMsg = "Not enough memory to create filter string.";
00254 }
00255 }
00256 else
00257 {
00258 escapedStr = NULL;
00259 }
00260
00261 if (rv == PRSSTAT_SUCCESS)
00262 {
00263 rv = (json_insert_pair_into_object(elem, "filter", (escapedStr != NULL) ? json_new_string(escapedStr) : json_new_null()) == JSON_OK) ? PRSSTAT_SUCCESS : PRSSTAT_JSONELEM;
00264 free(escapedStr);
00265 }
00266
00267 if (rv != PRSSTAT_SUCCESS)
00268 {
00269 errMsg = "Unable to create filter property in subset object.";
00270 break;
00271 }
00272
00273 rv = (json_insert_pair_into_object(elem, "autobang", (allvers[iSet] == 'Y' || allvers[iSet] == 'y') ? json_new_true() : json_new_false()) == JSON_OK) ? PRSSTAT_SUCCESS : PRSSTAT_JSONELEM;
00274 if (rv != PRSSTAT_SUCCESS)
00275 {
00276 errMsg = "Unable to create autobang property in subset object.";
00277 break;
00278 }
00279
00280 rv = (json_insert_child(subsets, elem) == JSON_OK) ? PRSSTAT_SUCCESS : PRSSTAT_JSONELEM;
00281 if (rv != PRSSTAT_SUCCESS)
00282 {
00283 errMsg = "Unable to insert a set into the subsets array.";
00284 break;
00285 }
00286 }
00287
00288 if (rv == PRSSTAT_SUCCESS)
00289 {
00290 rv = (json_insert_pair_into_object(root, "subsets", subsets) == JSON_OK) ? PRSSTAT_SUCCESS : PRSSTAT_JSONELEM;
00291 }
00292 }
00293 else
00294 {
00295 rv = PRSSTAT_NOMEM;
00296 errMsg = "Not enough memory to create JSON subsets array.";
00297 }
00298 }
00299
00300 free(spec);
00301 spec = NULL;
00302
00303 drms_record_freerecsetspecarr(&allvers, &sets, &settypes, &snames, &filts, nsets);
00304 }
00305 else
00306 {
00307 rv = PRSSTAT_NOMEM;
00308 errMsg = "Not enough memory to create JSON root object.";
00309 }
00310
00311
00312 if (rv != PRSSTAT_SUCCESS)
00313 {
00314 json_free_value(&root);
00315 root = json_new_object();
00316
00317 if (root)
00318 {
00319 escapedStr = json_escape(errMsg);
00320 if (!escapedStr)
00321 {
00322 rv = PRSSTAT_NOMEM;
00323 errMsg = "Not enough memory to create error-message string.";
00324 }
00325
00326 if (rv == PRSSTAT_SUCCESS)
00327 {
00328 rv = (json_insert_pair_into_object(root, "errMsg", json_new_string(escapedStr)) == JSON_OK) ? PRSSTAT_SUCCESS : PRSSTAT_JSONELEM;
00329 free(escapedStr);
00330 }
00331 }
00332 else
00333 {
00334 rv = PRSSTAT_NOMEM;
00335 errMsg = "Not enough memory to create JSON root object.";
00336 }
00337 }
00338 else
00339 {
00340 rv = (json_insert_pair_into_object(root, "errMsg", json_new_null()) == JSON_OK) ? PRSSTAT_SUCCESS : PRSSTAT_JSONELEM;
00341 }
00342
00343 char *jsonStr = NULL;
00344
00345 json_tree_to_string(root, &jsonStr);
00346 json_free_value(&root);
00347 printf(jsonStr);
00348 free(jsonStr);
00349 printf("\n");
00350
00351 return 0;
00352 }