Browse Source

Use getopt for arguments

Olivier Marty 9 years ago
parent
commit
8c46bbc736
2 changed files with 45 additions and 22 deletions
  1. 9 15
      README.md
  2. 36 7
      main.c

+ 9 - 15
README.md

@@ -1,47 +1,41 @@
 # SubtitlesPrinter
 
-
-Print subtitles above a X-screen
+Print subtitles above a X-screen, independently of the video player.
 
 ## Requirement
 
-This program uses X11 to print subtitles. If you are using Windows you probably don't use X11, if you are using Linux you probably do.
+This program uses X11 to show subtitles. If you are using Windows you probably don't use X11, if you are using Linux you probably do.
 
-Currently, this program support only .srt subtitles. Please check also there is no empty line at the beginning of the file, or some weird characters before the first "1".
+Currently, this program support only .srt subtitles. Please check also there is no empty line at the beginning of the file, or some unprintable characters before the first "1".
 You can see the specs of this format here : http://matroska.org/technical/specs/subtitles/srt.html
 
 ## Installation
 
 Run in a terminal
 ```bash
-    git clone https://github.com/OlivierMarty/SubtitlesPrinter.git
-    make
+git clone https://github.com/OlivierMarty/SubtitlesPrinter.git
+make
 ```
 
 ## Usage
 
 To show file.srt, run in the installation directory :
 ```bash
-    ./subtitlesPrinter file.srt
+./subtitlesPrinter file.srt
 ```
 
 To quit, press CTRL+C in the terminal.
 
 ### Optional arguments
 
-If you have already seen X seconds of your video, you can use the following to skip the beginning :
-```bash
-    ./subtitlesPrinter file.srt X
-```
-
-By default, the program leave you 5 seconds to start your video, you can change that with the last optional argument, for Y seconds :
+It is possible to skip x seconds, or to change the delay before starting. For details see
 ```bash
-    ./subtitlesPrinter file.srt X Y
+./subtitlesPrinter -h
 ```
 
 ## TODO
 
-* Manage some evenement, like pauses.
+* Manage some evenements, like pause, shift...
 * Support more type of file (with an extern library ?)
 * Use a larger, customizable font
 

+ 36 - 7
main.c

@@ -20,19 +20,48 @@
 #include "time.h"
 #include "printer.h"
 #include <stdlib.h>
+#include <unistd.h>
+
+void displayUsage(char *name)
+{
+  printf("Usage : %s file.srt [-s shift] [-d delay] [-h]\n", name);
+  printf("  -s x : skip the first x seconds\n");
+  printf("  -d x : wait x seconds before starting\n");
+  printf("  -h   : display this help and exit\n");
+}
 
 int main(int argc, char **argv)
 {
-  int i;
-  FILE *f= NULL;
+  int i, shift = 0, delay = 5;
+  FILE *f = NULL;
+  
+  // parse arguments
+  int c;
+  while((c = getopt (argc, argv, "hs:d:")) != -1)
+    switch(c)
+    {
+      case 'h':
+        displayUsage(argv[0]);
+        return 0;
+      case 's':
+        shift = atoi(optarg);
+        break;
+      case 'd':
+        delay = atoi(optarg);
+        break;
+      //case '?':
+      default:
+        return 1;
+    }
   
-  if(argc < 2)
+  if(optind >= argc)
   {
-    fprintf(stderr, "Usage : ./subtitlesPrinter file.srt [shift in seconds [waiting time in seconds]]\n");
+    fprintf(stderr, "Missing filename.\n", argv[0]);
+    displayUsage(argv[0]);
     return 1;
   }
   
-  f = fopen(argv[1], "r");
+  f = fopen(argv[optind], "r");
   if(f == NULL)
   {
     perror("fopen()");
@@ -43,7 +72,7 @@ int main(int argc, char **argv)
   struct printerEnv penv = printerOpenWindow(-1, 240, 0);
   
   // show a counter before start the clock
-  for(i = (argc > 3) ? atoi(argv[3]) : 5; i > 0; i--)
+  for(i = delay; i > 0; i--)
   {
     char t[16];
     sprintf(t, "%d...\n", i);
@@ -53,7 +82,7 @@ int main(int argc, char **argv)
   }
   printf("0 !\n");
   printerClean(penv);
-  timeInitialize((argc > 2) ? -atoi(argv[2]) : 0);
+  timeInitialize(-shift);
   
   struct SubtitleLine sline;
   int id = 0;