This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
tsmpipe/lib/dsmobjects.c

181 lines
3.3 KiB
C
Raw Normal View History

2013-07-18 13:59:14 +00:00
/*
* Date manipulations functions for tsmpipe
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dsmapitd.h"
/*
* This function will take a string date entered and convert that to a TSM
* date
*
* The date is passed to this function in the format mmddYYYY
*/
dsmDate dsmStrToDate(char *s) {
extern int verbose;
2013-07-18 13:59:14 +00:00
dsmDate *date;
dsUint32_t d,t;
char *x=NULL, *y=NULL;
2013-07-18 13:59:14 +00:00
date = (dsmDate*)malloc(sizeof(dsmDate));
x = (char *)malloc(sizeof(s));
y = (char *)malloc(sizeof(s));
2013-07-18 13:59:14 +00:00
if (date==NULL) {
perror("Arg, out of memory?");
exit(255);
}
memset(date,0x00,sizeof(dsmDate));
if (verbose)
printf("dsmStrToDate: Date String: %s\n", s);
/* if user key in some inputs */
if (s[0] != '\0') {
y=strchr(s,':');
if (y != NULL) {
strncpy(x,s,strlen(s)-strlen(y));
x[strlen(s)-strlen(y)] = '\0';
y = strchr(s,':')+1;
} else
x = s;
d = atol(x);
date->month = d / 1000000;
d %= 1000000;
date->day = d / 10000;
date->year = d % 10000;
2013-07-18 13:59:14 +00:00
if (y != NULL) {
t = atol(y);
date->hour = t / 10000;
t %= 10000;
date->minute = t / 100;
date->second = t % 100;
}
2013-07-18 13:59:14 +00:00
}
if (verbose)
printf("dsmStrToDate: date = %d, month = %d, year = %d, hour = %d, minute = %d, second = %d\n", date->day, date->month, date->year, date->hour, date->minute, date->second);
2013-07-18 13:59:14 +00:00
return *date;
}
/* Display the dsmDate value as a string */
char *dsmDateToStr(dsmDate date) {
char *s;
s = malloc(19);
if (s==NULL) {
perror("Arg, out of memory?");
exit(255);
}
memset(s,0x00,(sizeof s));
2013-07-18 13:59:14 +00:00
2014-10-02 12:42:03 +00:00
sprintf(s,"%04i-%02i-%02i %02i:%02i:%02i",
2013-07-18 13:59:14 +00:00
date.year,
2014-10-02 12:42:03 +00:00
(dsInt16_t)date.month,
(dsInt16_t)date.day,
2013-07-18 13:59:14 +00:00
(dsInt16_t)date.hour,
(dsInt16_t)date.minute,
(dsInt16_t)date.second);
return s;
}
dsmObjName dsmNameToObjname(char *fsname, char *filename) {
extern int verbose;
2013-07-18 13:59:14 +00:00
dsmObjName *objname;
char *p;
objname = (dsmObjName*)malloc(sizeof(dsmObjName));
if (objname==NULL) {
perror("Arg, out of memory?");
exit(255);
}
memset(objname,0x00,sizeof(dsmObjName));
if (verbose)
fprintf(stderr,"%s: FS: %s Filename: %s\n",__func__,fsname,filename);
2013-07-18 13:59:14 +00:00
/* fs == "/filesystem", hl == "/directory/path", ll == "/filename" */
strcpy(objname->fs,fsname);
p = strrchr(filename, '/');
if (p) {
*p = '\0';
if (*filename && *filename != '/')
strcpy(objname->hl, "/");
strcpy(objname->hl,filename);
sprintf(objname->ll,"/%s",p+1);
} else {
if (*filename && *filename != '/')
strcpy(objname->ll, "/");
strcat(objname->ll,filename);
}
objname->objType = DSM_OBJ_FILE;
return *objname;
}
char *dsmObjnameToStr(dsmObjName objName) {
char *s;
s = malloc(sizeof(dsmObjName));
if (s==NULL) {
perror("Arg, out of memory?");
exit(255);
}
memset(s,0x00,(sizeof s));
2013-07-18 13:59:14 +00:00
sprintf(s,"%s%s%s",objName.fs,objName.hl,objName.ll);
return s;
}
double dsmSizeToNum(dsStruct64_t dsStruct64,dsBool_t inMB) {
unsigned long long filesize;
extern int verbose;
2013-07-18 13:59:14 +00:00
filesize = dsStruct64.hi;
filesize <<= 32;
filesize |= dsStruct64.lo;
if (verbose)
fprintf(stderr,"%s: Size: %d\n",__func__,(int)filesize);
2013-07-18 13:59:14 +00:00
// Return number in MB
return inMB ? (double)filesize/1024/1024 : (double)filesize;
2013-07-18 13:59:14 +00:00
}
void debugLog(int level, const char *func, char *message, int die) {
extern int verbose;
if (level > verbose)
return;
fprintf(stderr,"%s: %s\n",func,message);
if (die)
exit(die);
}