Browse Source

If given dir does not contain DIRSEP add it

cl-refactor
Lefteris Karapetsas 10 years ago
parent
commit
25c62e1ae4
  1. 35
      io.h
  2. 25
      io_posix.c
  3. 25
      io_win32.c

35
io.h

@ -136,6 +136,21 @@ bool ethash_file_size(FILE* f, size_t* ret_size);
*/ */
int ethash_fileno(FILE* f); int ethash_fileno(FILE* f);
/**
* Create the filename for the DAG.
*
* @param dirname The directory name in which the DAG file should reside
* If it does not end with a directory separator it is appended.
* @param filename The actual name of the file
* @param filename_length The length of the filename in bytes
* @return A char* containing the full name. User must deallocate.
*/
char* ethash_io_create_filename(
char const* dirname,
char const* filename,
size_t filename_length
);
static inline bool ethash_io_mutable_name( static inline bool ethash_io_mutable_name(
uint32_t revision, uint32_t revision,
ethash_h256_t const* seed_hash, ethash_h256_t const* seed_hash,
@ -149,26 +164,6 @@ static inline bool ethash_io_mutable_name(
return snprintf(output, DAG_MUTABLE_NAME_MAX_SIZE, "%u_%016" PRIx64, revision, hash) >= 0; return snprintf(output, DAG_MUTABLE_NAME_MAX_SIZE, "%u_%016" PRIx64, revision, hash) >= 0;
} }
static inline char* ethash_io_create_filename(
char const* dirname,
char const* filename,
size_t filename_length
)
{
size_t dirlen = strlen(dirname);
// in C the cast is not needed, but a C++ compiler will complain for invalid conversion
char* name = (char*)malloc(dirlen + filename_length + 1);
if (!name) {
return NULL;
}
name[0] = '\0';
ethash_strncat(name, dirlen + filename_length + 1, dirname, dirlen);
ethash_strncat(name, dirlen + filename_length + 1, filename, filename_length);
return name;
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

25
io_posix.c

@ -48,6 +48,31 @@ int ethash_fileno(FILE *f)
return fileno(f); return fileno(f);
} }
char* ethash_io_create_filename(
char const* dirname,
char const* filename,
size_t filename_length
)
{
size_t dirlen = strlen(dirname);
size_t dest_size = dirlen + filename_length + 1;
if (dirname[dirlen] != '/') {
dest_size += 1;
}
char* name = malloc(dest_size);
if (!name) {
return NULL;
}
name[0] = '\0';
ethash_strncat(name, dest_size, dirname, dirlen);
if (dirname[dirlen] != '/') {
ethash_strncat(name, dest_size, "/", 1);
}
ethash_strncat(name, dest_size, filename, filename_length);
return name;
}
bool ethash_file_size(FILE* f, size_t* ret_size) bool ethash_file_size(FILE* f, size_t* ret_size)
{ {
struct stat st; struct stat st;

25
io_win32.c

@ -48,6 +48,31 @@ int ethash_fileno(FILE* f)
return _fileno(f); return _fileno(f);
} }
char* ethash_io_create_filename(
char const* dirname,
char const* filename,
size_t filename_length
)
{
size_t dirlen = strlen(dirname);
size_t dest_size = dirlen + filename_length + 1;
if (dirname[dirlen] != '\\' || dirname[dirlen] != '/') {
dest_size += 1;
}
char* name = malloc(dest_size);
if (!name) {
return NULL;
}
name[0] = '\0';
ethash_strncat(name, dest_size, dirname, dirlen);
if (dirname[dirlen] != '\\' || dirname[dirlen] != '/') {
ethash_strncat(name, dest_size, "\\", 1);
}
ethash_strncat(name, dest_size, filename, filename_length);
return name;
}
bool ethash_file_size(FILE* f, size_t* ret_size) bool ethash_file_size(FILE* f, size_t* ret_size)
{ {
struct _stat st; struct _stat st;

Loading…
Cancel
Save