Skip to content

Commit 32ea9aa

Browse files
committed
project added
*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.
0 parents  commit 32ea9aa

File tree

3 files changed

+247
-0
lines changed

3 files changed

+247
-0
lines changed

.gitignore

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# This file is used to ignore files which are generated
2+
# ----------------------------------------------------------------------------
3+
4+
*~
5+
*.autosave
6+
*.a
7+
*.core
8+
*.moc
9+
*.o
10+
*.obj
11+
*.orig
12+
*.rej
13+
*.so
14+
*.so.*
15+
*_pch.h.cpp
16+
*_resource.rc
17+
*.qm
18+
.#*
19+
*.*#
20+
core
21+
!core/
22+
tags
23+
.DS_Store
24+
.directory
25+
*.debug
26+
Makefile*
27+
*.prl
28+
*.app
29+
moc_*.cpp
30+
ui_*.h
31+
qrc_*.cpp
32+
Thumbs.db
33+
*.res
34+
*.rc
35+
/.qmake.cache
36+
/.qmake.stash
37+
38+
# qtcreator generated files
39+
*.pro.user*
40+
CMakeLists.txt.user*
41+
42+
# xemacs temporary files
43+
*.flc
44+
45+
# Vim temporary files
46+
.*.swp
47+
48+
# Visual Studio generated files
49+
*.ib_pdb_index
50+
*.idb
51+
*.ilk
52+
*.pdb
53+
*.sln
54+
*.suo
55+
*.vcproj
56+
*vcproj.*.*.user
57+
*.ncb
58+
*.sdf
59+
*.opensdf
60+
*.vcxproj
61+
*vcxproj.*
62+
63+
# MinGW generated files
64+
*.Debug
65+
*.Release
66+
67+
# Python byte code
68+
*.pyc
69+
70+
# Binaries
71+
# --------
72+
*.dll
73+
*.exe
74+

CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
3+
project(Photo-Packer LANGUAGES C)
4+
5+
add_executable(Photo-Packer main.c)
6+
7+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static")
8+
9+
include(GNUInstallDirs)
10+
install(TARGETS Photo-Packer
11+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
12+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
13+
)

main.c

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <dirent.h>
5+
#include <sys/stat.h>
6+
7+
#define MAX_PATH_LENGTH 256
8+
9+
int pack(const char *path)
10+
{
11+
DIR *dir;
12+
struct dirent *files;
13+
dir = opendir(path);
14+
15+
if(dir == NULL)
16+
{
17+
perror("Error opening directory");
18+
return -1;
19+
}
20+
21+
char binPath[MAX_PATH_LENGTH];
22+
snprintf(binPath, sizeof(binPath), "%s.asset", path);
23+
24+
FILE *bin = fopen(binPath, "wb");
25+
26+
if(bin == NULL)
27+
{
28+
perror("Error creating binary file");
29+
return -1;
30+
}
31+
32+
while((files = readdir(dir)) != NULL)
33+
{
34+
if(strstr(files->d_name, ".png") != NULL)
35+
{
36+
char filePath[MAX_PATH_LENGTH];
37+
snprintf(filePath, sizeof(filePath), "%s/%s", path, files->d_name);
38+
39+
FILE *photo = fopen(filePath, "rb");
40+
41+
if(photo == NULL)
42+
{
43+
perror("Error opening a photo file");
44+
return -1;
45+
}
46+
47+
fseek(photo, 0, SEEK_END);
48+
long size = ftell(photo);
49+
fseek(photo, 0, SEEK_SET);
50+
51+
fwrite(&size, sizeof(size), 1, bin);
52+
53+
char data[size];
54+
fread(data, 1, size, photo);
55+
56+
fwrite(files->d_name, sizeof(files->d_name), 1, bin);
57+
fwrite(data, 1, size, bin);
58+
59+
fclose(photo);
60+
}
61+
}
62+
63+
closedir(dir);
64+
fclose(bin);
65+
return 0;
66+
}
67+
68+
int unpack(const char *path)
69+
{
70+
char binPath[MAX_PATH_LENGTH];
71+
snprintf(binPath, sizeof(binPath), "%s.asset", path);
72+
73+
FILE *bin = fopen(binPath, "rb");
74+
75+
if(bin == NULL)
76+
{
77+
perror("Error opening binary file");
78+
return -1;
79+
}
80+
81+
struct stat st = {0};
82+
83+
if(stat(path, &st) == -1) mkdir(path);
84+
85+
while(!feof(bin))
86+
{
87+
long size;
88+
fread(&size, sizeof(size), 1, bin);
89+
90+
char name[256];
91+
fread(name, sizeof(name), 1, bin);
92+
93+
char filePath[MAX_PATH_LENGTH];
94+
snprintf(filePath, sizeof(filePath), "%s/%s", path, name);
95+
96+
FILE *photo = fopen(filePath, "wb");
97+
98+
if(photo == NULL)
99+
{
100+
perror("Error creating photo file");
101+
return -1;
102+
}
103+
104+
char data[size];
105+
fread(data, 1, size, bin);
106+
107+
fwrite(data, 1, size, photo);
108+
fclose(photo);
109+
}
110+
111+
fclose(bin);
112+
return 0;
113+
}
114+
115+
int main(int argc, char *argv[])
116+
{
117+
if(argc == 1)
118+
{
119+
printf("Usage: %s --pack|--unpack [path]\n", argv[0]);
120+
return -1;
121+
}
122+
123+
char path[MAX_PATH_LENGTH];
124+
125+
if(argc == 3)
126+
{
127+
if(strlen(argv[2]) < MAX_PATH_LENGTH)
128+
{
129+
strncpy(path, argv[2], MAX_PATH_LENGTH);
130+
strncat(path, "\\", MAX_PATH_LENGTH - strlen(path));
131+
}
132+
else
133+
{
134+
fprintf(stderr, "Path is too long.\n");
135+
return -1;
136+
}
137+
}
138+
else
139+
{
140+
if(getcwd(path, sizeof(path)) == NULL)
141+
{
142+
perror("Error getting current directory");
143+
return -1;
144+
}
145+
}
146+
147+
if(strcmp(argv[1], "--pack") == 0)
148+
{
149+
return pack(path);
150+
}
151+
else if(strcmp(argv[1], "--unpack") == 0)
152+
{
153+
return unpack(path);
154+
}
155+
else
156+
{
157+
fprintf(stderr, "Unknown command: %s\n", argv[1]);
158+
return -1;
159+
}
160+
}

0 commit comments

Comments
 (0)