Choosing by name

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

31
pkmn.c
View File

@ -1,4 +1,4 @@
#define SHINEY_PROB 8192 #define SHINEY_PROB 32
#include <stdlib.h> #include <stdlib.h>
#include <argp.h> #include <argp.h>
@ -65,7 +65,7 @@ parse_opt (int key, char *arg, struct argp_state *state)
break; break;
case ARGP_KEY_END: case ARGP_KEY_END:
if (state->arg_num < 0) if (state->arg_num < 1 && !arguments->random)
/* Not enough arguments. */ /* Not enough arguments. */
argp_usage (state); argp_usage (state);
break; 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 // We want to write the pokemon name in pokemon_name
// Using regex: s/^[S_]*[0-9]*_//g // Using regex: s/^[S_]*[0-9]*_//g
// char *pokemon_file = "843_silicobra.cow"; // 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; regex_t regex;
regmatch_t matches[100]; regmatch_t matches[100];
char match_buffer[BUFFER_SIZE]; char match_buffer[BUFFER_SIZE];
@ -127,6 +127,23 @@ int pkmn_name_from_file(char *pokemon_file, char *pokemon_name) {
return 0; 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) { int main (int argc, char **argv) {
struct arguments arguments; struct arguments arguments;
char pkmn_path[] = "~/pokemons/"; char pkmn_path[] = "~/pokemons/";
@ -161,9 +178,10 @@ int main (int argc, char **argv) {
printf("Random pokemon: %s\n", pokemon_file); printf("Random pokemon: %s\n", pokemon_file);
} }
} else { } else {
// TODO if (get_pokemon_file(arguments.args[0], pokemon_file) == 1) {
exit(EXIT_FAILURE); fprintf(stderr, "No matching pokemon found\n");
sprintf(pokemon_file, "%s%s", pkmn_path, arguments.args[0]); exit(EXIT_FAILURE);
}
} }
if (arguments.shiney) { if (arguments.shiney) {
@ -179,6 +197,7 @@ int main (int argc, char **argv) {
// Extract the pokemon name from the file // Extract the pokemon name from the file
char pokemon_name[512]; char pokemon_name[512];
pkmn_name_from_file(pokemon_file, pokemon_name); pkmn_name_from_file(pokemon_file, pokemon_name);
pokemon_name[0] = toupper(pokemon_name[0]);
if (arguments.verbose) { if (arguments.verbose) {
printf("Pokemon file: %s\n", pokemon_file); printf("Pokemon file: %s\n", pokemon_file);
printf("Pokemon name: %s\n", pokemon_name); printf("Pokemon name: %s\n", pokemon_name);