Commit 4e081dfc authored by Zhukov, Alexander P's avatar Zhukov, Alexander P
Browse files

Split out fucntions/definintions in a separate file

parent 331f7b27
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <byteswap.h>
#include <sys/time.h>
#include "binary_storage.h"
unsigned long encodeDbl(double a){
unsigned long b;
unsigned char *src = (unsigned char *)&a,
*dst = (unsigned char *)&b;
dst[0] = src[7];
dst[1] = src[6];
dst[2] = src[5];
dst[3] = src[4];
dst[4] = src[3];
dst[5] = src[2];
dst[6] = src[1];
dst[7] = src[0];
return b;
}
BinaryStorage bs_open(char* filename){
BinaryStorage storage;
storage.file = fopen(filename,"wb");
storage.buf_size = 500;
storage.con_idx = 0;
storage.contents = (long*) malloc(sizeof(unsigned long)*storage.buf_size);
char header[]="SNS-BIET2020";
fwrite(header,sizeof(header)-1,1,storage.file);
storage.contents[storage.con_idx]=ftell(storage.file);
storage.con_idx++;
return storage;
}
int write_record(BinaryStorage *bs, char* name, TSTMP timestamp, double value, TAGS *tags){
unsigned char name_len = strlen(name);
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);
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);
}
/* end of record */
fputc(254,bs->file);
bs->contents[bs->con_idx]=ftell(bs->file);
bs->con_idx++;
}
void bs_close(BinaryStorage *bs){
unsigned int total_length = bswap_32(1+1);
fwrite(&total_length,4,1,bs->file);
fputc(255,bs->file);
fputc(254,bs->file);
bs->contents[bs->con_idx]=ftell(bs->file);
bs->con_idx++;
int i;
for(i=0;i<bs->con_idx;i++){
unsigned long swapped = bswap_64(bs->contents[i]);
fwrite(&swapped,8,1,bs->file);
}
unsigned int num_of_rec = bswap_32(bs->con_idx);
fwrite(&num_of_rec,4,1,bs->file);
fclose(bs->file);
free(bs->contents);
}
typedef struct tagset {
char **data;
size_t num;
} TAGS;
typedef struct timestamp {
unsigned int seconds;
unsigned int nanos;
} TSTMP;
typedef struct storage {
FILE* file;
unsigned long* contents;
unsigned int con_idx;
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);
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <byteswap.h>
#include <sys/time.h> #include <sys/time.h>
#include <binary_storage.h>
unsigned long encodeDbl(double a){
unsigned long b;
unsigned char *src = (unsigned char *)&a,
*dst = (unsigned char *)&b;
dst[0] = src[7];
dst[1] = src[6];
dst[2] = src[5];
dst[3] = src[4];
dst[4] = src[3];
dst[5] = src[2];
dst[6] = src[1];
dst[7] = src[0];
return b;
}
typedef struct tagset {
char **data;
size_t num;
} TAGS;
typedef struct timestamp {
unsigned int seconds;
unsigned int nanos;
} TSTMP;
typedef struct storage {
FILE* file;
unsigned long* contents;
unsigned int con_idx;
size_t buf_size;
} BinaryStorage;
BinaryStorage bs_open(char* filename){
BinaryStorage storage;
storage.file = fopen(filename,"wb");
storage.buf_size = 500;
storage.con_idx = 0;
storage.contents = (long*) malloc(sizeof(unsigned long)*storage.buf_size);
char header[]="SNS-BIET2020";
fwrite(header,sizeof(header)-1,1,storage.file);
storage.contents[storage.con_idx]=ftell(storage.file);
storage.con_idx++;
return storage;
}
int write_record(BinaryStorage *bs, char* name, TSTMP timestamp, double value, TAGS *tags){
unsigned char name_len = strlen(name);
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);
bs->con_idx++;
}
void bs_close(BinaryStorage *bs){
unsigned int total_length = bswap_32(1+1);
fwrite(&total_length,4,1,bs->file);
fputc(255,bs->file);
fputc(254,bs->file);
bs->contents[bs->con_idx]=ftell(bs->file);
bs->con_idx++;
int i;
for(i=0;i<bs->con_idx;i++){
unsigned long swapped = bswap_64(bs->contents[i]);
fwrite(&swapped,8,1,bs->file);
}
unsigned int num_of_rec = bswap_32(bs->con_idx);
fwrite(&num_of_rec,4,1,bs->file);
fclose(bs->file);
free(bs->contents);
}
int main(){ int main(){
struct timeval tp; struct timeval tp;
...@@ -127,8 +9,7 @@ int main(){ ...@@ -127,8 +9,7 @@ int main(){
char *tag1 = "TAG1"; char *tag1 = "TAG1";
char *data[]={"MEGA_TAG",tag1}; char *data[]={"MEGA_TAG",tag1};
TAGS tags={.data=data, .num=2}; TAGS tags={.data=data, .num=2};
int i=0; int i=0;
for(i=0;i<10;i++){ for(i=0;i<10;i++){
......
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