Files
obitools3/src/utils.c
2016-04-25 18:02:58 +02:00

87 lines
1.8 KiB
C

/****************************************************************************
* Utility functions *
****************************************************************************/
/**
* @file utils.c
* @author Celine Mercier (celine.mercier@metabarcoding.org)
* @date 29 March 2016
* @brief Code for utility functions.
*/
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <time.h>
#include "utils.h"
#include "obidebug.h"
#include "obierrno.h"
#define DEBUG_LEVEL 0 // TODO has to be defined somewhere else (cython compil flag?)
/**********************************************************************
*
* D E F I N I T I O N O F T H E P U B L I C F U N C T I O N S
*
**********************************************************************/
int count_dir(char* dir_path)
{
struct dirent* dp;
DIR* fd;
int count;
count = 0;
if ((fd = opendir(dir_path)) == NULL)
{
obi_set_errno(OBI_UTILS_ERROR);
obidebug(1, "Error opening a directory: %s\n", dir_path);
return -1;
}
while ((dp = readdir(fd)) != NULL)
{
if ((dp->d_name)[0] == '.')
continue;
count++;
}
return count;
}
char* obi_format_date(time_t date)
{
char* formatted_time;
struct tm* tmp;
formatted_time = (char*) malloc(FORMATTED_TIME_LENGTH*sizeof(char));
tmp = localtime(&date);
if (tmp == NULL)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
obidebug(1, "\nError formatting a date");
return NULL;
}
if (strftime(formatted_time, FORMATTED_TIME_LENGTH, "%c", tmp) == 0)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
obidebug(1, "\nError formatting a date");
return NULL;
}
return formatted_time;
}