Choosing by name

This commit is contained in:
gauvain-thomas
2024-02-18 16:07:19 +01:00
parent 8bbcfd967a
commit 7dcc538166

29
pkmn.c
View File

@ -1,4 +1,4 @@
#define SHINEY_PROB 8192
#define SHINEY_PROB 32
#include <stdlib.h>
#include <argp.h>
@ -65,7 +65,7 @@ parse_opt (int key, char *arg, struct argp_state *state)
break;
case ARGP_KEY_END:
if (state->arg_num < 0)
if (state->arg_num < 1 && !arguments->random)
/* Not enough arguments. */
argp_usage (state);
break;
@ -88,7 +88,7 @@ int pkmn_name_from_file(char *pokemon_file, char *pokemon_name) {
// We want to write the pokemon name in pokemon_name
// Using regex: s/^[S_]*[0-9]*_//g
// char *pokemon_file = "843_silicobra.cow";
char regex_pattern[] = "^[0-9]+_(([a-zA-Z]|-)+)\\.cow$"; // Regex pattern to match the Pokémon name
char regex_pattern[] = "^S?_?[0-9]+_(([a-zA-Z-])+)\\.cow$"; // Regex pattern to match the Pokémon name
regex_t regex;
regmatch_t matches[100];
char match_buffer[BUFFER_SIZE];
@ -127,6 +127,23 @@ int pkmn_name_from_file(char *pokemon_file, char *pokemon_name) {
return 0;
}
int get_pokemon_file(char *arg, char *pokemon_file) {
// Finds a matching file in the pokemons directory from the argument
// Returns 0 if the file was found, 1 otherwise
// ls ~/pokemons/yes | fzf -f wp | head -1
char command[512];
sprintf(command, "ls ~/pokemons/yes | fzf -f %s | head -1 > /tmp/pkmn", arg);
system(command);
FILE *f = fopen("/tmp/pkmn", "r");
fscanf(f, "%s", pokemon_file);
fclose(f);
if (strlen(pokemon_file) == 0) {
return 1;
}
return 0;
}
int main (int argc, char **argv) {
struct arguments arguments;
char pkmn_path[] = "~/pokemons/";
@ -161,9 +178,10 @@ int main (int argc, char **argv) {
printf("Random pokemon: %s\n", pokemon_file);
}
} else {
// TODO
if (get_pokemon_file(arguments.args[0], pokemon_file) == 1) {
fprintf(stderr, "No matching pokemon found\n");
exit(EXIT_FAILURE);
sprintf(pokemon_file, "%s%s", pkmn_path, arguments.args[0]);
}
}
if (arguments.shiney) {
@ -179,6 +197,7 @@ int main (int argc, char **argv) {
// Extract the pokemon name from the file
char pokemon_name[512];
pkmn_name_from_file(pokemon_file, pokemon_name);
pokemon_name[0] = toupper(pokemon_name[0]);
if (arguments.verbose) {
printf("Pokemon file: %s\n", pokemon_file);
printf("Pokemon name: %s\n", pokemon_name);