[HH] c++ strings?

Jerry Feldman gaf at blu.org
Tue Nov 20 13:09:28 EST 2012


On 11/20/2012 12:41 PM, Greg London wrote:
> Hello,
>
> First of all, if anyone is planning on killing anyone over how
> they use a language construct, could they do me a favor and
> change the subject heading so I don't feel like I have blood
> on my hands? Thanks.
>
> Second, I'm an ASIC engineer and my usual programming language
> is verilog and systemverilog which is used for hardware design
> and simulating that hardware. Or programming an FPGA. stuff like
> that.
>
> If I need to do something that is "software"ish, then I usually
> use perl, mostly because I like the fact that in perl I can
> focus on the application and the implementation is mostly handled
> under the hood in perl's engine. garbage collection is automatic,
> for example.
>
> So, I'm working on a new project, and we're trying to write some
> C++ code that will work in our simulations and will also work
> on a real processor inthe lab. The C++ code will generate stimulus
> for the device under test (one of our ASICs we're designing).
>
> So, I decided to start this by taking a low level verilog block
> and converting it to C++. The block is a "logger" which mostly
> a bunch of subroutines that take in strings, fiddle with them,
> and then pass the result ot another subroutine. At some point,
> you get far enough down and some subroutine will decide whether
> to send the string to STDOUT or send it to a serial port on an
> FPGA which will then go to a terminal in the lab.
>
> So, I need to write a LOT of little subroutines and pretty much
> all they do is fiddle with strings and pass them to another subroutine.
>
> In perl, it might look like this:
>
> sub upper_subroutine{
>   my ($string)=@_;
>   lower_subroutine("prefix".$string."postfix".timestamp());
> }
>
> Could someone give me an example of how to do this in C++ so that
> it looks as close to this perl code as possible?
>
> clearly there will be differences, but for example, the perl code
> didn't have to declare the lengths of the strings and as another example,
> it could concatenate a bunch of different strings together and hand
> it directly to a subroutine without declaring an intermediate string
> variable first.
>
> Since it is going to run in simulation and on a computer in the
> lab, it probably needs to be somewhat... standard?
> i.e. it ought to use commonly available libraries so that it will
> compile in different environments.
>
> I've been googling around, and found a few different examples, but
> I seem to run into problems getting any of them to work.
> Either I'm missing a step or something is missing in the examples.
>
> I'm familiar with object oriented stuff. Mostly from perl,
> doing classes, encapsulation, polymorphism, and other buzz words.
> But I've done some C++ stuff for work stuff. But mostly what I
> do with C++ is write hardware models. And they haven't needed
> much string processing. It's just number crunching to calculate
> what the hardware will do so it has something to compare against.
>
> But the stuff I'm working on now has a lot of string stuff in it,
> and the C++ examples for dealing with strings all seem really
> klugey to this guy who is used to perl code.
>
> I don't want or need to worry about memory efficiency.
> It won't have to be the fastest code in the world.
> Ease of coding (fewest extra characters) is actually my first goal.
> This means I don't want to declare the size of character arrays.
> I just want it to be dealt with by the class or library or whatever.
>
> The only other important requirement is portability.
> The same code will be simultaneously running in at least
> two different environments, so I don't want to kluge it
> much to handle differences in each environment.
> So something fairly standard would be GREAT.
>
> But, I'm just a dumb hardware engineer and if someone could give me a
> concrete example of the above perl code converted into C++,
> I would be most appreciative.
>
> If this could happen without anyone having to die, that would
> be even better.
>
> ;)
>
Being a former Army officer in Viet Nam, I do like to kill people
occasionally :-)
What you want to do is very simple in C++.
You don't need to get into heavy object oriented code.
essentially your code would be relatively easy:
std::string foo = "1234";
int fubar = 5;
cout << foo << ":" << fubar << "\n";

This will display "1234:5" to stdout.
If you use cerr, it will go to stderr.

Please note that the ":" is a C language string.

Another reason for using ONLY iostream and the new/delete operators for
you are going to use different compilers, then don't take chances.
You don't need to create classes or even be object oriented. The C++
string class does not need you to preallocate size. It also support
concatenation and many other nice things.




-- 
Jerry Feldman <gaf at blu.org>
Boston Linux and Unix
PGP key id:3BC1EB90 
PGP Key fingerprint: 49E2 C52A FC5A A31F 8D66  C0AF 7CEA 30FC 3BC1 EB90


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 543 bytes
Desc: OpenPGP digital signature
URL: <http://lists.blu.org/pipermail/hardwarehacking/attachments/20121120/1f89d7b5/attachment.sig>


More information about the Hardwarehacking mailing list