static const char *default_out = "default.pgm";
static const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
+// I copied the checking logic here to main, because I thought the open_files
+// should not be doing input checking, but I will leave it here because
+// I feel it should stay here also
+// I just don't check the return value
+//
+// Another way would be to pass the index value from main to open_files through
+// the in_fd and out_fd integers (as input-output arguments) I don't think that
+// is terribly clean idea either though...
+int get_file_index(int argc, char **argv)
+{
+ int i;
+
+ i = 1;
+ while (i < argc && argv[i][0] == '-')
+ ++i;
+ if (argc == i || argc > i + 2)
+ return (0);
+ return (i);
+}
+
int open_files(int argc, char **argv, int *in_fd, int *out_fd)
{
- char *line;
+ char *line;
+ int file_index;
- if (argc == 3)
- *out_fd = open(argv[2], O_WRONLY | O_CREAT | O_EXCL, mode);
+ file_index = get_file_index(argc, argv);
+ if (argc > file_index)
+ *out_fd = open(argv[file_index + 1], O_WRONLY | O_CREAT | O_EXCL, mode);
else
*out_fd = open(default_out, O_WRONLY | O_CREAT, mode);
while (*out_fd < 0 && errno == EEXIST)
}
else if (!ft_strcmp(line, "y\n"))
{
- *out_fd = open(argv[2], O_WRONLY | O_CREAT, mode);
+ *out_fd = open(argv[file_index + 1], O_WRONLY | O_CREAT, mode);
free(line);
if (*out_fd < 0)
return (2);
}
}
- *in_fd = open(argv[1], O_RDONLY);
+ *in_fd = open(argv[file_index], O_RDONLY);
if (*in_fd >= 0)
return (0);
close(*out_fd);
return (2);
}
-void print_pgm_header(int fd)
+void print_pgm_header(int fd, int raw)
{
- ft_dprintf(fd, "P5\n256 256\n255\n");
+ if (raw)
+ ft_dprintf(fd, "P5\n256 256\n255\n");
+ else
+ ft_dprintf(fd, "P2\n256 256\n255\n");
return ;
}
return (min);
}
-void print_image(int fd, t_mat *mat)
+void print_image(int fd, t_mat *mat, int raw)
{
size_t i;
size_t j;
while (j < 256)
{
cell = *(t_count *)ft_mat_access(mat, i, j);
- ft_dprintf(fd, "%c", (char)(log(cell - min + 1) / log(max - min + 1) * 255));
+ if (raw)
+ ft_dprintf(fd, "%c", (char)(log(cell - min + 1) / log(max - min + 1) * 255));
+ else
+ ft_dprintf(fd, " %i", (int)(log(cell - min + 1) / log(max - min + 1) * 255));
++j;
}
+ if (!raw)
+ ft_dprintf(fd, "\n");
++i;
}
return ;
}
-void file_processing(int in_fd, int out_fd)
+void file_processing(int in_fd, int out_fd, int raw)
{
size_t i;
unsigned char buffer[BUFFER_SIZE];
++i;
}
}
- print_pgm_header(out_fd);
- print_image(out_fd, &matrix);
+ print_pgm_header(out_fd, raw);
+ print_image(out_fd, &matrix, raw);
ft_mat_free(&matrix, NULL);
return ;
}
{
ft_printf("This is the consebit tool by Lukáš Jiřiště\n"
"This tool plots frequency of pairs of consecutive bits.\n\n"
- "consebit <input_file> [<output_file>]\n");
+ "consebit [OPTIONS...] <input_file> [<output_file>]\n\n"
+ "OPTIONS:\n"
+ "\t-h, --help\n"
+ "\t\tprints this help\n\n"
+ "\t-p, --plain\n"
+ "\t\toutputs the image in plain (ASCII) rather than raw (binary) PPM\n");
return ;
}
int in_fd;
int out_fd;
int res;
+ int i;
+ int raw;
- if (argc < 2 || argc > 3)
+ if (argc < 2 || !get_file_index(argc, argv))
{
print_help();
return (1);
}
+ raw = 1;
+ i = 1;
+ while (i < argc)
+ {
+ if (!ft_strcmp(argv[i], "-h") || !ft_strcmp(argv[i], "--help"))
+ {
+ print_help();
+ return (0);
+ }
+ if (!ft_strcmp(argv[i], "-p") || !ft_strcmp(argv[i], "--plain"))
+ raw = 0;
+ ++i;
+ }
res = open_files(argc, argv, &in_fd, &out_fd);
if (res != 0)
{
ft_printf("There was a problem opening a file.\n");
return (res);
}
- file_processing(in_fd, out_fd);
+ file_processing(in_fd, out_fd, raw);
close(in_fd);
close(out_fd);
return (0);