/******************************************************************************
 * Steve Rider - stever@transmission23.com
 * QuickPics
 *   Image galleries for the web made easy
 *
 * utility.h - miscellaneous routines
 *
 *****************************************************************************/
/* 17 February 98 */

#ifndef UTILITY_H
#define UTILITY_H

#include <time.h>
#include "qp.h"

#if HAVE_SYS_NDIR_H || HAVE_SYS_DIR_H || HAVE_NDIR_H
# define dirent direct
# define NAMLEN(dirent) (dirent)->d_namlen
# if HAVE_SYS_NDIR_H
#  include <sys/ndir.h>
# endif
# if HAVE_SYS_DIR_H
#  include <sys/dir.h>
# endif
# if HAVE_NDIR_H
#  include <ndir.h>
# endif
#else
# include <dirent.h>
# define NAMLEN(dirent) Extent((dirent)->d_name)
#endif
#include <pwd.h>

#if HAVE_FCNTL_H
#include <fcntl.h>
#endif

#if !defined(S_ISDIR)
#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
#endif

#define EXTRA_CHAR(q)  q == 1 ? "" : "s"
#define EXTRA_SPC(q)  q < 10 ? " " : ""
#define isequal(s1,s2)  strcasecmp(s1,s2) == 0
#define isequalcase(s1,s2)  strcmp(s1,s2) == 0
#define upcase(c) (((c)>='a' && (c)<='z') ? (c)-'a'+'A' : (c))

#define nmalloc(x)	n_malloc((x),__FILE__,__LINE__)
#define nfree(x)	n_free((x),__FILE__,__LINE__)

#define TIME_HOUR 1
#define TIME_MIN 2
#define TIME_SEC 3
#define TIME_HOUR_0 4
#define TIME_MIN_0 5
#define TIME_SEC_0 6
#define DATE_MONTH 1
#define DATE_DAY_0 2
#define DATE_YEAR 3
#define DATE_DAY 4
#define DATE_YEAR_19 5
#define DATE_MONTH_0 6

#define BasenameSeparator  "/"
#define DirectorySeparator  "/"
#define SystemCommand(command)  system(command)
#define TemporaryTemplate  "%s/qpXXXXXX"

/* File stuff */
char	IsAccessible(const char *filename);
char	IsDirectory(const char *filename);
char	**ListFiles(const char *dir_name, int *number_entries);
u_long	GetFilesize(const char *filename);
int	copyfile(char *oldpath, char *newpath);
int	movefile(char *oldpath, char *newpath);
int	isfile(char *filename);
char	*readline(FILE *f, char *s);

/* Date and Time stuff */
void	timet_to_time_short(time_t tt, char *time);
void	timet_to_date_short(time_t time, char *date);
void	timet_to_dateonly_short(time_t time, char *date);
void	timet_to_ago_short(time_t when, char *ago);
void	time_to_elap_short(time_t dur, char *elap);
void	timet_to_ago_long(time_t when, char *ago);
void	timet_to_elap_short(time_t t, char *elap);

/* Formatting stuff */
void	bytes_to_kb(u_long bytes, char *kb);
void	comma_number(char *string, u_long sum);

/* String stuff */
void	splitc(char *first, char *rest, char divider);
void	split(char *first, char *rest);
void	rmspace(char *s);
int	rmquotes(char *s);
int	isanumber(char *s);
void	str_element(char *out, char *in, int item);
void	quoted_str_element(char *out, char *in, int item);
int	num_elements(char *s);
void	AppendImageFormat(const char *format, char *filename);
char	*BaseFilename(const char *name);
char	*FilenameExtension(const char *name);

/* Memory stuff */
void	mfree(void *ptr);
void	*n_malloc(int size, char *file, int line);
void	n_free(void *ptr, char *file, int line);
void	mstrcpy(char **dest, const char *src);

/* Calculation stuff */
int	nearest(float actual, int low, int high);


#endif /* UTILITY_H */
