source: trunk/src/astrometry/util/pad-file.c @ 12099

Revision 12099, 2.1 KB checked in by dstn, 15 months ago (diff)

add pad-file

Line 
1/*
2  This file is part of the Astrometry.net suite.
3  Copyright 2009 Dustin Lang.
4
5  The Astrometry.net suite is free software; you can redistribute
6  it and/or modify it under the terms of the GNU General Public License
7  as published by the Free Software Foundation, version 2.
8
9  The Astrometry.net suite is distributed in the hope that it will be
10  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  General Public License for more details.
13
14  You should have received a copy of the GNU General Public License
15  along with the Astrometry.net suite ; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17*/
18
19#include <stdlib.h>
20#include <stdio.h>
21#include <unistd.h>
22#include <string.h>
23
24#include "ioutils.h"
25#include "an-bool.h"
26#include "errors.h"
27
28const char* OPTIONS = "hv:c:";
29
30void printHelp(char* progname) {
31        fprintf(stderr, "\nUsage: %s <desired-length> <input file>\n"
32                        "    The file will be padded in-place.\n"
33                        "\n"
34                        "    By default, the file is padded with zeros, but:\n"
35                        "      [-v <numerical value of character to pad with>]\n"
36                        "      [-c <character to pad with>]\n"
37                        "\n", progname);
38}
39
40extern char *optarg;
41extern int optind, opterr, optopt;
42
43int main(int argc, char** args) {
44    int argchar;
45        char* progname = args[0];
46        char* infn;
47        int N;
48        char padchar = '\0';
49        int nargs;
50
51    while ((argchar = getopt (argc, args, OPTIONS)) != -1) {
52                switch (argchar) {
53                case 'v':
54                        padchar = atoi(optarg);
55                        printf("Padding with value %i, character \"%c\"\n", (int)padchar, padchar);
56                        break;
57                case 'c':
58                        padchar = optarg[0];
59                        printf("Padding with character \"%c\"\n", padchar);
60                        break;
61                case 'h':
62                default:
63                        printHelp(progname);
64                        exit(-1);
65                }
66        }
67        if (optind < argc) {
68                nargs = argc - optind;
69                args += optind;
70        }
71        if (nargs != 2) {
72                printHelp(progname);
73                exit(-1);
74        }
75        N = atoi(args[0]);
76        infn = args[1];
77
78        printf("Padding file \"%s\" to length %i.\n", infn, N);
79
80        if (pad_file(infn, N, padchar)) {
81                ERROR("Failed to pad file");
82                exit(-1);
83        }
84        return 0;
85}
Note: See TracBrowser for help on using the repository browser.