Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
	new file:   Makefile
	new file:   Readme.md
	new file:   main.c
  • Loading branch information
Kuraanal committed Dec 28, 2023
0 parents commit daadb17
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
main: main.c
gcc -o svg-deoptimise main.c
30 changes: 30 additions & 0 deletions Readme.md
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
```
79 changes: 79 additions & 0 deletions main.c
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;
}

0 comments on commit daadb17

Please sign in to comment.