Skip to content
Snippets Groups Projects
Commit 8db2d0f6 authored by poulpe's avatar poulpe
Browse files

[Update] Add argparse library for CLI flag + fix memory leaks

parent 1f3a38a4
Branches
No related tags found
No related merge requests found
...@@ -3,7 +3,7 @@ CFLAGS = -std=c11 -Wall -Wextra -Wpedantic -Wwrite-strings -Walloc-zero -Wparent ...@@ -3,7 +3,7 @@ CFLAGS = -std=c11 -Wall -Wextra -Wpedantic -Wwrite-strings -Walloc-zero -Wparent
CFLAGS += -g -pedantic -fsanitize=address -fsanitize=leak -fsanitize=undefined CFLAGS += -g -pedantic -fsanitize=address -fsanitize=leak -fsanitize=undefined
LDFLAGS = -lm LDFLAGS = -lm
OBJECTS = $(patsubst %.c, %.o, $(wildcard *.c)) OBJECTS = $(patsubst %.c, %.o, $(wildcard *.c))
OBJECTS_TEST = $(patsubst %.c, %.o, $(wildcard tests/*.c)) OBJECTS_LIBS = $(patsubst %.c, %.o, $(wildcard libs/*.c))
OBJECTS_MUNIT = $(patsubst %.c, %.o, $(filter-out example.c , tests/munit/*.c)) OBJECTS_MUNIT = $(patsubst %.c, %.o, $(filter-out example.c , tests/munit/*.c))
OBJECTS_SRC := $(patsubst %.c, %.o, $(filter-out main.c, $(wildcard *.c))) OBJECTS_SRC := $(patsubst %.c, %.o, $(filter-out main.c, $(wildcard *.c)))
...@@ -26,9 +26,9 @@ OBJECTS_SRC := $(patsubst %.c, %.o, $(filter-out main.c, $(wildcard *.c))) ...@@ -26,9 +26,9 @@ OBJECTS_SRC := $(patsubst %.c, %.o, $(filter-out main.c, $(wildcard *.c)))
PROG = Exe PROG = Exe
TEST = test TEST = test
all: $(PROG) $(TEST) all: $(PROG)
$(PROG): $(OBJECTS) $(PROG): $(OBJECTS) $(OBJECTS_LIBS)
$(CC) $^ -o $@ $(CFLAGS) $(LDFLAGS) $(CC) $^ -o $@ $(CFLAGS) $(LDFLAGS)
$(TEST): $(OBJECTS_SRC) $(OBJECTS_TEST) $(OBJECTS_MUNIT) $(TEST): $(OBJECTS_SRC) $(OBJECTS_TEST) $(OBJECTS_MUNIT)
......
\relax
\providecommand\hyper@newdestlabel[2]{}
\providecommand\HyperFirstAtBeginDocument{\AtBeginDocument}
\HyperFirstAtBeginDocument{\ifx\hyper@anchor\@undefined
\global\let\oldcontentsline\contentsline
\gdef\contentsline#1#2#3#4{\oldcontentsline{#1}{#2}{#3}}
\global\let\oldnewlabel\newlabel
\gdef\newlabel#1#2{\newlabelxx{#1}#2}
\gdef\newlabelxx#1#2#3#4#5#6{\oldnewlabel{#1}{{#2}{#3}}}
\AtEndDocument{\ifx\hyper@anchor\@undefined
\let\contentsline\oldcontentsline
\let\newlabel\oldnewlabel
\fi}
\fi}
\global\let\hyper@last\relax
\gdef\HyperFirstAtBeginDocument#1{#1}
\providecommand\HyField@AuxAddToFields[1]{}
\providecommand\HyField@AuxAddToCoFields[2]{}
\providecommand\babel@aux[2]{}
\@nameuse{bbl@beforestart}
\catcode `:\active
\catcode `;\active
\catcode `!\active
\catcode `?\active
\babel@aux{french}{}
\@writefile{nav}{\headcommand {\slideentry {0}{0}{1}{1/1}{}{0}}}
\@writefile{nav}{\headcommand {\beamer@framepages {1}{1}}}
\providecommand*\caption@xref[2]{\@setref\relax\@undefined{#1}}
\newlabel{fig:task-list}{{1}{2}{\relax }{Doc-Start}{}}
\@writefile{snm}{\beamer@slide {fig:task-list}{2}}
\@writefile{nav}{\headcommand {\slideentry {0}{0}{2}{2/2}{}{0}}}
\@writefile{nav}{\headcommand {\beamer@framepages {2}{2}}}
\newlabel{fig:todolist}{{2}{3}{\relax }{Doc-Start}{}}
\@writefile{snm}{\beamer@slide {fig:todolist}{3}}
\@writefile{nav}{\headcommand {\slideentry {0}{0}{3}{3/3}{}{0}}}
\@writefile{nav}{\headcommand {\beamer@framepages {3}{3}}}
\@writefile{nav}{\headcommand {\slideentry {0}{0}{4}{4/4}{}{0}}}
\@writefile{nav}{\headcommand {\beamer@framepages {4}{4}}}
\newlabel{fig:c-classique}{{3}{5}{\relax }{Doc-Start}{}}
\@writefile{snm}{\beamer@slide {fig:c-classique}{5}}
\newlabel{fig:sdllogo}{{4}{5}{\relax }{Doc-Start}{}}
\@writefile{snm}{\beamer@slide {fig:sdllogo}{5}}
\@writefile{nav}{\headcommand {\slideentry {0}{0}{5}{5/5}{}{0}}}
\@writefile{nav}{\headcommand {\beamer@framepages {5}{5}}}
\newlabel{fig:fonctionnement}{{5}{6}{\relax }{Doc-Start}{}}
\@writefile{snm}{\beamer@slide {fig:fonctionnement}{6}}
\@writefile{nav}{\headcommand {\slideentry {0}{0}{6}{6/6}{}{0}}}
\@writefile{nav}{\headcommand {\beamer@framepages {6}{6}}}
\newlabel{fig:convcode}{{6}{7}{\relax }{Doc-Start}{}}
\@writefile{snm}{\beamer@slide {fig:convcode}{7}}
\@writefile{nav}{\headcommand {\slideentry {0}{0}{7}{7/7}{}{0}}}
\@writefile{nav}{\headcommand {\beamer@framepages {7}{7}}}
\newlabel{fig:negcode}{{7}{8}{\relax }{Doc-Start}{}}
\@writefile{snm}{\beamer@slide {fig:negcode}{8}}
\@writefile{nav}{\headcommand {\slideentry {0}{0}{8}{8/8}{}{0}}}
\@writefile{nav}{\headcommand {\beamer@framepages {8}{8}}}
\newlabel{fig:undrawpresentation62e1}{{8}{9}{\relax }{Doc-Start}{}}
\@writefile{snm}{\beamer@slide {fig:undrawpresentation62e1}{9}}
\@writefile{nav}{\headcommand {\slideentry {0}{0}{9}{9/9}{}{0}}}
\@writefile{nav}{\headcommand {\beamer@framepages {9}{9}}}
\@writefile{nav}{\headcommand {\slideentry {0}{0}{10}{10/10}{}{0}}}
\@writefile{nav}{\headcommand {\beamer@framepages {10}{10}}}
\newlabel{fig:question}{{9}{11}{\relax }{Doc-Start}{}}
\@writefile{snm}{\beamer@slide {fig:question}{11}}
\@writefile{nav}{\headcommand {\slideentry {0}{0}{11}{11/11}{}{0}}}
\@writefile{nav}{\headcommand {\beamer@framepages {11}{11}}}
\@writefile{nav}{\headcommand {\beamer@partpages {1}{11}}}
\@writefile{nav}{\headcommand {\beamer@subsectionpages {1}{11}}}
\@writefile{nav}{\headcommand {\beamer@sectionpages {1}{11}}}
\@writefile{nav}{\headcommand {\beamer@documentpages {11}}}
\@writefile{nav}{\headcommand {\gdef \inserttotalframenumber {11}}}
\gdef \@abspage@last{11}
This diff is collapsed.
\headcommand {\slideentry {0}{0}{1}{1/1}{}{0}}
\headcommand {\beamer@framepages {1}{1}}
\headcommand {\slideentry {0}{0}{2}{2/2}{}{0}}
\headcommand {\beamer@framepages {2}{2}}
\headcommand {\slideentry {0}{0}{3}{3/3}{}{0}}
\headcommand {\beamer@framepages {3}{3}}
\headcommand {\slideentry {0}{0}{4}{4/4}{}{0}}
\headcommand {\beamer@framepages {4}{4}}
\headcommand {\slideentry {0}{0}{5}{5/5}{}{0}}
\headcommand {\beamer@framepages {5}{5}}
\headcommand {\slideentry {0}{0}{6}{6/6}{}{0}}
\headcommand {\beamer@framepages {6}{6}}
\headcommand {\slideentry {0}{0}{7}{7/7}{}{0}}
\headcommand {\beamer@framepages {7}{7}}
\headcommand {\slideentry {0}{0}{8}{8/8}{}{0}}
\headcommand {\beamer@framepages {8}{8}}
\headcommand {\slideentry {0}{0}{9}{9/9}{}{0}}
\headcommand {\beamer@framepages {9}{9}}
\headcommand {\slideentry {0}{0}{10}{10/10}{}{0}}
\headcommand {\beamer@framepages {10}{10}}
\headcommand {\slideentry {0}{0}{11}{11/11}{}{0}}
\headcommand {\beamer@framepages {11}{11}}
\headcommand {\beamer@partpages {1}{11}}
\headcommand {\beamer@subsectionpages {1}{11}}
\headcommand {\beamer@sectionpages {1}{11}}
\headcommand {\beamer@documentpages {11}}
\headcommand {\gdef \inserttotalframenumber {11}}
Pres/pres.pdf 0 → 100644
This diff is collapsed.
\beamer@slide {fig:task-list}{2}
\beamer@slide {fig:todolist}{3}
\beamer@slide {fig:c-classique}{5}
\beamer@slide {fig:sdllogo}{5}
\beamer@slide {fig:fonctionnement}{6}
\beamer@slide {fig:convcode}{7}
\beamer@slide {fig:negcode}{8}
\beamer@slide {fig:undrawpresentation62e1}{9}
\beamer@slide {fig:question}{11}
File added
This diff is collapsed.
\babel@toc {french}{}
File added
This diff is collapsed.
/**
* Copyright (C) 2012-2015 Yecheng Fu <cofyc.jackson at gmail dot com>
* All rights reserved.
*
* Use of this source code is governed by a MIT-style license that can be found
* in the LICENSE file.
*/
#ifndef ARGPARSE_H
#define ARGPARSE_H
/* For c++ compatibility */
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
struct argparse;
struct argparse_option;
typedef int argparse_callback (struct argparse *self,
const struct argparse_option *option);
enum argparse_flag {
ARGPARSE_STOP_AT_NON_OPTION = 1,
};
enum argparse_option_type {
/* special */
ARGPARSE_OPT_END,
ARGPARSE_OPT_GROUP,
/* options with no arguments */
ARGPARSE_OPT_BOOLEAN,
ARGPARSE_OPT_BIT,
/* options with arguments (optional or required) */
ARGPARSE_OPT_INTEGER,
ARGPARSE_OPT_FLOAT,
ARGPARSE_OPT_STRING,
};
enum argparse_option_flags {
OPT_NONEG = 1, /* disable negation */
};
/**
* argparse option
*
* `type`:
* holds the type of the option, you must have an ARGPARSE_OPT_END last in your
* array.
*
* `short_name`:
* the character to use as a short option name, '\0' if none.
*
* `long_name`:
* the long option name, without the leading dash, NULL if none.
*
* `value`:
* stores pointer to the value to be filled.
*
* `help`:
* the short help message associated to what the option does.
* Must never be NULL (except for ARGPARSE_OPT_END).
*
* `callback`:
* function is called when corresponding argument is parsed.
*
* `data`:
* associated data. Callbacks can use it like they want.
*
* `flags`:
* option flags.
*/
struct argparse_option {
enum argparse_option_type type;
const char short_name;
const char *long_name;
void *value;
const char *help;
argparse_callback *callback;
intptr_t data;
int flags;
};
/**
* argpparse
*/
struct argparse {
// user supplied
const struct argparse_option *options;
const char *const *usages;
int flags;
const char *description; // a description after usage
const char *epilog; // a description at the end
// internal context
int argc;
const char **argv;
const char **out;
int cpidx;
const char *optvalue; // current option value
};
// built-in callbacks
int argparse_help_cb(struct argparse *self,
const struct argparse_option *option);
// built-in option macros
#define OPT_END() { ARGPARSE_OPT_END, 0, NULL, NULL, 0, NULL, 0, 0 }
#define OPT_BOOLEAN(...) { ARGPARSE_OPT_BOOLEAN, __VA_ARGS__ }
#define OPT_BIT(...) { ARGPARSE_OPT_BIT, __VA_ARGS__ }
#define OPT_INTEGER(...) { ARGPARSE_OPT_INTEGER, __VA_ARGS__ }
#define OPT_FLOAT(...) { ARGPARSE_OPT_FLOAT, __VA_ARGS__ }
#define OPT_STRING(...) { ARGPARSE_OPT_STRING, __VA_ARGS__ }
#define OPT_GROUP(h) { ARGPARSE_OPT_GROUP, 0, NULL, NULL, h, NULL, 0, 0 }
#define OPT_HELP() OPT_BOOLEAN('h', "help", NULL, \
"show this help message and exit", \
argparse_help_cb, 0, 0)
int argparse_init(struct argparse *self, struct argparse_option *options,
const char *const *usages, int flags);
void argparse_describe(struct argparse *self, const char *description,
const char *epilog);
int argparse_parse(struct argparse *self, int argc, const char **argv);
void argparse_usage(struct argparse *self);
#ifdef __cplusplus
}
#endif
#endif
...@@ -6,31 +6,75 @@ ...@@ -6,31 +6,75 @@
#include "pgm_io.h" #include "pgm_io.h"
#include "quadtree.h" #include "quadtree.h"
#include "Matrix.h" #include "Matrix.h"
#include "libs/argparse.h"
int main() static const char *const usages[] = {
"Exe [options] [[--] args]",
"Exe [options]",
NULL,
};
int main(int argc, const char **argv)
{ {
char *file = NULL;
char *output = NULL;
int32_t level = 0;
int verbose = false;
struct argparse_option options[] = {
OPT_HELP(),
OPT_STRING('f', "file", &file, "file to use with library", NULL, 0, 0),
OPT_INTEGER('l', "level", &level, "compression level", NULL, 0, 0),
OPT_STRING('o', "output", &output, "output name of file after program use", NULL, 0, 0),
OPT_BOOLEAN('v', "verbose", &verbose, "more verbose output", NULL, 0, 0),
OPT_END(),
};
struct argparse argparse;
argparse_init(&argparse, options, usages, 0);
argparse_describe(&argparse, "\nProgram for convert pgm (P5) file to quadtree and apply transformation on quadtree", "\nExample :\n\t./Exe -f \"buzz.pgm\" -l 900 -o \"out.pgm\" -v");
argc = argparse_parse(&argparse, argc, argv);
if (file != NULL)
{
pgm *p = malloc(1 * sizeof(pgm)); pgm *p = malloc(1 * sizeof(pgm));
pgm_read_from_file(p,"buzz.pgm"); pgm_read_from_file(p, file);
uint32_t val = quadtree_get_depth_from_image_size(p->pixels.col); uint32_t val = quadtree_get_depth_from_image_size(p->pixels.col);
if(verbose)
{
printf("Image input\nx : %d\ty : %d,grayscale : %d\n",p->pixels.lin,p->pixels.col,p->max);
}
node *tree = quadtree_tree_create(val); node *tree = quadtree_tree_create(val);
quadtree_matrix2tree(&p->pixels, tree); quadtree_matrix2tree(&p->pixels, tree);
pgm *pp = malloc(1 * sizeof(pgm)); pgm *pp = malloc(1 * sizeof(pgm));
pp->max = p->max; pp->max = p->max;
matrix_alloc(&pp->pixels, p->pixels.lin, p->pixels.col); matrix_alloc(&pp->pixels, p->pixels.lin, p->pixels.col);
quadtree_moyenne(tree); quadtree_moyenne(tree);
quadtree_compress(tree,200); if (verbose)
{
printf("Compressed level : %d\n",level);
}
quadtree_compress(tree, level);
quadtree_tree2matrix(tree, &pp->pixels); quadtree_tree2matrix(tree, &pp->pixels);
pgm_write_from_file(pp,"Output.pgm"); pgm_write_from_file(pp, output);
matrix_destroy(&p->pixels); matrix_destroy(&p->pixels);
quadtree_tree_destroy(tree);
matrix_destroy(&pp->pixels); matrix_destroy(&pp->pixels);
quadtree_tree_destroy(&tree);
free(p); free(p);
free(pp); free(pp);
}
else if (argc == 0) {
argparse_usage(&argparse);
}
return 0; return 0;
} }
\ No newline at end of file
This diff is collapsed.
...@@ -116,10 +116,10 @@ void quadtree_matrix2tree(matrix *mat, node *arbre); ...@@ -116,10 +116,10 @@ void quadtree_matrix2tree(matrix *mat, node *arbre);
/** /**
* @name quadtree_tree_destroy * @name quadtree_tree_destroy
* @brief Destroy all memory alloc from quadtree_create * @brief Destroy all memory alloc from quadtree_create
* @param tree pointer on pointer on node * @param tree pointer on node
* @return void * @return void
*/ */
void quadtree_tree_destroy(node **tree); void quadtree_tree_destroy(node *tree);
/** /**
* @name quadtree_tree2matrix * @name quadtree_tree2matrix
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment