Debian Codegraph @

ctags/misc/txt2cstr

raw
1 #!/usr/bin/env perl
2 #
3 # txt2cstr - a tool converting a text file into char[].
4 #
5 # Copyright (C) 2021 Masatake YAMATO
6 # Copyright (C) 2021 Red Hat Inc.
7 #
8
9 use File::Basename;
10
11 sub show_help {
12 print<<EOF;
13 Usage:
14 $0 --help
15 $0 INPUT.txt > OUTPUT.c
16 EOF
17 }
18
19 sub convert {
20 my $input = $_[0];
21 my $name = basename $input;
22
23 $name =~ s/\.[a-z]+$//g;
24
25 open(INPUT, "< " . $input) or die("faild to open " . $input);
26
27 print "const char ctags$name []=\n";
28 while (<INPUT>) {
29 chop;
30 $_ =~ s/\\/\\\\/g;
31 $_ =~ s/\"/\\\"/g;
32 print '"' . "$_" . '\\n"' . "\n";
33 }
34 print ";\n";
35
36 close (INPUT);
37 }
38
39 sub main {
40 my $input;
41
42 for (@_) {
43 if ( ($_ eq '-h') || ($_ eq '--help') ) {
44 show_help;
45 exit 0;
46 } elsif ( /^-.*/ ) {
47 die "unrecongnized option: $_";
48 } else {
49 if ($input) {
50 die "Don't specify more than 1 input file: @_";
51 }
52 $input=$_;
53 }
54 }
55
56 unless ($input) {
57 die "No input file given";
58 }
59
60 convert $input;
61 0;
62 }
63
64 main @ARGV;
65
66 # txt2cstr ends here