Commit 2bb2d141 authored by Zhukov, Alexander P's avatar Zhukov, Alexander P
Browse files

Timestamp works now

parent 0269b299
......@@ -19,6 +19,11 @@ unsigned long encodeDbl(double a){
return b;
}
typedef struct timestamp {
unsigned int seconds;
unsigned int nanos;
} TSTMP;
typedef struct storage {
FILE* file;
unsigned long* contents;
......@@ -41,7 +46,7 @@ BinaryStorage bs_open(char* filename){
return storage;
}
int write_record(BinaryStorage *bs, char* name, double timestamp, double value){
int write_record(BinaryStorage *bs, char* name, TSTMP timestamp, double value){
unsigned char name_len = strlen(name);
unsigned int total_length = bswap_32(1+1+name_len+1+8+1+8+1);
......@@ -51,11 +56,13 @@ int write_record(BinaryStorage *bs, char* name, double timestamp, double value){
fwrite(name, 1,name_len,bs->file);
fputc(2,bs->file);
unsigned long rawdata = encodeDbl(timestamp);
fwrite(&rawdata,8,1,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);
fputc(17,bs->file);
rawdata = encodeDbl(value);
unsigned long rawdata = encodeDbl(value);
fwrite(&rawdata,8,1,bs->file);
fputc(254,bs->file);
......@@ -72,10 +79,8 @@ void bs_close(BinaryStorage *bs){
bs->contents[bs->con_idx]=ftell(bs->file);
bs->con_idx++;
int i;
printf("total = %d\n",bs->con_idx);
for(i=0;i<bs->con_idx;i++){
unsigned long swapped = bswap_64(bs->contents[i]);
printf("%d\n",bs->contents[i]);
fwrite(&swapped,8,1,bs->file);
}
unsigned int num_of_rec = bswap_32(bs->con_idx);
......@@ -86,14 +91,16 @@ void bs_close(BinaryStorage *bs){
}
int main(){
printf("Hello World!\n");
BinaryStorage storage = bs_open("test_c.bin");
struct timeval tp;
gettimeofday(&tp, NULL);
long int ms = tp.tv_sec * 1000 + tp.tv_usec / 1000;
long int ms;
BinaryStorage storage = bs_open("test_c.bin");
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);
}
double timestamp = tp.tv_sec;
printf("%d\n",tp.tv_sec);
write_record(&storage,"Hello World!", timestamp, 314);
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