Skip to content

Instantly share code, notes, and snippets.

@LukeMS
Last active November 5, 2017 12:11
Show Gist options
  • Save LukeMS/3a4a370247b58dc52dff35b7884dc815 to your computer and use it in GitHub Desktop.
Save LukeMS/3a4a370247b58dc52dff35b7884dc815 to your computer and use it in GitHub Desktop.
allegro5
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <zlib.h> // for the adler-32 checksum
#include <allegro5/allegro.h>
//required by fmtime
#include <sys/types.h>
#include <sys/stat.h>
#ifndef WIN32 // linking -lmingw32 will be required
#include <unistd.h>
#endif
#ifdef WIN32
#define stat _stat
#endif
/*
Get file modification time (stackoverflow.com/a/40504396/5496529)
*/
uint32_t fmtime(const char* filename) {
if(stat(filename, &fmtime_result)==0) {
return fmtime_result.st_mtime;
}
return 0;
}
/*
Get the extension of a file
*/
const char* get_file_ext(const char* fspec) {
char *e = strrchr (fspec, '.');
if (e == NULL) {
e = "";
} else {
++e;
}
return e;
}
/*
Iterate over each .png file at a folder and calculate an adler-32 checksum by feeding it the name and modification time of files only
(based on code from allegro.cc/forums/thread/606721/908205#target)
*/
int getDirectoryItems(const char* path) {
// name of each file during iteration
char *name;
// modification date of each file during iteration
uint32_t fmtime_v;
// running Adler-32 checksum
long unsigned int adler = adler32(0L, Z_NULL, 0);
ALLEGRO_FS_ENTRY* dir = al_create_fs_entry(path);
if(al_open_directory(dir)) {
ALLEGRO_FS_ENTRY* file;
while((file = al_read_directory(dir))) {
name = (char*)al_get_fs_entry_name(file);
if (!strcmp(get_file_ext(name), "png")) {
// adler consider bytes [0..len-1], so we pass the full length of a string
adler = adler32(adler, (const unsigned char*) name, sizeof(name));
fmtime_v = fmtime(name);
// adler consider bytes [0..len-1], so we pass the length of the number + 1
adler = adler32(adler, (const unsigned char*) &fmtime_v, sizeof(fmtime_v) + 1);
}
al_destroy_fs_entry(file);
}
}
printf("adler32 %lu\n", adler); // e.g.: 3391961898
al_destroy_fs_entry(dir);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment