Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Meow committed Apr 9, 2019
0 parents commit 4cbf407
Show file tree
Hide file tree
Showing 18 changed files with 1,398 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.vscode
*/.vscode
gmsv_file/.vs/
gmsv_file/Release/
gmsv_file/Debug/
gmsv_file/bin/
gmsv_file/project/
*.sln
*.vcxproj
*.vcproj
*.filters
*.user
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Luna D.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# gmsv_file
A simple file i/o library for Garry's Mod written in ANSI C.

## Installing
1. Go to the "Releases" of this GitHub repository.
2. Download the latest `.dll` file for your platform.
3. Copy that file to `garrysmod/lua/bin` of your server.

## Building
1. Install premake5
2. Use `premake5` followed by your platform (`vs2015` for windows, `gmake` for linux).
3. Navigate to the `project` folder.
4. If on Windows, open the `.sln` file, in on Linux, run `make`.
5. The compiled binary should be in the `gmsv_file/bin` folder.

## Using
The module provides barebones functionality for file manipulation. It's being worked on, more features are coming soon!
(All functions are relative to `garrysmod/` folder!!)

```lua
--- Appends contents at the end of file.
-- @return[Boolean success]
File.append(filename, contents)

--- Deletes file.
-- @return[Boolean success]
File.delete(filename)

--- Creates a folder with specified name.
-- Returns false if the folder exists, or was unable to be created.
-- @return[Boolean success]
File.mkdir(directory)

--- Reads specified file.
-- @return[String contents]
File.read(filename)

--- Writes contents to file. Overwrites existing contents.
-- @return[Boolean success]
File.write(filename, contents)
```
76 changes: 76 additions & 0 deletions gmsv_file/premake5.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
local config = {
garrysmod = '/home/example/server',
libname = 'gmsv_file_'..(os.target() == 'windows' and 'win32' or 'linux')..'.dll',
std = 'C89'
}

local is_windows = os.target() == 'windows'
local is_linux = not is_windows
local ansi_c = string.lower(config.std) == 'c89' or string.lower(config.std) == 'ansi'
local suffix = is_windows and '_win' or '_linux'

workspace 'file'
location './project'
configurations { 'x86', 'x86_64' }
flags { 'NoPCH', 'NoImportLib'}
symbols 'On'
editandcontinue 'Off'
vectorextensions 'SSE'
defines { 'NDEBUG' }
optimize 'Full'
floatingpoint 'Fast'

-- Link stdlib statically for compatibility.
if is_linux then
linkoptions{ '-static-libstdc++', '-static-libgcc' }
else
staticruntime 'on'
end

filter "configurations:x86"
architecture 'x86'
targetsuffix(suffix..(is_windows and '32' or ''))

filter "configurations:x86_64"
architecture 'x86_64'
targetsuffix(suffix..'64')

project 'file'
kind 'SharedLib'
language 'C'
location './project'
targetdir './bin'
-- libdirs { '../lib/'..string.lower(os.target()) }
includedirs { '../include' }
targetprefix 'gmsv_'
targetextension '.dll'

if is_linux then
if ansi_c then
buildoptions { '-ansi', '-pedantic' }
end

pic 'On'
end

language 'C++'
files { '../include/GarrysMod/Lua/CCompat.cpp' }
language 'C'
files {
'src/**.c',
'src/**.h'
}

--[[
if is_windows then
links { }
else
links { }
end
--]]

--[[
postbuildcommands {
'{COPY} "%{cfg.buildtarget.abspath}" "'..config.garrysmod..'/garrysmod/lua/bin/'..config.libname..'"*',
}
]]
105 changes: 105 additions & 0 deletions gmsv_file/src/file.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include "file.h"

#ifdef _WIN32
extern int getcwd(char *buf, size_t size);
extern int mkdir(const char *dirname);
#endif

char* concat(const char *s1, const char *s2) {
char *result = malloc(strlen(s1) + strlen(s2) + 1);
strcpy(result, s1);
strcat(result, s2);
return result;
}

void setup_directory(char **directory) {
char current_folder[256], *result;
getcwd(current_folder, sizeof(current_folder));
result = concat(current_folder, "/garrysmod/");
*directory = concat(result, *directory);
free(result);
}

int file_write(const char *filename, void *data, size_t len) {
FILE *f;
char *fn = (char*)filename;

setup_directory(&fn);

f = fopen(fn, "wb");

if (f == NULL) {
return 0;
}

fwrite(data, 1, len, f);
fclose(f);

return 1;
}

int file_append(const char *filename, void *data, size_t len) {
FILE *f;
char *fn = (char*)filename;

setup_directory(&fn);

f = fopen(fn, "ab");

if (f == NULL) {
return 0;
}

fseek(f, 0, SEEK_END);
fwrite(data, 1, len, f);
fclose(f);

return 1;
}

int file_read(const char *filename, char **out) {
FILE *f;
int len;
char *buf, *fn = (char*)filename;

setup_directory(&fn);

f = fopen(fn, "rb");

if (f == NULL) {
return 0;
}

fseek(f, 0L, SEEK_END);
len = ftell(f);
fseek(f, 0L, SEEK_SET);
buf = (char*)malloc(len + 1);

if (!buf) {
fclose(f);
return 0;
}

fread(buf, 1, len, f);
fclose(f);

buf[len] = '\0';

*out = buf;

return 1;
}

int file_delete(const char *filename) {
char *fn = (char*)filename;
setup_directory(&fn);

return remove(fn) == 0 ? 1 : 0;
}

int file_mkdir(const char *dirname) {
char *fn = (char*)dirname;
setup_directory(&fn);

return mkdir(fn) == 0 ? 1 : 0;
}
20 changes: 20 additions & 0 deletions gmsv_file/src/file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef GMSV_FILE_FILE_H
#define GMSV_FILE_FILE_H

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

#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif

int file_write(const char *filename, void *data, size_t len);
int file_append(const char *filename, void *data, size_t len);
int file_read(const char *filename, char **buffer);
int file_delete(const char *filename);
int file_mkdir(const char *dirname);

#endif
Loading

0 comments on commit 4cbf407

Please sign in to comment.