2015-06-23 17:55:00 +02:00
|
|
|
/****************************************************************************
|
2016-04-08 15:38:57 +02:00
|
|
|
* Utility functions *
|
2015-06-23 17:55:00 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/**
|
2016-04-08 15:38:57 +02:00
|
|
|
* @file utils.c
|
2015-09-30 12:03:46 +02:00
|
|
|
* @author Celine Mercier (celine.mercier@metabarcoding.org)
|
2016-04-08 15:38:57 +02:00
|
|
|
* @date 29 March 2016
|
|
|
|
* @brief Code for utility functions.
|
2015-06-23 17:55:00 +02:00
|
|
|
*/
|
|
|
|
|
2015-06-18 17:19:23 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdio.h>
|
2015-06-23 17:55:00 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <dirent.h>
|
2016-03-21 11:33:06 +01:00
|
|
|
#include <unistd.h>
|
2016-04-25 18:02:58 +02:00
|
|
|
#include <time.h>
|
2015-06-18 17:19:23 +02:00
|
|
|
|
2016-04-08 15:38:57 +02:00
|
|
|
#include "utils.h"
|
2015-08-03 15:10:39 +02:00
|
|
|
#include "obidebug.h"
|
|
|
|
#include "obierrno.h"
|
|
|
|
|
|
|
|
|
2015-09-30 12:03:46 +02:00
|
|
|
#define DEBUG_LEVEL 0 // TODO has to be defined somewhere else (cython compil flag?)
|
2015-06-18 17:19:23 +02:00
|
|
|
|
|
|
|
|
2016-04-08 15:38:57 +02:00
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
|
|
|
|
2015-07-20 11:38:43 +02:00
|
|
|
|
2016-04-25 18:02:58 +02:00
|
|
|
int count_dir(char* dir_path)
|
2016-04-08 15:38:57 +02:00
|
|
|
{
|
2016-04-25 18:02:58 +02:00
|
|
|
struct dirent* dp;
|
|
|
|
DIR* fd;
|
|
|
|
int count;
|
2016-04-08 15:38:57 +02:00
|
|
|
|
|
|
|
count = 0;
|
2016-04-25 18:02:58 +02:00
|
|
|
if ((fd = opendir(dir_path)) == NULL)
|
2016-04-08 15:38:57 +02:00
|
|
|
{
|
|
|
|
obi_set_errno(OBI_UTILS_ERROR);
|
2016-04-25 18:02:58 +02:00
|
|
|
obidebug(1, "Error opening a directory: %s\n", dir_path);
|
2016-04-08 15:38:57 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
while ((dp = readdir(fd)) != NULL)
|
|
|
|
{
|
|
|
|
if ((dp->d_name)[0] == '.')
|
|
|
|
continue;
|
|
|
|
count++;
|
|
|
|
}
|
2016-04-25 18:02:58 +02:00
|
|
|
|
2016-04-08 15:38:57 +02:00
|
|
|
return count;
|
|
|
|
}
|
2016-04-25 18:02:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|