1#!/usr/bin/perl
2
3#
4# Copyright 2019, Cypress Semiconductor Corporation or a subsidiary of
5# Cypress Semiconductor Corporation. All Rights Reserved.
6#
7# This software, including source code, documentation and related
8# materials ("Software"), is owned by Cypress Semiconductor Corporation
9# or one of its subsidiaries ("Cypress") and is protected by and subject to
10# worldwide patent protection (United States and foreign),
11# United States copyright laws and international treaty provisions.
12# Therefore, you may use this Software only as provided in the license
13# agreement accompanying the software package from which you
14# obtained this Software ("EULA").
15# If no EULA applies, Cypress hereby grants you a personal, non-exclusive,
16# non-transferable license to copy, modify, and compile the Software
17# source code solely for use in connection with Cypress's
18# integrated circuit products. Any reproduction, modification, translation,
19# compilation, or representation of this Software except as specified
20# above is prohibited without the express written permission of Cypress.
21#
22# Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO WARRANTY OF ANY KIND,
23# EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED
24# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress
25# reserves the right to make changes to the Software without notice. Cypress
26# does not assume any liability arising out of the application or use of the
27# Software or any product or circuit described in the Software. Cypress does
28# not authorize its products for use in any products where a malfunction or
29# failure of the Cypress product may reasonably be expected to result in
30# significant property damage, injury or death ("High Risk Product"). By
31# including Cypress's product in a High Risk Product, the manufacturer
32# of such system or application assumes all risk of such use and in doing
33# so agrees to indemnify Cypress against all liability.
34#
35
36if (! $ARGV[0] )
37{
38    print "Usage ./text_to_resource__c.pl  <MEM|FILESYS> <variable name> <text file>";
39    exit;
40}
41
42# Print start of output
43my $location = shift @ARGV;
44my $variable_name = shift @ARGV;
45my $original_variable_name = $variable_name;
46my $file = shift @ARGV;
47
48#open the file
49open INFILE, "<:raw", $file or die "cant open " . $file;
50@file_cont_array = <INFILE>;
51close INFILE;
52$file_cont = join('',@file_cont_array);
53
54
55print "#include \"cy_abs_resource.h\"\n";
56print "\n";
57
58my $pos = 0;
59
60if ( ( $file =~ m/\.html$/sgi ) ||
61     ( $file =~ m/\.txt$/sgi ) )
62{
63  while ( $file_cont =~ s/^(.*?)\r?\n?\<WICED\:section\s+suffix=\"(\S+)\"\s*\/\>\r?\n?(.*)$/$3/sgi )
64  {
65    my $substr = $1;
66    my $variable_suffix = $2;
67    my $nextpos = $-[3];
68
69    print "\n";
70    if ( $location ne "MEM" )
71    {
72        $name = $file;
73        $name=~s/^.+\/(.*?)/$1/;
74        print "const cy_resource_handle_t ${variable_name} = { CY_RESOURCE_IN_FILESYSTEM, " . (length( $substr )) . ", { .fs = { $pos, \"$name\" }}};\n";
75        print "\n";
76    }
77    else
78    {
79        print "const uint8_t ${variable_name}_data[" . (length( $substr )+1) . "] = ";
80        my $section_length = length( $substr );
81        while ( $substr =~ s/^(.*?\n)(.*)$/$2/sgi )
82        {
83            print "\"" . escape_string( $1 ) . "\" \\\n";
84        }
85        print "\"" . escape_string( $substr ) . "\";\n";
86
87        print "const cy_resource_handle_t ${variable_name} = { CY_RESOURCE_IN_MEMORY, $section_length, { .mem_data = ${variable_name}_data}};";
88        print "\n";
89    }
90
91    $variable_name = $original_variable_name . $variable_suffix;
92    $pos += $nextpos;
93  }
94}
95
96if ( $location ne "MEM" )
97{
98    print "\n";
99    $name = $file;
100    $name=~s/^.+\/(.*?)/$1/;
101    print "const cy_resource_handle_t ${variable_name} = { CY_RESOURCE_IN_FILESYSTEM, " . (length( $file_cont )) . ", { .fs = { $pos, \"$name\" }}};\n";
102    print "\n";
103}
104else
105{
106    print "const uint8_t ${variable_name}_data[" . (length( $file_cont ) + 1) . "] = ";
107    my $section_length = length( $file_cont );
108    while ( $file_cont =~ s/^(.*?\n)(.*)$/$2/sgi )
109    {
110        print "\"" . escape_string( $1 ) . "\" \\\n";
111    }
112    print "\"" . escape_string( $file_cont ) . "\";\n";
113    print "const cy_resource_handle_t ${variable_name} = { CY_RESOURCE_IN_MEMORY, $section_length, { .mem_data = ${variable_name}_data }};";
114    print "\n";
115}
116
117sub escape_string( $escstring )
118{
119  my $escstring = shift;
120  # Escape characters for C string
121  $escstring =~ s/\\/\\\\/sgi; # backslash
122  $escstring =~ s/\a/\\a/sgi;  # bell
123  $escstring =~ s/\x8/\\b/sgi; # backspace
124  $escstring =~ s/\f/\\f/sgi;  # formfeed
125  $escstring =~ s/\n/\\n/sgi;  # linefeed
126  $escstring =~ s/\r/\\r/sgi;  # carriage return
127  $escstring =~ s/\t/\\t/sgi;  # tab
128  $escstring =~ s/\xB/\\v/sgi; # vertical tab
129  $escstring =~ s/\'/\\'/sgi;  # single quote
130  $escstring =~ s/\"/\\"/sgi;  # double quote
131  return $escstring;
132}
133