Skip to content

Commit

Permalink
Add bgen_strdup and update filepath allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
horta committed Jun 17, 2024
1 parent 3ce4bfe commit 8cbde3f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ add_library(
src/partition.c
src/bstring.c
src/unzlib.c
src/unzstd.c)
src/unzstd.c
src/strdup.c)
add_library(BGEN::bgen ALIAS bgen)

include(GenerateExportHeader)
Expand Down
3 changes: 2 additions & 1 deletion src/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "mem.h"
#include "report.h"
#include "samples.h"
#include "strdup.h"
#include <inttypes.h>
#include <stdbool.h>

Expand Down Expand Up @@ -190,7 +191,7 @@ int bgen_file_seek_variants_start(struct bgen_file* bgen_file)
static struct bgen_file* bgen_file_create(char const* filepath)
{
struct bgen_file* bgen = malloc(sizeof(struct bgen_file));
bgen->filepath = strdup(filepath);
bgen->filepath = bgen_strdup(filepath);
bgen->stream = NULL;
bgen->nvariants = 0;
bgen->nsamples = 0;
Expand Down
3 changes: 2 additions & 1 deletion src/metafile.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "metafile_write.h"
#include "partition.h"
#include "report.h"
#include "strdup.h"
#include <string.h>

static struct bgen_metafile* metafile_alloc(char const* filepath);
Expand Down Expand Up @@ -222,7 +223,7 @@ uint32_t bgen_metafile_partition_size(uint32_t nvariants, uint32_t npartitions)
static struct bgen_metafile* metafile_alloc(char const* filepath)
{
struct bgen_metafile* metafile = malloc(sizeof(struct bgen_metafile));
metafile->filepath = strdup(filepath);
metafile->filepath = bgen_strdup(filepath);
metafile->stream = NULL;
metafile->partition_offset = NULL;
return metafile;
Expand Down
12 changes: 12 additions & 0 deletions src/strdup.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "strdup.h"
#include <stdlib.h>
#include <string.h>

char* bgen_strdup(const char* s)
{
size_t l = strlen(s);
char* d = malloc(l + 1);
if (!d)
return NULL;
return memcpy(d, s, l + 1);
}
6 changes: 6 additions & 0 deletions src/strdup.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef STRDUP_H
#define STRDUP_H

char* bgen_strdup(const char* s);

#endif

0 comments on commit 8cbde3f

Please sign in to comment.