commit d71164171ca09aec4ffa1def8cd4d7e8b21ffeb7
parent 3b7328ef708d8c20a46dfb41301b8b2e66dc4e3b
Author: ByteProject <stefan.vogt@byteproject.net>
Date: Sat, 21 Jan 2012 18:02:07 +0100
proper C99 port of the filesize() function known from PHP
Diffstat:
2 files changed, 71 insertions(+), 0 deletions(-)
diff --git a/ansilove/filesize.c b/ansilove/filesize.c
@@ -0,0 +1,45 @@
+//
+// filesize.m
+// AnsiLove/C
+//
+// Copyright (c) 2011, Stefan Vogt. All rights reserved.
+// http://byteproject.net
+//
+// Use of this source code is governed by a MIT-style license.
+// See the file LICENSE for details.
+//
+
+#if defined(__APPLE__) && defined(__MACH__)
+#import "filesize.h"
+#else
+#include "filesize.h"
+#endif
+
+int64_t filesize(char *filepath)
+{
+ // pointer to file at path
+ int64_t size;
+ FILE *file;
+
+ // To properly determine the size, we open it in binary mode.
+ file = fopen(filepath, "rb");
+
+ if(file != NULL)
+ {
+ // Error while seeking to end of file?
+ if(fseek(file, 0, SEEK_END)) {
+ rewind(file);
+ fclose(file);
+ return -1;
+ }
+
+ size = ftell(file);
+ // Close file and return the file size.
+ rewind(file);
+ fclose(file);
+ return size;
+ }
+
+ // In case we encounter an error.
+ return -1;
+}
diff --git a/ansilove/filesize.h b/ansilove/filesize.h
@@ -0,0 +1,26 @@
+//
+// filesize.h
+// AnsiLove/C
+//
+// Copyright (c) 2011, Stefan Vogt. All rights reserved.
+// http://byteproject.net
+//
+// Use of this source code is governed by a MIT-style license.
+// See the file LICENSE for details.
+//
+
+#if defined(__APPLE__) && defined(__MACH__)
+#import <Foundation/Foundation.h>
+#else
+#include <stdio.h>
+#include <stdlib.h>
+#endif
+
+#ifndef filesize_h
+#define filesize_h
+
+// Returns size of a file at a given path as integer.
+
+int64_t filesize(char *filepath);
+
+#endif