-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new file: Makefile new file: Readme.md new file: main.c
- Loading branch information
0 parents
commit daadb17
Showing
3 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
main: main.c | ||
gcc -o svg-deoptimise main.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# svg-deoptimise | ||
|
||
`svg-deoptimise` is a simple C program designed to improve the readability of SVG files. Optimized SVG files often have all their content in a single line, making it challenging for humans to read and understand. | ||
|
||
This program takes a destination folder and source SVG files as arguments, then outputs a deoptimized version of each file with proper line breaks for improved readability. | ||
|
||
## Usage | ||
|
||
```bash | ||
./svg-deoptimise <destination_folder> <source_svg_file_1> <source_svg_file_2> ... | ||
``` | ||
|
||
- `<destination_folder>`: The folder where deoptimized SVG files will be saved. | ||
- `<source_svg_file_1> <source_svg_file_2> ...`: List of source SVG files to be deoptimized. | ||
|
||
### Example | ||
|
||
```bash | ||
./svg-deoptimise output_folder input_file1.svg input_file2.svg | ||
``` | ||
|
||
This command will deoptimize `input_file1.svg` and `input_file2.svg`, saving the deoptimized versions in the `output_folder`. | ||
|
||
## Building the Program | ||
|
||
To build the program, use a C compiler. For example, with `gcc`: | ||
|
||
```bash | ||
gcc -o svg-deoptimise main.c | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
char* getFileNameFromPath(char* path) | ||
{ | ||
for(size_t i = strlen(path) - 1; i; i--) | ||
{ | ||
if (path[i] == '/') | ||
{ | ||
return &path[i+1]; | ||
} | ||
} | ||
return path; | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
if(argc <= 2) | ||
{ | ||
printf("Missing arguments\n"); | ||
printf("%s destinationfolder/ filename ...\n", argv[0]); | ||
return 1; | ||
} | ||
|
||
char *dest = argv[1]; | ||
|
||
for(int i = 2; i < argc; i++) | ||
{ | ||
char *src_file = argv[i]; | ||
|
||
char *filename = getFileNameFromPath(src_file); | ||
char dst_file[100]; | ||
|
||
strcpy(dst_file, dest); | ||
strcat(dst_file, filename); | ||
|
||
FILE *output, *input; | ||
|
||
input = fopen(src_file, "r"); | ||
output = fopen(dst_file, "w"); | ||
|
||
if(input == NULL) | ||
{ | ||
printf("File Error: %s\n", src_file); | ||
return 1; | ||
} | ||
|
||
if(output == NULL) | ||
{ | ||
printf("File Error: %s\n", dst_file); | ||
return 1; | ||
} | ||
|
||
char buffer = '\0'; | ||
char previous_char = '\0'; | ||
char two_previous_char = '\0'; | ||
|
||
while (fread(&buffer, sizeof(char), 1, input) != 0) | ||
{ | ||
if(buffer != '/' && previous_char == '<' && two_previous_char == '>') | ||
{ | ||
fseek(output, -1, SEEK_END); | ||
fprintf(output, "%c%c%c", '\n', '<', buffer); | ||
} | ||
else | ||
fprintf(output, "%c", buffer); | ||
|
||
two_previous_char = previous_char; | ||
previous_char = buffer; | ||
} | ||
|
||
fclose(input); | ||
fclose(output); | ||
printf("Finished: %s\n", src_file); | ||
} | ||
|
||
return 0; | ||
} |