Advertisement
symdrome

main.c

Sep 6th, 2023 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.46 KB | None | 0 0
  1. #include <dirent.h>
  2. #include <stdbool.h>
  3. #include <stddef.h>
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #define MAX_ARRAY 1000
  10.  
  11. typedef struct fileDir {
  12.   char *src;
  13.   char *dst;
  14.   char *op;
  15. } fileDir;
  16.  
  17. int main(int argc, char *argv[]) {
  18.   void help();
  19.   fileDir cli(int argc, char *argv[]);
  20.   void process(fileDir d);
  21.  
  22.   if (argc <= 2) {
  23.     fprintf(stderr, "Please type a name for csv file.\n");
  24.     help();
  25.   }
  26.  
  27.   fileDir dir = cli(argc, argv);
  28.  
  29.   process(dir);
  30.    
  31.   return 0;
  32. }
  33.  
  34. void process(fileDir d) {
  35.   DIR *dir;
  36.   bool isAudioFiles(char const *name);
  37.   struct dirent *entry;
  38.   FILE *file;
  39.   char csvLocation[200] = "";
  40.   strcat(csvLocation, d.dst);
  41.   strcat(csvLocation, "/");
  42.   strcat(csvLocation, d.op);
  43.   file = fopen(csvLocation, "w+");
  44.  
  45.   FILE *fp;
  46.   char time[1024];
  47.  
  48.   char cmd[100] = "soxi -D";
  49.   if ((dir = opendir(d.src)) == NULL)
  50.     fprintf(stderr, "could't open %s\n", d.src), exit(1);
  51.   else {
  52.     fprintf(file, "NOME,DURAÇÃO\n");
  53.     while ((entry = readdir(dir)) != NULL) {
  54.       if (isAudioFiles(entry->d_name) == true) {
  55.     strcat(cmd, " ");
  56.     strcat(cmd, d.src);
  57.     strcat(cmd, "/");
  58.     strcat(cmd, entry->d_name);
  59.    
  60.     fp = popen(cmd, "r");
  61.  
  62.     if (fp == NULL) {
  63.       fprintf(stderr, "soxi command failed\n");
  64.       exit(1);
  65.     }
  66.     if (fgets(time, 60, fp) != NULL)
  67.       fprintf(file, "%s,%s",entry->d_name, time);
  68.       }
  69.     }
  70.     pclose(fp);
  71.     fclose(file);
  72.     closedir(dir);
  73.  
  74.   }
  75.  
  76. }
  77.  
  78. bool isAudioFiles(char const *name) {
  79.   size_t len = strlen(name);
  80.   if (len > 4 &&
  81.       (strcmp(name + len - 4, ".mp3") && strcmp(name + len - 4, ".mp4") &&
  82.        strcmp(name + len - 4, ".wav")) == 0)
  83.     return true;
  84.   else
  85.     return false;
  86. }
  87.  
  88. fileDir cli(int argc, char *argv[]) {
  89.   void help();
  90.   int opt;
  91.   fileDir dir = {".", ".", ""};
  92.  
  93.   while ((opt = getopt(argc, argv, "s:d:n:")) != -1) {
  94.     switch (opt) {
  95.     case 's':
  96.       dir.src = optarg;
  97.       break;
  98.     case 'd':
  99.       dir.dst = optarg;
  100.       break;
  101.     case 'n':
  102.       dir.op = optarg;
  103.       break;
  104.     default:
  105.       printf("Unknown option: %c\n", optopt);
  106.       help();
  107.       exit(1);
  108.     }
  109.   }
  110.  
  111.   return dir;
  112. }
  113.  
  114. void help() {
  115.   fprintf(stderr, "\nUSAGE: audio_length [OPTIOMS] -n <FILENAME.csv>\n"
  116.                   "Options:\n"
  117.                   "       -s <path for source directory>\n"
  118.                   "       -d <path for destination file>\n\n"
  119.   );
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement