00001
00002
00003
00004
00005
00006
00007
00008 #include "jsoc_main.h"
00009 #include "drms_types.h"
00010 #include <sys/file.h>
00011
00012 char *module_name = "timeslot";
00013
00014 typedef enum
00015 {
00016 kTSErrSuccess = 0,
00017 kTSErrBadArgs = 1,
00018 kTSErrCantOpenRec = 2,
00019 kTSErrOutOfMemory = 3,
00020 kTSErrConversion = 4,
00021 kTSErrFileIO = 5
00022 } TSError_t;
00023
00024 #define kSeries "series"
00025 #define kTimeKey "tkey"
00026 #define kTimeVal "tval"
00027 #define kUndef "undef"
00028
00029 ModuleArgs_t module_args[] =
00030 {
00031 {ARG_STRING, kSeries, NULL, "The series with a time-slotted keyword."},
00032 {ARG_STRING, kTimeKey, NULL, "The time-slotted keyword."},
00033 {ARG_STRING, kTimeVal, kUndef, "The time value of the time-slotted keyword, or a file containing a list of such values."},
00034 {ARG_END}
00035 };
00036
00037 int DoIt(void)
00038 {
00039 TSError_t rv = kTSErrSuccess;
00040 int status;
00041
00042 const char *series = cmdparams_get_str(&cmdparams, kSeries, &status);
00043 const char *tkey = cmdparams_get_str(&cmdparams, kTimeKey, &status);
00044 const char *tval = cmdparams_get_str(&cmdparams, kTimeVal, &status);
00045
00046
00047
00048 if (!drms_series_exists(drms_env, series, &status) || status != DRMS_SUCCESS)
00049 {
00050 fprintf(stderr, "Series %s does not exist.\n", series);
00051 rv = kTSErrBadArgs;
00052 }
00053 else
00054 {
00055 DRMS_Keyword_t *key = NULL;
00056 DRMS_Record_t *rec = NULL;
00057
00058 rec = drms_template_record(drms_env, series, &status);
00059
00060 if (!rec || status != DRMS_SUCCESS)
00061 {
00062 fprintf(stderr, "Series %s does not exist.\n", series);
00063 rv = kTSErrBadArgs;
00064 }
00065 else
00066 {
00067 if ((key = drms_keyword_lookup(rec, tkey, 0)) == NULL)
00068 {
00069 fprintf(stderr, "Invalid keyword %s.\n", tkey);
00070 rv = kTSErrBadArgs;
00071 }
00072 else
00073 {
00074
00075 if (!drms_keyword_isslotted(key))
00076 {
00077 fprintf(stderr, "Keyword %s is not a time-slotted keyword.\n", tkey);
00078 rv = kTSErrBadArgs;
00079 }
00080 else
00081 {
00082 DRMS_Value_t tin;
00083 DRMS_Value_t sout;
00084 LinkedList_t *list = NULL;
00085 ListNode_t *ln = NULL;
00086 TIME timetval;
00087 struct stat stBuf;
00088 FILE *stream = NULL;
00089
00090 tin.type = DRMS_TYPE_TIME;
00091 list = list_llcreate(sizeof(TIME), NULL);
00092
00093 if (list == NULL)
00094 {
00095 rv = kTSErrOutOfMemory;
00096 }
00097
00098 if (rv == kTSErrSuccess)
00099 {
00100 if (strcmp(tval, kUndef) == 0)
00101 {
00102
00103 stream = stdin;
00104 }
00105 else if (!stat(tval, &stBuf) && S_ISREG(stBuf.st_mode))
00106 {
00107
00108
00109 stream = fopen(tval, "r");
00110 if (!stream)
00111 {
00112 rv = kTSErrFileIO;
00113 }
00114 }
00115 }
00116
00117 if (rv == kTSErrSuccess)
00118 {
00119 if (stream == NULL)
00120 {
00121
00122 timetval = sscan_time((char *)tval);
00123 list_llinserttail(list, &timetval);
00124 }
00125 else
00126 {
00127
00128 char rbuf[LINE_MAX];
00129
00130 while (!(fgets(rbuf, LINE_MAX, stream) == NULL))
00131 {
00132 timetval = sscan_time(rbuf);
00133 list_llinserttail(list, &timetval);
00134 }
00135 }
00136 }
00137
00138 if (rv == kTSErrSuccess)
00139 {
00140 list_llreset(list);
00141 while ((ln = (ListNode_t *)(list_llnext(list))) != NULL)
00142 {
00143 timetval = *((TIME *)(ln->data));
00144 tin.value.time_val = timetval;
00145
00146 if (drms_keyword_slotval2indexval(key, &tin, &sout, NULL) != DRMS_SUCCESS)
00147 {
00148 fprintf(stderr, "Unable to calculate slot number.\n");
00149 rv = kTSErrConversion;
00150 }
00151 else
00152 {
00153 printf("%lld\n", sout.value.longlong_val);
00154 }
00155 }
00156 }
00157 }
00158 }
00159 }
00160 }
00161
00162 return rv;
00163 }