Skip to content

Commit

Permalink
project added
Browse files Browse the repository at this point in the history
*Packing and unpacking of png files has been implemented.
*Windows only.
*Bug with the path and bug with the binary file name when using a custom path.
  • Loading branch information
Besdroxk committed Sep 25, 2023
0 parents commit 32ea9aa
Show file tree
Hide file tree
Showing 3 changed files with 247 additions and 0 deletions.
74 changes: 74 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# This file is used to ignore files which are generated
# ----------------------------------------------------------------------------

*~
*.autosave
*.a
*.core
*.moc
*.o
*.obj
*.orig
*.rej
*.so
*.so.*
*_pch.h.cpp
*_resource.rc
*.qm
.#*
*.*#
core
!core/
tags
.DS_Store
.directory
*.debug
Makefile*
*.prl
*.app
moc_*.cpp
ui_*.h
qrc_*.cpp
Thumbs.db
*.res
*.rc
/.qmake.cache
/.qmake.stash

# qtcreator generated files
*.pro.user*
CMakeLists.txt.user*

# xemacs temporary files
*.flc

# Vim temporary files
.*.swp

# Visual Studio generated files
*.ib_pdb_index
*.idb
*.ilk
*.pdb
*.sln
*.suo
*.vcproj
*vcproj.*.*.user
*.ncb
*.sdf
*.opensdf
*.vcxproj
*vcxproj.*

# MinGW generated files
*.Debug
*.Release

# Python byte code
*.pyc

# Binaries
# --------
*.dll
*.exe

13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.5)

project(Photo-Packer LANGUAGES C)

add_executable(Photo-Packer main.c)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static")

include(GNUInstallDirs)
install(TARGETS Photo-Packer
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
160 changes: 160 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>

#define MAX_PATH_LENGTH 256

int pack(const char *path)
{
DIR *dir;
struct dirent *files;
dir = opendir(path);

if(dir == NULL)
{
perror("Error opening directory");
return -1;
}

char binPath[MAX_PATH_LENGTH];
snprintf(binPath, sizeof(binPath), "%s.asset", path);

FILE *bin = fopen(binPath, "wb");

if(bin == NULL)
{
perror("Error creating binary file");
return -1;
}

while((files = readdir(dir)) != NULL)
{
if(strstr(files->d_name, ".png") != NULL)
{
char filePath[MAX_PATH_LENGTH];
snprintf(filePath, sizeof(filePath), "%s/%s", path, files->d_name);

FILE *photo = fopen(filePath, "rb");

if(photo == NULL)
{
perror("Error opening a photo file");
return -1;
}

fseek(photo, 0, SEEK_END);
long size = ftell(photo);
fseek(photo, 0, SEEK_SET);

fwrite(&size, sizeof(size), 1, bin);

char data[size];
fread(data, 1, size, photo);

fwrite(files->d_name, sizeof(files->d_name), 1, bin);
fwrite(data, 1, size, bin);

fclose(photo);
}
}

closedir(dir);
fclose(bin);
return 0;
}

int unpack(const char *path)
{
char binPath[MAX_PATH_LENGTH];
snprintf(binPath, sizeof(binPath), "%s.asset", path);

FILE *bin = fopen(binPath, "rb");

if(bin == NULL)
{
perror("Error opening binary file");
return -1;
}

struct stat st = {0};

if(stat(path, &st) == -1) mkdir(path);

while(!feof(bin))
{
long size;
fread(&size, sizeof(size), 1, bin);

char name[256];
fread(name, sizeof(name), 1, bin);

char filePath[MAX_PATH_LENGTH];
snprintf(filePath, sizeof(filePath), "%s/%s", path, name);

FILE *photo = fopen(filePath, "wb");

if(photo == NULL)
{
perror("Error creating photo file");
return -1;
}

char data[size];
fread(data, 1, size, bin);

fwrite(data, 1, size, photo);
fclose(photo);
}

fclose(bin);
return 0;
}

int main(int argc, char *argv[])
{
if(argc == 1)
{
printf("Usage: %s --pack|--unpack [path]\n", argv[0]);
return -1;
}

char path[MAX_PATH_LENGTH];

if(argc == 3)
{
if(strlen(argv[2]) < MAX_PATH_LENGTH)
{
strncpy(path, argv[2], MAX_PATH_LENGTH);
strncat(path, "\\", MAX_PATH_LENGTH - strlen(path));
}
else
{
fprintf(stderr, "Path is too long.\n");
return -1;
}
}
else
{
if(getcwd(path, sizeof(path)) == NULL)
{
perror("Error getting current directory");
return -1;
}
}

if(strcmp(argv[1], "--pack") == 0)
{
return pack(path);
}
else if(strcmp(argv[1], "--unpack") == 0)
{
return unpack(path);
}
else
{
fprintf(stderr, "Unknown command: %s\n", argv[1]);
return -1;
}
}

0 comments on commit 32ea9aa

Please sign in to comment.