NAME
C::Utility - utilities for generating C programs
DESCRIPTION
This module was released to CPAN as a helper module for C::Template. It contains simple functions which assist in automatic generation of C programs.
FUNCTIONS
convert_to_c_string
my $c_string = convert_to_c_string ($perl_string);
This converts a Perl string into a C string. For example, it converts
my $string =<<EOF;
The quick "brown" fox
jumped over the lazy dog.
EOF
into
"The quick \"brown\" fox\n"
"jumped over the lazy dog.\n"
It also removes backslashes from before the @ symbol, so \@ is transformed to @.
ch_files
Make a .h filename from a .c filename. Back up both C and .h files.
convert_to_c_pc
my $c_string = convert_to_c_pc ($string);
As "convert_to_c" but also with % (the percent character) converted to a double percent, %%. This is for generating strings which may be used as C format strings.
escape_string
my $escaped_string = escape_string ($normal_string);
Escape double quotes (") in a string with a backslash.
c_to_h_name
my $h_file = c_to_h_name ("frog.c");
# $h_file = "frog.h".
Make a .h file name from a .c file name.
valid_c_variable
valid_c_variable ($variable_name);
This returns 1 if $variable_name
is a valid C variable, the undefined value otherwise.
wrapper_name
my $wrapper = wrapper_name ($file_name);
Given a file name, return a suitable C preprocessor wrapper name based on the file name.
print_top_h_wrapper
print_top_h_wrapper ($file_handle, $file_name);
# Prints #ifndef wrapper at top of file.
Print an "include wrapper" for a .h file to $file_handle
. For example,
#ifndef MY_FILE
#define MY_FILE
The name of the wrapper comes from "wrapper_name" applied to $file_name
.
See also "print_bottom_h_wrapper".
print_bottom_h_wrapper
print_bottom_h_wrapper ($file_handle, $file_name);
Print the bottom part of an include wrapper for a .h file to $file_handle
.
The name of the wrapper comes from "wrapper_name" applied to $file_name
.
See also "print_top_h_wrapper".
print_include
print_include ($file_handle, $file_name);
Print an #include statement for a .h file named $file_name
to $file_handle
:
#include "file.h"
hash_to_c_file
hash_to_c_file ($c_file_name, \%hash);
Output a Perl hash as a set of const char * strings. For example,
hash_to_c_file ('my.c', { version => '0.01', author => 'Michael Caine' });
prints
#include "my.h"
const char * version = "0.01";
const char * author = "Michael Caine";
to my.c, and
#ifndef MY_H
#define MY_H
extern const char * version;
extern const char * author;
#endif
to my.h.
The keys of the hash are checked with "valid_c_variable", and the routine dies if they are not valid C variable names.
A third argument, $prefix
, contains an optional prefix to add to all the variable names:
hash_to_c_file ('that.c', {ok => 'yes'}, 'super_');
prints
const char * super_ok = "yes";
line_directive
line_directive ($fh, 42, "file.x")
prints
#line 42 "file.x"
This prints a C preprocessor line directive to the file specified by $fh
.
brute_force_line
brute_force_line ($input_file, $output_file);
Put #line directives on every line of a file. This is a fix used to force line numbers into a file before it is processed by Template.
add_lines
my $text = add_lines ($file);
Replace strings of the form #line in the file specified by $file
with a C-style line directive before it is processed by Template.
remove_quotes
my $unquoted_string = remove_quotes ($string);
This removes the leading and trailing quotes from $string
. It also removes the "joining quotes" in composite C strings. Thus input of the form "composite " "C" " string" is converted into composite C string without the quotes.
SEE ALSO
AUTHOR
Ben Bullock, <bkb@cpan.org>
COPYRIGHT AND LICENSE
This module and its associated files are copyright (C) 2012 Ben Bullock. They may be copied, used, modified and distributed under the same terms as the Perl programming language.