Commit 331f7b27 authored by Zhukov, Alexander P's avatar Zhukov, Alexander P
Browse files

Tags work

parent 2bb2d141
......@@ -19,6 +19,11 @@ unsigned long encodeDbl(double a){
return b;
}
typedef struct tagset {
char **data;
size_t num;
} TAGS;
typedef struct timestamp {
unsigned int seconds;
unsigned int nanos;
......@@ -46,25 +51,50 @@ BinaryStorage bs_open(char* filename){
return storage;
}
int write_record(BinaryStorage *bs, char* name, TSTMP timestamp, double value){
int write_record(BinaryStorage *bs, char* name, TSTMP timestamp, double value, TAGS *tags){
unsigned char name_len = strlen(name);
unsigned int total_length = bswap_32(1+1+name_len+1+8+1+8+1);
int i=0;
unsigned int tag_length=0;
for(i=0;i<tags->num;i++){
tag_length+=strlen(tags->data[i])+2;
}
unsigned int total_length = bswap_32(1+1+name_len+1+8+1+8+1+tag_length);
printf("%d\n",tags->num);
fwrite(&total_length,4,1,bs->file);
/* name */
fputc(1,bs->file);
fwrite(&name_len,1,1,bs->file);
fwrite(name, 1,name_len,bs->file);
/* timestamp */
fputc(2,bs->file);
unsigned int sec = bswap_32(timestamp.seconds);
unsigned int nano = bswap_32(timestamp.nanos);
fwrite(&sec,4,1,bs->file);
fwrite(&nano,4,1,bs->file);
/* double scalar */
fputc(17,bs->file);
unsigned long rawdata = encodeDbl(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(tag_len,bs->file);
fwrite(tags->data[i],1,tag_len,bs->file);
printf("%s\n",tags->data[i]);
}
/* end of record */
fputc(254,bs->file);
bs->contents[bs->con_idx]=ftell(bs->file);
......@@ -94,12 +124,18 @@ int main(){
struct timeval tp;
long int ms;
BinaryStorage storage = bs_open("test_c.bin");
char *tag1 = "TAG1";
char *data[]={"MEGA_TAG",tag1};
TAGS tags={.data=data, .num=2};
int i=0;
for(i=0;i<10;i++){
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);
write_record(&storage,"Test_Record", timestamp, 3.1415*i, &tags);
}
bs_close(&storage);
......
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