dvb: Move the generic read/write routines to the library
authorMauro Carvalho Chehab <mchehab@redhat.com>
Tue, 24 Jan 2012 17:00:59 +0000 (15:00 -0200)
committerMauro Carvalho Chehab <mchehab@redhat.com>
Tue, 24 Jan 2012 17:00:59 +0000 (15:00 -0200)
Instead of having them only for dvb-format-convert, move them
to the library. This way, they can be used by the other tools.

Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
utils/dvb/dvb-fe-tool.c
utils/dvb/dvb-file.c
utils/dvb/dvb-file.h
utils/dvb/dvb-format-convert.c

index 18c2988d97e9d667e2bb5271bfa2f3339013e6e4..747f3cfb2c545a18537967505ad18da141033ad9 100644 (file)
@@ -47,24 +47,9 @@ static error_t parse_opt(int k, char *arg, struct argp_state *state)
                frontend = atoi(arg);
                break;
        case 'd':
-               for (i = 0; i < ARRAY_SIZE(delivery_system_name); i++)
-                       if (delivery_system_name[i] &&
-                           !strcasecmp(arg, delivery_system_name[i]))
-                               break;
-               if (i < ARRAY_SIZE(delivery_system_name)) {
-                       delsys = i;
-                       break;
-               }
-               /* Not found. Print all possible values */
-               fprintf(stderr, "Delivery system %s is not known. Valid values are:\n",
-                       arg);
-               for (i = 0; i < ARRAY_SIZE(delivery_system_name) - 1; i++) {
-                       fprintf(stderr, "%-15s", delivery_system_name[i]);
-                       if (!((i + 1) % 5))
-                               fprintf(stderr, "\n");
-               }
-               fprintf(stderr, "\n");
-               return ARGP_ERR_UNKNOWN;
+               delsys = parse_delsys(arg);
+               if (delsys < 0)
+                       return ARGP_ERR_UNKNOWN;
                break;
        case 's':
                set_params = arg;
index 1c5cecea3a0fe086dab4768d607ecb9ead8a310b..b7d2d68802685ec76c987e1407c102da465c98ac 100644 (file)
@@ -953,3 +953,107 @@ int store_dvb_channel(struct dvb_file **dvb_file,
 
        return 0;
 }
+
+enum file_formats parse_format(const char *name)
+{
+       if (!strcasecmp(name, "ZAP"))
+               return FILE_ZAP;
+       if (!strcasecmp(name, "CHANNEL"))
+               return FILE_CHANNEL;
+       if (!strcasecmp(name, "DVBV5"))
+               return FILE_DVBV5;
+
+       fprintf(stderr, "File format %s is unknown\n", name);
+       return FILE_UNKNOWN;
+}
+
+int parse_delsys(const char *name)
+{
+       int i;
+
+       /* Check for DVBv5 names */
+       for (i = 0; i < ARRAY_SIZE(delivery_system_name); i++)
+               if (delivery_system_name[i] &&
+                       !strcasecmp(name, delivery_system_name[i]))
+                       break;
+       if (i < ARRAY_SIZE(delivery_system_name))
+               return i;
+
+       /* Also accept DVB-<foo> format */
+       if (!strcasecmp(name, "DVB-T"))
+               return SYS_DVBT;
+       if (!strcasecmp(name, "DVB-C"))
+               return SYS_DVBC_ANNEX_A;
+       if (!strcasecmp(name, "DVB-S"))
+               return SYS_DVBS;
+       if (!strcasecmp(name, "ISDB-T"))
+               return SYS_ISDBT;
+
+       /* Not found. Print all possible values, "by the book" */
+       fprintf(stderr, "Delivery system %s is not known. Valid values are:\n",
+               name);
+       for (i = 0; i < ARRAY_SIZE(delivery_system_name) - 1; i++) {
+               fprintf(stderr, "%-15s", delivery_system_name[i]);
+               if (!((i + 1) % 5))
+                       fprintf(stderr, "\n");
+       }
+
+       fprintf(stderr, "Delivery system unknown\n");
+       return -1;
+}
+
+struct dvb_file *read_file_format(const char *fname,
+                                 uint32_t delsys,
+                                 enum file_formats format)
+{
+       struct dvb_file *dvb_file;
+
+       switch (format) {
+       case FILE_CHANNEL:              /* DVB channel/transponder old format */
+               dvb_file = parse_format_oneline(fname,
+                                               SYS_UNDEFINED,
+                                               &channel_file_format);
+               break;
+       case FILE_ZAP:
+               dvb_file = parse_format_oneline(fname,
+                                               delsys,
+                                               &channel_file_zap_format);
+               break;
+       case FILE_DVBV5:
+               dvb_file = read_dvb_file(fname);
+       default:
+               return NULL;
+       }
+
+       return dvb_file;
+}
+
+int write_file_format(const char *fname,
+                     struct dvb_file *dvb_file,
+                     uint32_t delsys,
+                     enum file_formats format)
+{
+       int ret;
+
+       switch (format) {
+       case FILE_CHANNEL:              /* DVB channel/transponder old format */
+               ret = write_format_oneline(fname,
+                                          dvb_file,
+                                          SYS_UNDEFINED,
+                                          &channel_file_format);
+               break;
+       case FILE_ZAP:
+               ret = write_format_oneline(fname,
+                                          dvb_file,
+                                          delsys,
+                                          &channel_file_zap_format);
+               break;
+       case FILE_DVBV5:
+               ret = write_dvb_file(fname, dvb_file);
+       default:
+               return -1;
+       }
+
+       return ret;
+}
+
index 5f9e6e506a57cc983a65e5da668fbfd8a3f38172..9fa5e549c5f1cc6c45abb2d928d8398f5b3f4b73 100644 (file)
@@ -65,6 +65,14 @@ struct parse_file {
        struct parse_struct formats[];
 };
 
+/* Known file formats */
+enum file_formats {
+       FILE_UNKNOWN,
+       FILE_ZAP,
+       FILE_CHANNEL,
+       FILE_DVBV5,
+};
+
 #define PTABLE(a) .table = a, .size=ARRAY_SIZE(a)
 
 /* FAKE DTV codes, for internal usage */
@@ -133,3 +141,12 @@ int store_dvb_channel(struct dvb_file **dvb_file,
                      struct dvb_v5_fe_parms *parms,
                      struct dvb_descriptors *dvb_desc,
                      int get_detected, int get_nit);
+int parse_delsys(const char *name);
+enum file_formats parse_format(const char *name);
+struct dvb_file *read_file_format(const char *fname,
+                                          uint32_t delsys,
+                                          enum file_formats format);
+int write_file_format(const char *fname,
+                     struct dvb_file *dvb_file,
+                     uint32_t delsys,
+                     enum file_formats format);
index 14c6cb735bf968798e0453c432cddd253985226b..b20a178bec57ea199f41af994bc322f309743bf0 100644 (file)
 
 #define PROGRAM_NAME   "dvb-format-convert"
 
-enum file_formats {
-       FILE_UNKNOWN,
-       FILE_ZAP,
-       FILE_CHANNEL,
-       FILE_DVBV5,
-};
-
 struct arguments {
        char *input_file, *output_file;
        enum file_formats input_format, output_format;
@@ -60,34 +53,6 @@ static const struct argp_option options[] = {
 const char *argp_program_version = PROGRAM_NAME " version " V4L_UTILS_VERSION;
 const char *argp_program_bug_address = "Mauro Carvalho Chehab <mchehab@redhat.com>";
 
-enum file_formats parse_format(const char *name)
-{
-       if (!strcasecmp(name, "ZAP"))
-               return FILE_ZAP;
-       if (!strcasecmp(name, "CHANNEL"))
-               return FILE_CHANNEL;
-       if (!strcasecmp(name, "DVBV5"))
-               return FILE_DVBV5;
-
-       fprintf(stderr, "File format %s is unknown\n", name);
-       return FILE_UNKNOWN;
-}
-
-int parse_delsys(const char *name)
-{
-       if (!strcasecmp(name, "DVB-T"))
-               return SYS_DVBT;
-       if (!strcasecmp(name, "DVB-C"))
-               return SYS_DVBC_ANNEX_A;
-       if (!strcasecmp(name, "DVB-S"))
-               return SYS_DVBS;
-       if (!strcasecmp(name, "ATSC"))
-               return SYS_ATSC;
-
-       fprintf(stderr, "Delivery system unknown\n");
-       return -1;
-}
-
 static error_t parse_opt(int k, char *optarg, struct argp_state *state)
 {
        struct arguments *args = state->input;
@@ -113,44 +78,13 @@ static int convert_file(struct arguments *args)
        int ret;
 
        printf("Reading file %s\n", args->input_file);
-       switch (args->input_format) {
-       case FILE_CHANNEL:              /* DVB channel/transponder old format */
-               dvb_file = parse_format_oneline(args->input_file,
-                                               SYS_UNDEFINED,
-                                               &channel_file_format);
-               break;
-       case FILE_ZAP:
-               dvb_file = parse_format_oneline(args->input_file,
-                                               (uint32_t)args->delsys,
-                                               &channel_file_zap_format);
-               break;
-       case FILE_DVBV5:
-               dvb_file = read_dvb_file(args->input_file);
-       default:
-               return -1;
-       }
-       if (!dvb_file)
-               return -2;
+
+       dvb_file = read_file_format(args->input_file, args->delsys,
+                                   args->input_format);
 
        printf("Writing file %s\n", args->output_file);
-       switch (args->output_format) {
-       case FILE_CHANNEL:              /* DVB channel/transponder old format */
-               ret = write_format_oneline(args->output_file,
-                                          dvb_file,
-                                          SYS_UNDEFINED,
-                                          &channel_file_format);
-               break;
-       case FILE_ZAP:
-               ret = write_format_oneline(args->output_file,
-                                          dvb_file,
-                                          (uint32_t)args->delsys,
-                                          &channel_file_zap_format);
-               break;
-       case FILE_DVBV5:
-               ret = write_dvb_file(args->output_file, dvb_file);
-       default:
-               return -1;
-       }
+       ret = write_file_format(args->output_file, dvb_file,
+                               args->delsys, args->output_format);
 
        return ret;
 }