Tiffy is a static website generator that uses Lizard markup files to create a website.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

141 lines
2.2 KiB

#define _XOPEN_SOURCE 500
#include <ftw.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
void (* build_cb)(const char *);
int exists( const char * f ) {
struct stat buf;
return ( stat( f, &buf ) == 0);
}
int isdir( const char * f ) {
struct stat buf;
if ( stat( f, &buf ) == -1 ) {
perror( "isdir() stat" );
exit(EXIT_FAILURE);
}
if ( ( buf.st_mode & S_IFMT ) == S_IFDIR ) {
return 1;
}
return 0;
}
int filesize( const char * f ) {
FILE * fp;
fp = fopen( f, "r" );
if ( fp == NULL ) {
perror( "Unable to open file: " );
perror( f );
return -1;
}
fseek( fp, 0, SEEK_END );
long fsize = ftell( fp );
rewind( fp );
fclose( fp );
return fsize;
}
void readfile( const char * f, char ** str ) {
size_t fsz = filesize( f );
*str = (char *) malloc( fsz + 1 );
if ( *str == NULL ) {
perror( "ERROR: " );
return;
}
FILE * fp = fopen ( f, "r" );
if ( f == NULL ) {
perror( "ERROR: " );
free( *str );
*str = NULL;
return;
}
int rb = fread( *str, 1, fsz, fp );
if ( rb < (int)fsz ) {
perror( "ERROR" );
free( *str );
*str = NULL;
};
*(*str + rb) = 0;
fclose( fp );
}
void writefile( const char * f, const char * s ) {
FILE * fp = fopen ( f, "w" );
if ( f == NULL ) {
perror( "Unable to open file: " );
perror( f );
return;
}
int r = fputs( s, fp );
if (r == EOF) {
perror( "Error writing file: " );
}
fclose( fp );
}
char broot[ 300 ] = {};
int unlink_cb( const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf ) {
// dont remove the actual build dir
if ( strcmp( fpath, broot ) == 0 ) {
return 0;
}
// dont remove the git dir
if ( strstr( fpath, ".git" ) != NULL ) {
return 0;
}
int rv = remove( fpath );
if ( rv )
perror( fpath );
return rv;
}
int cleanbuild( const char *path ) {
sprintf( broot, "%s", path );
return nftw( path, unlink_cb, 64, FTW_DEPTH | FTW_PHYS );
}
int b_cb( const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf ) {
build_cb( fpath );
return 0;
}
int dobuild( const char *path, void ( *fn ) ( const char * path ) ) {
build_cb = fn;
return nftw( path, b_cb, 64, FTW_DEPTH | FTW_PHYS );
}