Skip to content

Commit

Permalink
Merge pull request #16 from Blizzard/pr10
Browse files Browse the repository at this point in the history
add os.writefile_ifnotequal
  • Loading branch information
starkos committed May 7, 2015
2 parents 613cd3f + 4723ca7 commit f233bd7
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 21 deletions.
91 changes: 91 additions & 0 deletions src/host/os_writefile_ifnotequal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* \file os_copyfile.c
* \brief Copy a file from one location to another.
* \author Copyright (c) 2002-2008 Jason Perkins and the Premake project
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "premake.h"

#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif

static int compare_file(const char* content, size_t length, const char* dst)
{
FILE* file = fopen(dst, "rb");
long size;
char buffer[4096];
int num;

if (file == NULL)
{
return FALSE;
}

// check sizes.
fseek(file, 0, SEEK_END);
size = ftell(file);
fseek(file, 0, SEEK_SET);

if (length != size)
{
fclose(file);
return FALSE;
}

while (size > 0)
{
num = size > 4096 ? 4096 : size;

fread(buffer, 1, num, file);

if (memcmp(content, buffer, num) != 0)
{
fclose(file);
return FALSE;
}

size -= num;
content += num;
}

fclose(file);
return TRUE;
}


int os_writefile_ifnotequal(lua_State* L)
{
FILE* file;
size_t length;
const char* content = luaL_checklstring(L, 1, &length);
const char* dst = luaL_checkstring(L, 2);

// if destination exist, and they are the same, no need to copy.
if (do_isfile(dst) && compare_file(content, length, dst))
{
lua_pushinteger(L, 0);
return 1;
}


file = fopen(dst, "wb");
if (file != NULL)
{
fwrite(content, 1, length, file);
fclose(file);

lua_pushinteger(L, 1);
return 1;
}

lua_pushinteger(L, -1);
lua_pushfstring(L, "unable to write file to '%s'", dst);
return 2;
}
43 changes: 22 additions & 21 deletions src/host/premake.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,28 @@ static const luaL_Reg path_functions[] = {
};

static const luaL_Reg os_functions[] = {
{ "chdir", os_chdir },
{ "chmod", os_chmod },
{ "copyfile", os_copyfile },
{ "_is64bit", os_is64bit },
{ "isdir", os_isdir },
{ "getcwd", os_getcwd },
{ "getversion", os_getversion },
{ "isfile", os_isfile },
{ "islink", os_islink },
{ "locate", os_locate },
{ "matchdone", os_matchdone },
{ "matchisfile", os_matchisfile },
{ "matchname", os_matchname },
{ "matchnext", os_matchnext },
{ "matchstart", os_matchstart },
{ "mkdir", os_mkdir },
{ "pathsearch", os_pathsearch },
{ "realpath", os_realpath },
{ "rmdir", os_rmdir },
{ "stat", os_stat },
{ "uuid", os_uuid },
{ "chdir", os_chdir },
{ "chmod", os_chmod },
{ "copyfile", os_copyfile },
{ "_is64bit", os_is64bit },
{ "isdir", os_isdir },
{ "getcwd", os_getcwd },
{ "getversion", os_getversion },
{ "isfile", os_isfile },
{ "islink", os_islink },
{ "locate", os_locate },
{ "matchdone", os_matchdone },
{ "matchisfile", os_matchisfile },
{ "matchname", os_matchname },
{ "matchnext", os_matchnext },
{ "matchstart", os_matchstart },
{ "mkdir", os_mkdir },
{ "pathsearch", os_pathsearch },
{ "realpath", os_realpath },
{ "rmdir", os_rmdir },
{ "stat", os_stat },
{ "uuid", os_uuid },
{ "writefile_ifnotequal", os_writefile_ifnotequal },
{ NULL, NULL }
};

Expand Down
1 change: 1 addition & 0 deletions src/host/premake.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ int os_realpath(lua_State* L);
int os_rmdir(lua_State* L);
int os_stat(lua_State* L);
int os_uuid(lua_State* L);
int os_writefile_ifnotequal(lua_State* L);
int string_endswith(lua_State* L);
int string_hash(lua_State* L);
int string_sha1(lua_State* L);
Expand Down

0 comments on commit f233bd7

Please sign in to comment.