#include #include #include #include #include "SDDAS_types.h" #include "libCfg.h" #include "libserver.h" #include "libserverWrap.h" #include "local.h" static int _wasInitialized = 0; static char _error_str [125]; /* initialize the DB sub-system */ void SwRI_dbInitialize () { _wasInitialized = 1; memset (_error_str, 0, sizeof (_error_str)); } SDDAS_BOOL SwRI_dbIsReadOnly (const char *tablename) { return sFalse; } SDDAS_BOOL SwRI_dbInitializeMsg () { SwRI_dbInitialize (); return sTrue; } /* close the DB sub-system */ void SwRI_dbClose () { _wasInitialized = 0; } /* return a data key from a hierarchy */ SDDAS_ULONG SwRI_dbGetDataKey (char *p, char *m, char *e, char *i, char *v) { SDDAS_ULONG data_key; SDDAS_SHORT ret_val; ret_val = swri_get_data_key (p, m, e, i, v, &data_key); if (ret_val != ALL_OKAY) { data_key = 0; server_error_string (ret_val, _error_str); } return data_key; } char *SwRI_dbFilename (SDDAS_ULONG key) { static char fname [256]; char *path; char P [MAX_IDFS_NAME], S [MAX_IDFS_NAME], E [MAX_IDFS_NAME], I [MAX_IDFS_NAME], V [MAX_IDFS_VIRT_NAME]; SwRI_dbKeyToStrings_r (key, P, S, E, I, V); memset (fname, 0, sizeof (fname)); if ((path = SwRI_dbCfgPath (key, "DatabasePath")) == NULL) { return NULL; } strcpy (fname, path); path = strrchr (fname, '/'); if (path != NULL) path [0] = '\0'; /* remove everything after the last / */ strcat (fname, "/"); strcat (fname, "Database"); strcat (fname, "/"); strcat (fname, I); /* append the instrument name */ return fname; } char *SwRI_dbFilename_r (SDDAS_ULONG key, char *fname) { char *path; char P [MAX_IDFS_NAME], S [MAX_IDFS_NAME], E [MAX_IDFS_NAME], I [MAX_IDFS_NAME], V [MAX_IDFS_VIRT_NAME]; SwRI_dbKeyToStrings_r (key, P, S, E, I, V); if ((path = SwRI_dbCfgPath (key, "DatabasePath")) == NULL) { return NULL; } strcpy (fname, path); path = strrchr (fname, '/'); if (path != NULL) path [0] = '\0'; /* remove everything after the last / */ strcat (fname, "/"); strcat (fname, "Database"); strcat (fname, "/"); strcat (fname, I); /* append the instrument name */ return fname; } /* return a header/data file name from a given time */ int SwRI_dbIDFSGetRealTimeFile (SDDAS_ULONG key, RequestedDataType type, const char *exten, char *name1, char *name2) { int ret_val; Time_t btime, etime; btime.yr = -1; btime.day = -1; btime.msec = -1; etime.yr = -1; etime.day = -1; etime.msec = -1; if (type == _H_AND_D_) { ret_val = get_file_name (key, &btime, etime, _HEADER_, exten, name1); if (ret_val == ALL_OK) { strcpy (name2, name1); name2 [strlen (name2) - 1] = 'D'; } } else { ret_val = get_file_name (key, &btime, etime, type, exten, name1); name2 = NULL; } return (ret_val); } int SwRI_dbIDFSGetFileAndTimes (SDDAS_ULONG key, SDDAS_SHORT *btime_year, SDDAS_SHORT *btime_day, SDDAS_LONG *btime_msec, SDDAS_SHORT etime_year, SDDAS_SHORT etime_day, SDDAS_LONG etime_msec, RequestedDataType type, const char *exten, char *name1, char *name2) { int ret_val; Time_t btime, etime; btime.yr = (SDDAS_LONG) *btime_year; btime.day = (SDDAS_LONG) *btime_day; btime.msec = *btime_msec; etime.yr = (SDDAS_LONG) etime_year; etime.day = (SDDAS_LONG) etime_day; etime.msec = etime_msec; if (_wasInitialized == 0) printf ("You need to call dbInitialize!\n"); if (type == _H_AND_D_) { ret_val = get_file_name (key, &btime, etime, _HEADER_, exten, name1); if (ret_val == ALL_OK) { strcpy (name2, name1); name2 [strlen (name2) - 1] = 'D'; } } else { ret_val = get_file_name (key, &btime, etime, type, exten, name1); name2 = NULL; } *btime_year = btime.yr; *btime_day = btime.day; *btime_msec = btime.msec; return (ret_val); } int SwRI_dbIDFSGetFile (SDDAS_ULONG key, SDDAS_SHORT btime_year, SDDAS_SHORT btime_day, SDDAS_LONG btime_msec, SDDAS_SHORT etime_year, SDDAS_SHORT etime_day, SDDAS_LONG etime_msec, RequestedDataType type, const char *exten, char *name1, char *name2) { SDDAS_SHORT btime_year_r = btime_year; /* These will be overwritten! */ SDDAS_SHORT btime_day_r = btime_day; SDDAS_LONG btime_msec_r = btime_msec; return (SwRI_dbIDFSGetFileAndTimes (key, &btime_year_r, &btime_day_r, &btime_msec_r, etime_year, etime_day, etime_msec, type, exten, name1, name2)); } /* return the strings given a data key - returns either sFalse on error or sTrue if all is well */ SDDAS_BOOL SwRI_dbKeyToStrings_r (SDDAS_ULONG key, char *P, char *S, char *E, char *I, char *V) { int err; SDDAS_SHORT keys [5]; key_to_fields (key, keys); /* convert the keys to character strings */ err = KeysToStr (keys, P, S, E, I, V); if (err != ALL_OKAY) { server_error_string (err, _error_str); return sFalse; } else return sTrue; } SDDAS_BOOL SwRI_dbKeyToStrings (SDDAS_ULONG key, char *P, char *S, char *E, char *I, char *V) { return (SwRI_dbKeyToStrings_r (key, P, S, E, I, V)); } /* return a list of strings defining the hierarchy */ char **SwRI_dbHierarchy (int numargs, char **args) { char **ret_val = NULL; int num = 0; LinkList l = CfgProjects (); char *str = NULL; while (l != NULL) { switch (num) { case 0 : str = args [0]; break; case 1 : str = args [1]; break; case 2 : str = args [2]; break; case 3 : str = args [3]; break; case 4 : str = args [4]; break; } if (str == NULL) break; /* we found the limit of what we were looking */ else if (strcmp (CfgListName (l->data), str) == 0) { l = SUBLIST ((StrHier)l->data); num++; } else l = l->next; } if (l != NULL) { ret_val = (char **) malloc ((LinkCount (l) + 1) * sizeof (char *)); num = 0; while (l != NULL) { ret_val [num] = strdup (CfgListName (l->data)); l = l->next; num++; } ret_val [num] = NULL; } return (ret_val); } void SwRI_dbFreeHierarchyList (char **ret_val) { int num = 0; while (ret_val [num] != NULL) { free (ret_val [num]); num++; } free (ret_val); } /* return a nicely formatted hierarchy string */ char *SwRI_dbHierarchyString (SDDAS_ULONG key) { char P [MAX_IDFS_NAME], S [MAX_IDFS_NAME], E [MAX_IDFS_NAME], I [MAX_IDFS_NAME], V [MAX_IDFS_VIRT_NAME]; static char hierString [MAX_IDFS_NAME * 4 + MAX_IDFS_VIRT_NAME + 5]; SwRI_dbKeyToStrings_r (key, P, S, E, I, V); sprintf (hierString, "%s/%s/%s/%s/%s", P, S, E, I, V); return hierString; } /* return the virtual name */ char *SwRI_dbVirtualName (SDDAS_ULONG key) { char P [MAX_IDFS_NAME], S [MAX_IDFS_NAME], E [MAX_IDFS_NAME], I [MAX_IDFS_NAME]; static char V [MAX_IDFS_VIRT_NAME]; SwRI_dbKeyToStrings_r (key, P, S, E, I, V); return V; } /* return the virtual name - thread safe */ int SwRI_dbVirtualName_r (SDDAS_ULONG key, char *V, int len) { char P [MAX_IDFS_NAME], S [MAX_IDFS_NAME], E [MAX_IDFS_NAME], I [MAX_IDFS_NAME]; SwRI_dbKeyToStrings_r (key, P, S, E, I, V); return 1; } /* return an error message given an error code */ char *SwRI_dbErrorMsg () { return _error_str; } /* use these when you have a data key and not the full hierarchy */ char *SwRI_dbCfgItem (SDDAS_ULONG key, const char *item, int element) { char P [MAX_IDFS_NAME], S [MAX_IDFS_NAME], E [MAX_IDFS_NAME], I [MAX_IDFS_NAME], V [MAX_IDFS_VIRT_NAME]; SwRI_dbKeyToStrings_r (key, P, S, E, I, V); return (CfgGetItem (item, element, P, S, E, I, V, NULL)); } char *SwRI_dbCfgPath (SDDAS_ULONG key, const char *item) { char P [MAX_IDFS_NAME], S [MAX_IDFS_NAME], E [MAX_IDFS_NAME], I [MAX_IDFS_NAME], V [MAX_IDFS_VIRT_NAME]; SwRI_dbKeyToStrings_r (key, P, S, E, I, V); return (CfgGetPath (item, P, S, E, I, V, NULL)); } /* use this to query the data base */ void *SwRI_dbQuery (const char *query_str) { return NULL; } int SwRI_dbQueryExec (const char *query_str) { return 0; } void *SwRI_dbQueryStore (const char *query_str) { return NULL; } /* if the above is supposed to return NULL, use this function to see if the query was valid! */ unsigned int SwRI_dbBadQuery () { return 0; } /* free the resulting query */ void SwRI_dbFreeResult (void *query) { } SDDAS_BOOL SwRI_dbConnected () { return sTrue; }