Commit 29f44fe7 authored by Zhukov, Alexander P's avatar Zhukov, Alexander P
Browse files

Implemented double[] in C

parent dd8f683a
......@@ -4,12 +4,13 @@
#include <sys/time.h>
#include "binary_storage.h"
uint64_t bswap_d(double a){
uint64_t *b = (uint64_t *)&a;
return bswap_64(*b);
}
BinaryStorage bs_open(char* filename){
BinaryStorage bsOpen(char* filename){
BinaryStorage storage;
storage.file = fopen(filename,"wb");
storage.buf_size = 500;
......@@ -23,8 +24,16 @@ BinaryStorage bs_open(char* filename){
return storage;
}
void close_record(BinaryStorage *bs){
/* end of record */
fputc(BS_ENDRCRD,bs->file);
bs->contents[bs->con_idx]=ftell(bs->file);
bs->con_idx++;
}
int write_record(BinaryStorage *bs, char* name, TSTMP timestamp, double value, TAGS *tags){
int write_meta(BinaryStorage *bs, char* name, TSTMP timestamp, TAGS *tags, uint32_t extra_count){
unsigned char name_len = strlen(name);
int i=0;
......@@ -33,46 +42,62 @@ int write_record(BinaryStorage *bs, char* name, TSTMP timestamp, double value, T
tag_length+=strlen(tags->data[i])+2;
}
uint32_t total_length = bswap_32(1+1+name_len+1+8+1+8+1+tag_length);
uint32_t total_length = bswap_32(1+1+name_len+1+8+extra_count+1+tag_length);
fwrite(&total_length,4,1,bs->file);
/* name */
fputc(1,bs->file);
fputc(BS_NAME,bs->file);
fwrite(&name_len,1,1,bs->file);
fwrite(name, 1,name_len,bs->file);
/* timestamp */
fputc(2,bs->file);
fputc(BS_UNIXTST,bs->file);
uint32_t sec = bswap_32(timestamp.seconds);
uint32_t nano = bswap_32(timestamp.nanos);
uint32_t nano = bswap_32(timestamp.nanos);
fwrite(&sec,4,1,bs->file);
fwrite(&nano,4,1,bs->file);
/* double scalar */
fputc(17,bs->file);
uint64_t rawdata = bswap_d(value);
fwrite(&rawdata,8,1,bs->file);
/* tags */
for(i=0;i<tags->num;i++){
unsigned char tag_len = strlen(tags->data[i]);
fputc(4,bs->file);
fputc(BS_TAG,bs->file);
fputc(tag_len,bs->file);
fwrite(tags->data[i],1,tag_len,bs->file);
}
}
/* end of record */
fputc(254,bs->file);
int bsWriteDBL(BinaryStorage *bs, char* name, TSTMP timestamp, DBL value, TAGS *tags){
uint32_t count = value.count;
if(count==0){
return -1;
}
else if(count==1){
write_meta(bs, name, timestamp, tags, 9);
fputc(BS_DBL,bs->file);
}
else {
write_meta(bs, name, timestamp, tags, 5+8*count);
fputc(BS_DBLWF,bs->file);
uint32_t rawcount = bswap_32(count*sizeof(double));
fwrite(&rawcount,4,1,bs->file);
}
uint32_t i = 0;
for(i=0;i<count;i++){
uint64_t rawdata = bswap_d(value.data[i]);
fwrite(&rawdata,8,1,bs->file);
bs->contents[bs->con_idx]=ftell(bs->file);
bs->con_idx++;
}
close_record(bs);
}
void bs_close(BinaryStorage *bs){
void bsClose(BinaryStorage *bs){
uint32_t total_length = bswap_32(1+1);
fwrite(&total_length,4,1,bs->file);
fputc(255,bs->file);
fputc(254,bs->file);
fputc(BS_ENDFILE,bs->file);
fputc(BS_ENDRCRD,bs->file);
bs->contents[bs->con_idx]=ftell(bs->file);
bs->con_idx++;
int i;
......
......@@ -16,6 +16,35 @@
(uint64_t)bswap_32((uint32_t)((value) >> 32)))
#endif
#define BS_ENDRCRD 254
#define BS_ENDFILE 255
#define BS_NAME 1
#define BS_UNIXTST 2
#define BS_EPICSTST 3
#define BS_TAG 4
#define BS_DESCR 5
#define BS_I32PARM 6
#define BS_I32 16
#define BS_I32WFPARM 26
#define BS_I32WF 36
#define BS_DBLPARM 7
#define BS_DBL 17
#define BS_DBLWFPARM 27
#define BS_DBLWF 37
#define BS_U8PARM 8
#define BS_U8 18
#define BS_U8WFPARM 28
#define BS_U8WF 38
#define BS_FLTPARM 9
#define BS_FLT 19
#define BS_FLTWFPARM 29
#define BS_FLT8WF 39
typedef struct tagset {
char **data;
size_t num;
......@@ -26,6 +55,11 @@ typedef struct timestamp {
uint32_t nanos;
} TSTMP;
typedef struct doublevalue {
double *data;
uint32_t count;
} DBL;
typedef struct storage {
FILE* file;
uint64_t* contents;
......@@ -33,6 +67,6 @@ typedef struct storage {
size_t buf_size;
} BinaryStorage;
BinaryStorage bs_open(char* filename);
int write_record(BinaryStorage *bs, char* name, TSTMP timestamp, double value, TAGS *tags);
void bs_close(BinaryStorage *bs);
BinaryStorage bsOpen(char* filename);
int bsWriteDBL(BinaryStorage *bs, char* name, TSTMP timestamp, DBL value, TAGS *tags);
void bsClose(BinaryStorage *bs);
......@@ -5,7 +5,7 @@
int main(){
struct timeval tp;
long int ms;
BinaryStorage storage = bs_open("test_c.bin");
BinaryStorage storage = bsOpen("test_c.bin");
char *tag1 = "TAG1";
char *data[]={"MEGA_TAG",tag1};
......@@ -16,8 +16,20 @@ int main(){
gettimeofday(&tp, NULL);
ms = tp.tv_sec * 1000 + tp.tv_usec / 1000;
TSTMP timestamp = {.seconds = tp.tv_sec, .nanos=tp.tv_usec*1000};
write_record(&storage,"Test_Record", timestamp, 3.1415*i, &tags);
DBL value;
double x;
if(i%2==0){
x = 3.1415*i;
value = (DBL){.count = 1, .data=&x};
} else {
double wf[] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
value = (DBL){.count = sizeof(wf)/8, .data=wf};
}
bsWriteDBL(&storage,"Test_Record", timestamp, value, &tags);
}
bs_close(&storage);
bsClose(&storage);
}
......@@ -29,8 +29,9 @@ print(args.file,'has',len(file), 'records')
def show(record):
if 'value' in record:
value = record['value']
if isinstance(value,(str,list,tuple)):
record['value'] = value[0]
# if isinstance(value,(str,list,tuple)):
if hasattr(value,'__len__') and len(value)>4:
record['value'] = [value[0],value[1], '...', value[len(value)-1]]
return record
if(not args.index):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment