cli.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /** ~~~~~~~~~~ COMMAND LINE INTERFACE ~~~~~~~~~~ **/
  2. /** ---------------------------------------------- *
  3. * *
  4. * Este archivo contiene la implementacion de la *
  5. * lectura de argumentos genericos por linea de *
  6. * comandos (los argumentos particulares de cada *
  7. * filtro se leen en su respectivo .c) *
  8. * *
  9. * Autor: orga2 *
  10. * *
  11. * ----------------------------------------------- *
  12. **/
  13. #include <getopt.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include <sys/stat.h>
  19. #include "tp2.h"
  20. void procesar_opciones(int argc, char **argv, configuracion_t *config)
  21. {
  22. // Si se ejecuta sin parametros ni opciones
  23. if (argc == 1) {
  24. imprimir_ayuda (argv[0]);
  25. exit ( EXIT_SUCCESS );
  26. }
  27. char *tipo_filtro;
  28. int tiempo = 0;
  29. int siguiente_opcion;
  30. // opciones por defecto
  31. config->es_video = false;
  32. config->verbose = false;
  33. config->frames = false;
  34. config->nombre = false;
  35. config->cant_iteraciones = 1;
  36. config->archivo_entrada = NULL;
  37. config->archivo_entrada_2 = NULL;
  38. config->carpeta_salida = ".";
  39. config->extra_archivo_salida = "";
  40. // extraemos opciones de la linea de comandos
  41. const char* const op_cortas = "hi:vt:fo:wn";
  42. const struct option op_largas[] = {
  43. { "help", 0, NULL, 'h' },
  44. { "implementacion", 1, NULL, 'i' },
  45. { "verbose", 0, NULL, 'v' },
  46. { "video", 0, NULL, 'w' },
  47. { "tiempo", 1, NULL, 't' },
  48. { "frames", 0, NULL, 'f' },
  49. { "nombre", 0, NULL, 'n' },
  50. { "output", 1, NULL, 'o' },
  51. { NULL, 0, NULL, 0 }
  52. };
  53. while (1) {
  54. siguiente_opcion = getopt_long ( argc, argv, op_cortas, op_largas, NULL);
  55. // No hay mas opciones
  56. if ( siguiente_opcion == -1 )
  57. break;
  58. // Procesar opcion
  59. switch ( siguiente_opcion ) {
  60. case 'h' : /* -h o --help */
  61. imprimir_ayuda (argv[0]);
  62. exit ( EXIT_SUCCESS );
  63. break;
  64. case 'i' : /* -i o --implementacion */
  65. tipo_filtro = optarg;
  66. break;
  67. case 't' : /* -t o --tiempo */
  68. tiempo = 1;
  69. config->cant_iteraciones = atoi ( optarg );
  70. break;
  71. case 'v' : /* -v o --verbose */
  72. config->verbose = true;
  73. break;
  74. case 'f' : /* -f o --frames */
  75. config->frames = true;
  76. break;
  77. case 'n' : /* -n o --nombre */
  78. config->nombre = true;
  79. break;
  80. case 'o' : /* -o o --output */
  81. config->carpeta_salida = optarg;
  82. break;
  83. case 'w' : /* -w o --video */
  84. config->es_video = true;
  85. break;
  86. case '?' : /* opcion no valida */
  87. imprimir_ayuda (argv[0]);
  88. exit ( EXIT_SUCCESS );
  89. default : /* opcion no valida */
  90. abort ( );
  91. }
  92. }
  93. // Verifico nombre del proceso
  94. config->nombre_filtro = argv[optind++];
  95. if (config->nombre_filtro == NULL) {
  96. imprimir_ayuda (argv[0]);
  97. exit ( EXIT_SUCCESS );
  98. }
  99. // Verifico nombre de la implementacion
  100. if (tipo_filtro == NULL ||
  101. (strcmp(tipo_filtro, "c") != 0 &&
  102. strcmp(tipo_filtro, "asm") != 0)) {
  103. imprimir_ayuda (argv[0]);
  104. exit ( EXIT_SUCCESS );
  105. }
  106. if (strcmp(tipo_filtro, "c") == 0) {
  107. config->tipo_filtro = FILTRO_C;
  108. } else {
  109. config->tipo_filtro = FILTRO_ASM;
  110. }
  111. // Verifico nombre de archivo
  112. config->archivo_entrada = argv[optind++];
  113. if (config->archivo_entrada == NULL) {
  114. imprimir_ayuda (argv[0]);
  115. exit ( EXIT_SUCCESS );
  116. }
  117. if (access( config->archivo_entrada, F_OK ) == -1) {
  118. printf("Error al intentar abrir el archivo: %s.\n", config->archivo_entrada);
  119. exit ( EXIT_SUCCESS );
  120. }
  121. filtro_t * filtro = detectar_filtro(config);
  122. if (filtro != NULL && optind < argc && filtro->n_entradas > 1) {
  123. config->archivo_entrada_2 = argv[optind++];
  124. if (config->archivo_entrada_2 == NULL) {
  125. imprimir_ayuda (argv[0]);
  126. exit ( EXIT_SUCCESS );
  127. }
  128. if (access( config->archivo_entrada_2, F_OK ) == -1) {
  129. printf("Error al intentar abrir el archivo: %s.\n", config->archivo_entrada_2);
  130. exit ( EXIT_SUCCESS );
  131. }
  132. }
  133. }
  134. void imprimir_ayuda ( char *nombre_programa) {
  135. printf ( "Uso: %s opciones filtro nombre_archivo_entrada parametros_filtro\n",
  136. nombre_programa );
  137. printf ( " Los filtros que se pueden aplicar son \n");
  138. for (int i = 0; filtros[i].nombre != 0; i++)
  139. filtros[i].ayuda();
  140. printf ( "\n" );
  141. printf ( " -h, --help: \n");
  142. printf ( " Imprime esta ayuda\n" );
  143. printf ( "\n" );
  144. printf ( " -i, --implementacion NOMBRE_MODO\n");
  145. printf ( " "
  146. "Implementación sobre la que se ejecutará el filtro\n"
  147. " "
  148. "seleccionado. Los implementaciones disponibles\n"
  149. " "
  150. "son: c, asm\n");
  151. printf ( "\n" );
  152. printf ( " -t, --tiempo CANT_ITERACIONES\n");
  153. printf ( " "
  154. "Mide el tiempo que tarda en ejecutar el filtro sobre la\n"
  155. " "
  156. "imagen de entrada una cantidad de veces igual a\n"
  157. " "
  158. "CANT_ITERACIONES\n");
  159. printf ( "\n" );
  160. printf ( " -o, --output CARPETA\n");
  161. printf ( " "
  162. "Carpeta de salida. Por defecto es la misma que la de entrada\n");
  163. printf ( " -n, --nombre\n");
  164. printf ( " "
  165. "No aplica el filtro, solo muestra el nombre del archivo de salida\n");
  166. printf ( " -v, --verbose\n");
  167. printf ( " "
  168. "Imprime información adicional\n" );
  169. printf ( "\n" );
  170. }