Solved

Perl to C++

Posted on 2001-08-16
9
695 Views
Last Modified: 2011-09-20
I have a perl script that i need converting to C++, am not that good a programmer, does anyone know of a programm that comverts or would do it them selves.
0
Comment
Question by:frankle
9 Comments
 
LVL 8

Expert Comment

by:bebonham
Comment Utility
well post the perl script :)

I don't believe there is such a tool...

however

1. you can compile perl to exe

2. you can compile perl to byte code.

but if you actually want it in c++, I think that would have to be done by hand.
0
 
LVL 86

Accepted Solution

by:
jkr earned 300 total points
Comment Utility
Check http://webmaster.indiana.edu/perl56/pod/perlcompile.html - that's not a converter, but something that comes close:

"Because its original purpose was to be a way to produce C code corresponding to a Perl program, and in turn a native executable, the B module and its associated back ends are known as ``the compiler'', even though they don't really compile anything. Different parts of the compiler are more accurately a ``translator'', or an ``inspector'', but people want Perl to have a ``compiler option'' not an ``inspector gadget''. What can you do?"
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Please post the code.
0
 

Author Comment

by:frankle
Comment Utility
The code is:

#!/usr/bin/perl
# liste skal laves til en t?ller og s? skal "markers" laves med en t?ller og
#med en nline der bliver talt op.
#use strict;

$MCL = 1;
$MCH = 0;
@MA[0] = ("0");
@line[0]=(9999999);
$nline = 0;
$numb = 0;
$var = 0;
$nosem = 0;
$in_pos = 0;

# Slurp whole file into an array of bytes.
# and then copy to the string $fs
open(IN, "mission.sqm")  or die "Can't open mission.sqm : $!";
binmode IN;
$/ = undef;
my @bytes = split //, <IN>;
close IN;
$lenread = ($#bytes +1);
# my bit taken from "unpack.pl" also removed other file stuff. <|:)

while($in_pos < @bytes) {
      #print $bytes[$in_pos];
      $fs = $fs . $bytes[$in_pos++];
}
#print "fs ", $fs,"\n";

$fp = (index $fs,".sqm") +7;

#print "lenread ", $lenread,"\n";
do
{
      MSWITCH: {
            $t = ord(substr ($fs,$fp,1));
            #print "\n nl",$nline;
            if ($numb > 0)
                  {
                        if ($numb == 1)
                              { printf "%f",unpack("f",substr($fs,$fp,4)); $fp=$fp+4;}
                        if ($numb == 2)
                              { printf "%lu",unpack("L",substr($fs,$fp,4)); $fp=$fp+4;}
                        $var--;
                        if ($var <=0)
                        {
                              $var=0;
                              if ($nosem == 0)
                                    { print ";"; }
                              $list=0;$arr=0;$numb=0;$var=0;last MSWITCH;
                        }
                        else
                        {
                              print ",";
                              $nosem =1;
                        }
                        $numb = ord(substr($fs,$fp++,1));
                        if ($numb >2)
                              { print " #nL",$number;$list=0;$nosem=0;$numb=0;$var=0;$arr=0;last MSWITCH; }      
                        if ($numb == 0)
                        {
                                    $list = $var;$nosem=0;$arr=0;$numb=0;$var=0;last MSWITCH;
                        }
                        else
                        {
                                    $arr=0;$list=0;last MSWITCH;
                        }
                  }

            if ($line[$nline] <= 0 and $nline >0 and $list ==0)
            {
                  if ($nosem == 0)
                  {
                        print "\n";
                        for ($i=0;$i < ($nline-1);$i++)
                              { print "\t"; }
                  }
                  $nosem=0;
                  print "};";
                  $nline--;
                  if ($line[$nline] <= 0)
                        { last MSWITCH; }
            }
                  
            if ($t == 1 and $var == 0)
                  {
                        $fp++;
                        if (ord(substr ($fs,$fp,1)) > 2)
                              { print " #V",$t;$nosem=0;$list=0;$numb=0;$var=0;$arr=0;last MSWITCH }
                        $var = 1;$arr=0;$list=0;$nosem=0;
                        #print "\nset var";
                        $fp++;
                        last MSWITCH;
                  }
                  
            if ($t == 2 and $arr == 0 and $MCL != 2)
                  {
                        $fp++;
                        $arr = -1;
                        #print "\nset arr",$MCL;
                        last MSWITCH;
                  }
                  
            if (($t == $MCL and $MCH == 0)
                  or ($t == $MCL and $MCH > 0 and $MCH == ord(substr($fs,$fp+1,1))))
                  {      
                        $fp++;
                        if ($MCH > 0)
                              { $fp++; }
                        #print "\n in learn ",$list;
                        if ($list > 0)
                        {
                              $ep = index ( $fs,"\0",$fp);
                              if ($ep <= $fp)
                                    { print " #lL",$t;$list=0;$nosem=0;$numb=0;$var=0;$arr=0;last MSWITCH; }      
                              $MA[($MCL + ($MCH*256))] = substr ($fs,$fp,($ep - $fp));
                              $MA[($MCL + ($MCH*256))] =~s/"/""/g;
                              print "\"",$MA[($MCL + ($MCH*256))],"\"";
                              $fp=$ep+1;
                              if (++$MCL == 128)
                                    { $MCH++; }
                              if ($MCL == 256)
                              {
                                    $MCL = 128;
                                    $MCH++;
                              }                  
                              $list--;
                              if ($list <=0)
                              {
                                    $list=0;
                                    if ($nosem == 0)
                                          { print ";"; }
                                    $nosem = 0;
                              }
                              else
                              {
                                    print ",";
                                    $nosem =1;
                              }
                              $numb=0;$var=0;$arr=0;last MSWITCH;
                        }
                        
                        if ($arr != 0)
                        {
                              #print "\nl arr";
                              $ep = index ( $fs,"\0",$fp);
                              if ($ep <= $fp)
                                    { print " #aL",$t;$list=0;$nosem=0;$numb=0;$var=0;$arr=0;last MSWITCH; }      
                              $MA[($MCL + ($MCH*256))] = substr ($fs,$fp,($ep - $fp));
                              print "\n";
                              $line[$nline]--;
                              for ($i=0;$i < $nline;$i++)
                                    { print "\t"; }
                              print $MA[($MCL + ($MCH*256))],"[]={";
                              if (++$MCL == 128)
                                    { $MCH++; }
                              if ($MCL == 256)
                              {
                                    $MCL = 128;
                                    $MCH++;
                              }
                              $fp=$ep+1;
                              # skal erstates!
                              $th = ord(substr($fs,$fp++,1));
                              $line[++$nline]=0;
                              if ($th == 0)
                                    { $numb=0;$var = 0;$nosem=1;$arr=0;$list=0;last MSWITCH;}
                              $numb = ord(substr($fs,$fp++,1));
                              if ($numb >2)
                                    { print " #,L",$t;$list=0;$nosem=0;$numb=0;$var=0;$arr=0;last MSWITCH; }      
                              if ($numb == 0)
                              {
                                          $list = $th;$nosem=1;$arr=0;$numb=0;$var=0;last MSWITCH;
                              }
                              else
                              {
                                          $var = $th;$nosem=1;$arr=0;$list=0;last MSWITCH;
                              }                              
                        }
                        
                        if ($var == 1)
                        {
                              $numb = ord(substr ($fs,$fp-2,1));
                              if ($MCH > 0)
                                    { $numb = ord(substr ($fs,$fp-3,1)); }
                              $ep = index ( $fs,"\0",$fp);
                              if ($ep <= $fp)
                                    { print " #vL",$t;$list=0;$nosem=0;$numb=0;$var=0;$arr=0;last MSWITCH; }      
                              $MA[($MCL + ($MCH*256))] = substr ($fs,$fp,($ep - $fp));
                              print "\n";
                              $line[$nline]--;
                              for ($i=0;$i < $nline;$i++)
                                    { print "\t"; }
                              print $MA[($MCL + ($MCH*256))],"=";
                              $ep++;
                              $list = 0;
                              if ($numb == 0)
                              {
                                    $list = 1;
                              }
                              $fp=$ep;
                        }
                        else
                        {
                              $ep = index ( $fs,"\0",$fp);
                              if ($ep <= $fp)
                                    { print " #cL",$t;$list=0;$numb=0;$nosem=0;$var=0;$arr=0;last MSWITCH; }      
                              $MA[($MCL + ($MCH*256))] = substr ($fs,$fp,($ep - $fp));
                              print "\n";
                              $line[$nline]--;
                              for ($i=0;$i < $nline;$i++)
                                    { print "\t"; }
                              print "class ",$MA[($MCL + ($MCH*256))],"\n";
                              $fp=$ep +2;
                              for ($i=0;$i < $nline;$i++)
                                    { print "\t"; }
                              print "{";
                              $line[++$nline] = ord(substr($fs,$fp++,1));
                              if ($line[$nline] > 127) #ignore next byte "I'm lazy"
                                    { $fp++ };
                              $numb = 0;
                        }
                        if (++$MCL == 128)
                              { $MCH++; }
                        if ($MCL == 256)
                        {
                              $MCL = 128;
                              $MCH++;
                        }                                    
                  $var = 0;
                  last MSWITCH;                                                      
                  }
            if ($t > 0 and $t < $MCL and $t < 128 )      
                  {      
                        $fp++;
                        #print " in ";
                        if ($list > 0)
                        {
                              print "\"",$MA[($t)],"\"";
                              $list--;
                              if ($list <=0)
                              {
                                    $list=0;
                                    if ($nosem == 0)
                                          { print ";"; }
                                    $nosem = 0;
                              }
                              else
                              {
                                    print ",";
                                    $nosem =1;
                                 }                  
                              $numb=0;$var=0;$arr=0;last MSWITCH;
                        }
                        if ($arr != 0)
                        {
                              print "\n";
                              $line[$nline]--;
                              for ($i=0;$i < $nline;$i++)
                                    { print "\t"; }
                              print $MA[($t)],"[]={";
                              # skal erstates!
                              $th = ord(substr($fs,$fp++,1));
                              $line[++$nline]=0;
                              if ($th == 0)
                                    { $numb=0;$var = 0;$nosem=1;$arr=0;$list=0;last MSWITCH;}
                              $numb = ord(substr($fs,$fp++,1));
                              if ($numb >2)
                                    { print " #,L",$t;$list=0;$nosem=0;$numb=0;$var=0;$arr=0;last MSWITCH; }      
                              if ($numb == 0)
                              {
                                          $list = $th;$nosem=1;$arr=0;$numb=0;$var=0;last MSWITCH;
                              }
                              else
                              {
                                          $var = $th;$nosem=1;$arr=0;$list=0;last MSWITCH;
                              }                              
                        }
                        if ($var == 1)
                        {
                              $numb = ord(substr ($fs,$fp-2,1));
                              print "\n";
                              $line[$nline]--;
                              for ($i=0;$i < $nline;$i++)
                                    { print "\t"; }
                              print $MA[($t)],"=";
                              $list = 0;
                              if ($numb == 0)
                              {
                                    $list = 1;
                              }
                        }
                        else
                        {       
                              print "\n";
                              $line[$nline]--;
                              for ($i=0;$i < $nline;$i++)
                                    { print "\t"; }
                              print "class ",$MA[($t)],"\n";
                              $fp++;
                              for ($i=0;$i < $nline;$i++)
                                    { print "\t"; }
                              print "{";
                              $line[++$nline] = ord(substr($fs,$fp++,1));
                              if ($line[$nline] > 127) #ignore next byte "I'm lazy"
                                    { $fp++ };
                              $numb = 0;
                        }
                        $var = 0;
                        last MSWITCH;                                                      
                   }
            if (($t > 127 and $MCH > 0))
                  {      
                        $fp++;
                        $th=ord(substr ($fs,$fp,1));
                        #print "\n th ",$th;
                           if($MCH >= $th)
                        {
                              $fp++;
                              if ($list > 0)
                              {
                                    print "\"",$MA[($t+($th*256))],"\"";
                                    $list--;
                              if ($list <=0)
                              {
                                    $list=0;
                                    if ($nosem == 0)
                                          { print ";"; }
                                    $nosem = 0;
                              }
                              else
                              {
                                    print ",";
                                    $nosem =1;
                              }
                                    $numb=0;$var=0;$arr=0;last MSWITCH;
                              }
                              if ($arr != 0)
                              {
                                    print "\n";
                                    $line[$nline]--;
                                    for ($i=0;$i < $nline;$i++)
                                          { print "\t"; }
                                    print $MA[($t)],"[]={";
                                    # skal erstates!
                                    $th = ord(substr($fs,$fp++,1));
                                    $line[++$nline]=0;
                                    if ($th == 0)
                                          { $numb=0;$var = 0;$nosem=1;$arr=0;$list=0;last MSWITCH;}
                                    $numb = ord(substr($fs,$fp++,1));
                                    if ($numb >2)
                                          { print " #,L",$t;$list=0;$nosem=0;$numb=0;$var=0;$arr=0;last MSWITCH; }      
                                    if ($numb == 0)
                                    {
                                                $list = $th;$nosem=1;$arr=0;$numb=0;$var=0;last MSWITCH;
                                    }
                                    else
                                    {
                                                $var = $th;$nosem=1;$arr=0;$list=0;last MSWITCH;
                                    }                              
                              }
                              if ($var == 1)
                              {
                                    $numb = ord(substr ($fs,$fp-3,1));
                                    print "\n";
                                    $line[$nline]--;
                                    for ($i=0;$i < $nline;$i++)
                                          { print "\t"; }
                                    print $MA[($t+($th*256))],"=";
                                    $list = 0;
                                    if ($numb == 0)
                                    {
                                          $list = 1;       
                                    }
                              }
                              else
                              {
                                    print "\n";
                                    $line[$nline]--;
                                    for ($i=0;$i < $nline;$i++)
                                          { print "\t"; }
                                    print "class ",$MA[($t+($th*256))],"\n";
                                    $fp++;
                                    for ($i=0;$i < $nline;$i++)
                                          { print "\t"; }
                                    print "{";
                                    $line[++$nline] = ord(substr($fs,$fp++,1));
                                    if ($line[$nline] > 127) #ignore next byte "I'm lazy"
                                          { $fp++ };
                                    $numb = 0;
                              }
                        }
                        else
                        {
                              print " #P",$t;$list=0;$nosem=0;
                        }
                        $var = 0;
                        last MSWITCH;                                                      
                   }
            $fp++;
            #print " #",$t;
            if ($t > 0)
            {
                  print " #",$t;
                  $list = 0;
                  $numb = 0;
                  $var = 0;
                  $nosem = 0;
            }
            last MSWITCH;                                                      
      }
} until ($fp > $lenread);                              
print "\n";
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:frankle
Comment Utility
Is it possible to convert to a DLL, it would be much easyier, all i want to do is stick a C++ front end on for windows. There is probably a much easyier way but i like C++.
0
 

Author Comment

by:frankle
Comment Utility
By the way it is not my code. It is German so don't ask about the comments
0
 
LVL 8

Expert Comment

by:bebonham
Comment Utility
well after reading through that pretty quickly (quite a lot of code!!) I can tell you a few things for sure.

--that script is an excellent candidate for conversion....all it does is translate a binary file using arrays -- ! it doesn't even use associate arrays, so it should be ..in one way.. very easy to port to c.

jkr's link seems well worth investigating in my opinion.  Although the code isn't beginner perl, it is very low-level and basic, so it should be pretty easy to translate with that tool.

As far as rewriting it... geez it's awfuly long, and it would take someone very familiar with bit level manipulation, which is not me in perl, and certainly not in c :)

my book tells me not to focus on that stuff ?
0
 
LVL 11

Expert Comment

by:griessh
Comment Utility
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "jkr" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 5

Expert Comment

by:Netminder
Comment Utility
Per recommendation, force-accepted by
Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now