novo cabeçalho: args-parser
This commit is contained in:
parent
5914591860
commit
598b41f62d
3 changed files with 39 additions and 30 deletions
20
src/args-parser.c
Normal file
20
src/args-parser.c
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
#include <string.h>
|
||||||
|
#include "args-parser.h"
|
||||||
|
|
||||||
|
int parse_arg(const char *arg) {
|
||||||
|
// Argumento iniciado com '-'...
|
||||||
|
if (arg[0] == '-') {
|
||||||
|
if (strcmp(arg, "-a") == 0) {
|
||||||
|
return OPT_APPEND;
|
||||||
|
} else if (strcmp(arg, "-o") == 0) {
|
||||||
|
return OPT_OVERWRITE;
|
||||||
|
} else if (strcmp(arg, "-h") == 0) {
|
||||||
|
return OPT_HELP;
|
||||||
|
} else {
|
||||||
|
// Argumento inválido!
|
||||||
|
return OPT_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Sem traço é nome de arquivo...
|
||||||
|
return OPT_NAME;
|
||||||
|
}
|
17
src/args-parser.h
Normal file
17
src/args-parser.h
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
/*
|
||||||
|
* Strings definidas em messages.c
|
||||||
|
*/
|
||||||
|
#ifndef ARGS_PARSER_H
|
||||||
|
#define ARGS_PARSER_H
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
OPT_ERROR = -1,
|
||||||
|
OPT_NAME,
|
||||||
|
OPT_OVERWRITE,
|
||||||
|
OPT_APPEND,
|
||||||
|
OPT_HELP
|
||||||
|
} Option;
|
||||||
|
|
||||||
|
int parse_arg(const char *arg);
|
||||||
|
|
||||||
|
#endif // ARGS_PARSER_H
|
32
src/hdoc.c
32
src/hdoc.c
|
@ -1,22 +1,12 @@
|
||||||
/*
|
/*
|
||||||
* Compilar com: gcc -Wall hdoc.c messages.c -o hdoc
|
* Compilar com: gcc -Wall hdoc.c messages.c args-parser.c -o hdoc
|
||||||
*/
|
*/
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "messages.h"
|
#include "messages.h"
|
||||||
|
#include "args-parser.h"
|
||||||
typedef enum {
|
|
||||||
OPT_ERROR = -1,
|
|
||||||
OPT_NAME,
|
|
||||||
OPT_OVERWRITE,
|
|
||||||
OPT_APPEND,
|
|
||||||
OPT_HELP
|
|
||||||
} Option;
|
|
||||||
|
|
||||||
int parse_arg(char *arg);
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
char *file = NULL;
|
char *file = NULL;
|
||||||
|
@ -77,21 +67,3 @@ int main(int argc, char **argv) {
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
int parse_arg(char *arg) {
|
|
||||||
// Argumento iniciado com '-'...
|
|
||||||
if (arg[0] == '-') {
|
|
||||||
if (strcmp(arg, "-a") == 0) {
|
|
||||||
return OPT_APPEND;
|
|
||||||
} else if (strcmp(arg, "-o") == 0) {
|
|
||||||
return OPT_OVERWRITE;
|
|
||||||
} else if (strcmp(arg, "-h") == 0) {
|
|
||||||
return OPT_HELP;
|
|
||||||
} else {
|
|
||||||
// Argumento inválido!
|
|
||||||
return OPT_ERROR;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Sem traço é nome de arquivo...
|
|
||||||
return OPT_NAME;
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue