xref: /Universal-ctags/misc/txt2cstr (revision 431a587e6c6caab7b964eb69bae0b65d342d8b69)
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
9use File::Basename;
10
11sub show_help {
12    print<<EOF;
13Usage:
14	$0 --help
15	$0 INPUT.txt > OUTPUT.c
16EOF
17}
18
19sub 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
39sub 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
64main @ARGV;
65
66# txt2cstr ends here
67