Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
ML
lib
Binary Storage
Commits
4e081dfc
Commit
4e081dfc
authored
Feb 26, 2020
by
Zhukov, Alexander P
Browse files
Split out fucntions/definintions in a separate file
parent
331f7b27
Changes
3
Hide whitespace changes
Inline
Side-by-side
c-code/binary_storage.c
0 → 100644
View file @
4e081dfc
#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
);
}
c-code/binary_storage.h
0 → 100644
View file @
4e081dfc
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
);
c-code/test_code.c
View file @
4e081dfc
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <byteswap.h>
#include <sys/time.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
);
}
#include <binary_storage.h>
int
main
(){
struct
timeval
tp
;
...
...
@@ -127,8 +9,7 @@ int main(){
char
*
tag1
=
"TAG1"
;
char
*
data
[]
=
{
"MEGA_TAG"
,
tag1
};
TAGS
tags
=
{.
data
=
data
,
.
num
=
2
};
TAGS
tags
=
{.
data
=
data
,
.
num
=
2
};
int
i
=
0
;
for
(
i
=
0
;
i
<
10
;
i
++
){
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment