@rem = '--*-Perl-*--';
@rem = '
@echo off
set NAME=%~n0.bat
if "%CTCHOME%"=="" goto NoCTCHOME
if not exist "%CTCHOME%\%NAME%" goto Nosetup
if not exist "%CTCHOME%\perl\perl.exe" goto NoPerl
"%CTCHOME%\perl\perl.exe" "%CTCHOME%\%NAME%" %*
if errorlevel 1 goto End
exit /b 0
goto End
@rem ';
########################################################################
#
#                  CTC++ System, any2mem (v1.11)
#                  CTC++ bitcov add-on
#
# RCSfile     : $RCSfile: any2mem.pl $
# Version     : $Revision:$ $Date:$
# Last Edited : 3.9.2015
# Author      : $Author: roland $
#
#                  Copyright (c) 2006-2013 Testwell Oy
#                  Copyright (c) 2006-2015 Verifysoft Technology GmbH
##########################################################################
# Additional notice to users:
# You may use this Perl script as part of your authorized use of the
# CTC++ System. You/your organization is supposed to acquire the actual
# Perl system independently (start for example from http://www.perl.org).
#
# We would however be pleased to receive your bug corrections/reports
# and enhancement suggestions and will consider them in future versions of
# this tool.
#
#
##########################################################################

# subroutines used
#
sub usage;
sub version;
sub basename;
sub myname;

$total = 0;
$current = 0;
$version = "v1.11";
$warned = 0;

if (0 && $#ARGV >= 0) {
    if ($ARGV[0] eq "--version") {
	version();
	exit 0;
    }
    usage();
    exit 0;
}

$define='';
foreach(@ARGV) {
    if (/^-D(\w+)=(.*)/) {
	$define.="#ifndef $1\n".
	    "# define $1 $2\n".
	    "#endif\n";
    } elsif (/^-D(.*)/) { 
	$define.="#ifndef $1\n".
	    "# define $1\n".
	    "#endif\n";
    }
}

@ARGV=();

if (-t STDIN) {
    select STDERR;
    usage();
    print "\nQuit with Ctrl-C\n";
    select STDOUT;
}

while (<>) {
#    Wed Mar  8 11:11:08 2006     ./can_db/can_0.c
    if (/^ .*(\d\d\d\d)\s+(.*)/) { #ctcpost -L output
	$_ = <>;
	if (/counters: F: (\d+), J: (\d+), D: (\d+), C: (\d+), T: (\d+)/) {
	    $current = $1 + $2 + $3 + $3 + $4 + $5 + $5;
	    $current++; #GAP
	} else {
	    if (!$warned) {
		print STDERR myname().": Expected string \"counters:\" at line $. of input...\n\tare you really using ctcpost -L output (uppercase L)?\n";
		$warned++;
	    }
	}
	$total+=$current;
    }
}
if (-t STDOUT) {
    my $short=($total>>3)+1;
    my $name=myname();
    print <<EOM;
$name $version

Insert following line somewhere in the global scope of your application:
  unsigned char CTC_array[$short] = {0};

If you want a compilable output file, redirect $name output to a file, e.g.
$name > CTC_array.c

EOM
} else {
    my $include = '';
    $include = '#include "CTC_array.h"' if -f "CTC_array.h";
print <<EOM;
/* Created by $0 $version */
$include
$define

#ifdef CTC_ARRAY_SIZE
# if (CTC_ARRAY_SIZE * 8) > $total
#  define CTC_NUM_COUNTERS (CTC_ARRAY_SIZE * 8)
# endif
#endif

#ifndef CTC_NUM_COUNTERS 
# define CTC_NUM_COUNTERS $total
#endif

#ifndef CTC_COUNTER
typedef unsigned long CTC_COUNTER;
#endif

#ifndef CTC_NO_BITS

/* OVERALL: $total Bits needed */
/* This one needed for using bit counters */

 unsigned char CTC_array[(CTC_NUM_COUNTERS>>3) + 1] = {0};

#else

/* OVERALL: $total CTC_COUNTER elements needed */
/* That one needed for real CTC_COUNTER */
/* These are mostly unsigned long counters */

  CTC_COUNTER CTC_array[CTC_NUM_COUNTERS] = {0};

#endif

#ifdef CTC_NO_BITCOV_CTC_INIT
# ifndef CTC_NO_RUNTIME
#  define  CTC_NO_RUNTIME
# endif
#endif

#pragma CTC SKIP

#ifndef CTC_NO_BITCOV_CTC_INIT 
# include <stdio.h>
# ifndef CTC_H
void ctc_init(void);
void ctc_send_data(void);
void ctc_append_all(void);
# endif
extern void ctc_send_char(char);

void ctc_init(void) 
{
    int i;
    for (i=0;i<(sizeof(CTC_array) / sizeof(CTC_array[0]));++i) {
	CTC_array[i]=0;
    }
}
#endif

#ifdef CTC_RUNTIME

static void ctc_send_string(char *);
static void ctc_send_number(unsigned long);
#define START_SEQ   "<start:"
#define END_SEQ     ">"
#define END_NUMBER  ','

#define NUMBER_MAP  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

#define NUMBER_BASE 62
#define MAX_CHARS   20
static void number_to_string(unsigned long i, char* buffer)
{
    int end   = 0;
    int start = 0;
    char start_c;

    do {
        buffer[end++] = NUMBER_MAP[i % NUMBER_BASE];
        i = i / NUMBER_BASE;
    } while (i && end < (MAX_CHARS - 1));

    buffer[end] = (char) END_NUMBER;
    buffer[end + 1] = '\\0';

    end--;
    while (start < end) {
        start_c = buffer[start];
        buffer[start] = buffer[end];
        buffer[end] = start_c;
        start++;
        end--;
    }
}


static void ctc_send_string(char *p)
{
#if defined(CTC_USE_PUTCH)
    while (*p) {
	putch(*p++);
    }
#elif defined(CTC_USE_PRINTF)
    printf("%s", p);
#else
    while (*p) {
	ctc_send_char(*p++);
    }
#endif
}
static void ctc_send_number(unsigned long i)
{
    char buffer[MAX_CHARS];
    number_to_string(i,buffer);
    ctc_send_string(buffer);

}


void ctc_append_all(void) {
    int i = 0;
    int cnt = CTC_NUM_COUNTERS;
#ifndef CTC_USE_PUTCH
    ctc_send_string(START_SEQ);
#else
    putch('<');
    putch('s');
    putch('t');
    putch('a');
    putch('r');
    putch('t');
    putch(':');
#endif
#ifndef CTC_NO_BITS
    ctc_send_number(1);
    cnt=(cnt>>3)+1;
#else 
    ctc_send_number(sizeof(unsigned long)*8);
#endif    
    ctc_send_number(cnt);
    for (i=0;i<cnt;i++) {
	ctc_send_number(CTC_array[i]);
    }
#ifndef CTC_USE_PUTCH
    ctc_send_string(END_SEQ);
    ctc_send_string("\\n");
#else
    putch('>');    
    putch(10);
#endif
#ifndef CTC_NO_RESET
    ctc_init();
#endif
}

void ctc_send_data(void)
{
    ctc_append_all();
}
#endif

#pragma CTC ENDSKIP

EOM
}

1;
#
# The program execution ends here. The definitions of the subroutines
# follow.
#

##########################################################################
# SUBROUTINES
##########################################################################

##########################################################################
# SUBROUTINE: usage
# PURPOSE   : Prints brief usage informations
##########################################################################
sub usage {
    $name = myname();
    print "$name: reads output of ctcpost -L from stdin, example:\n";
    print "\tctcpost -L MON.sym | $name \n";
    print "Just tool testing options are\n";
    print "\t$name --version\n"; 
    print "\t$name --help\n"; 
}

##########################################################################
# SUBROUTINE: version
# PURPOSE   : Prints brief version informations
##########################################################################
sub version {
    print myname()." $version\n";
}

##########################################################################
# SUBROUTINE: basename
# PURPOSE   : Strip directory from a filename
##########################################################################
sub basename 
{
    my ($filename) = @_;
    my $base = $filename;
    my $end;

    $end = rindex $filename, '\\';
    if ((rindex $filename, '/') > $end) {
       $end = rindex $filename, '/';
    }
    if ($end >= 0) {
       $base = substr $filename, $end + 1;
    }
    return $base;
}

##########################################################################
# SUBROUTINE: myname
# PURPOSE   : Gets tool name out of $0
# RETURNS   : Toolname
##########################################################################
sub myname
{
    my $name = basename($0);
    $name=~s/.bat$//i;
    $name=~s/.pl$//i;
    return $name;
}
__END__
:NoCTCHOME
echo.
echo The environment variable CTCHOME does not exist. Set CTCHOME
echo to point to the CTC++ installation directory! (Use short name
echo for it so that the value does not contain spaces!)
echo.
goto End

:Nosetup
echo.
echo Cannot find '%%CTCHOME%%\%%NAME%%'. Set CTCHOME
echo to point to the CTC++ installation directory! (Use short name
echo for it so that the value does not contain spaces!)
echo.
goto End

:NoPerl
echo.
echo Cannot find the '%%CTCHOME%%\perl\perl.exe' program. Set CTCHOME
echo to point to the CTC++ installation directory! (Use short name
echo for it so that the value does not contain spaces!)
echo.
goto End

:End
exit /b 1

