Added random generator and hashing library.

This commit is contained in:
2019-02-09 12:14:18 +00:00
parent d6f97767bb
commit 892994331a
9 changed files with 433 additions and 35 deletions

View File

@@ -1,7 +1,13 @@
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../lib)
add_library(SDBLib STATIC InputBuffer.c InputBuffer.h SQL.c SQL.h scanner.c scanner.h parser.c parser.h bplus_tree.c bplus_tree.h)
add_library(SDBLib STATIC
InputBuffer.c InputBuffer.h
SQL.c SQL.h
scanner.c scanner.h
parser.c parser.h
bplus_tree.c bplus_tree.h
../lib/MurmurHash3.c ../lib/MurmurHash3.h random.c random.h)
add_executable(SDB main.c)
target_link_libraries(SDB SDBLib)

View File

@@ -4,15 +4,16 @@
#include <string.h>
#include <strings.h>
#include <assert.h>
#include <time.h>
#include "InputBuffer.h"
#include "scanner.h"
#include "parser.h"
#include "bplus_tree.h"
#include "random.h"
void prompt() {
printf("SDB> ");
fflush(stdout);
}
void read_input(InputBuffer *buffer) {
@@ -51,14 +52,16 @@ void parse_input(char *input) {
free_scanner(scanner);
free_parser(parser);
}
int main() {
setbuf(stdout, 0);
setbuf(stderr, 0);
InputBuffer *buffer = input_buffer_new();
if (!init_random()) {
fprintf(stderr, "Error: failed to init random device\n");
exit(EXIT_FAILURE);
}
while (true) {
prompt();
@@ -72,5 +75,6 @@ int main() {
}
input_buffer_free(buffer);
deinit_random();
return EXIT_SUCCESS;
}

View File

@@ -300,7 +300,7 @@ ParserNode *parser_parse(Parser *parser, Scanner *scanner) {
}
break;
default:
parser_set_error(parser, "Unexpected node type", token);
parser_set_error(parser, "Unexpected input, expected start of statement", token);
break;
}
}

36
src/random.c Normal file
View File

@@ -0,0 +1,36 @@
//
// Created by sam on 12/07/18.
//
#include <stdio.h>
#include <assert.h>
#include "random.h"
FILE * randomFile = NULL;
bool init_random() {
randomFile = fopen("/dev/urandom", "r");
return randomFile != NULL;
}
bool deinit_random() {
int result = 0;
if (randomFile != NULL) {
result = fclose(randomFile);
randomFile = NULL;
}
return result == 0;
}
uint32_t random_32(bool *success) {
assert(randomFile != NULL);
uint32_t output = 0;
if (fread(&output, sizeof(uint32_t), 1, randomFile) < 1) {
if (success != NULL) {
*success = false;
}
return 0;
}
if (success != NULL) {
*success = true;
}
return output;
}

15
src/random.h Normal file
View File

@@ -0,0 +1,15 @@
//
// Created by sam on 12/07/18.
//
#ifndef SDB_RANDOM_H
#define SDB_RANDOM_H
#include <stdint.h>
#include <stdbool.h>
bool init_random();
bool deinit_random();
uint32_t random_32(bool *success);
#endif //SDB_RANDOM_H