Solved

HP C++ conversion to gnu g++: uses stringvar.shrink stringvar.dump

Posted on 2011-02-22
10
617 Views
Last Modified: 2012-05-11
I'm converting C++ code written on an HP system to a Linux system using GNU G++
Sample code (not in entirety) follows:
 
        char LocationName[MAX_NAME_LENGTH];
    
        String Name;
        Name="                     ";

        while (test == FALSE) {
                cout << "Enter the Location name: ";
                cin >> Name;
                Name.shrink(MAX_NAME_LENGTH-1);
                Name.dump(LocationName);

                cout << "Do you wish to accept this location name (y,n) : ";
                cin>>accept;
                if (accept == 'y') test=TRUE;
        }

Open in new window


Where can I find information on shrink and dump (online)?
Are they HP specific?
How might I convert that part of the code to g++, if not part of g++?

(p.s. shrink and dump are very hard to successfully Google for.)

Appreciative of any helpful responses.

Regards,
Max
0
Comment
Question by:madmax61
  • 6
  • 2
  • 2
10 Comments
 
LVL 11

Assisted Solution

by:cup
cup earned 100 total points
ID: 34958649
name is a String not a std::string.  That is either project specific or something from HP.  Check in your code whether you can find the definition of String.
0
 

Author Comment

by:madmax61
ID: 34965399
I found String.h at /opt/aCC/include/SC/String.h

Inside String.h the references to shrink and dump were:

        char* dump(char* s) const {
                memcpy(s,d->str,d->len);
                s[d->len]='\0'; return s;
        }

Open in new window


and:

void shrink(int newlen);

Open in new window


and I also noticed some more problematic code (of the code I'm porting to Linux), a reference to int_to_str() inside this HP header;

friend String int_to_str(int);

Open in new window



So, dump and shrink (and also int_to_str) are not standard C++, that is, they are specific to the HP C++ (aC++ to me more precise) compiler?

In laymans terms what is the dump code above doing?

What is shrink likely to be doing? Only reducing the length of a string? Trimming whitespace? Allowing someone to enter input of 20 characters say, but only assigning the first, say, 15 to the string?

Cheers,
Max

0
 

Author Comment

by:madmax61
ID: 34965441
Is dump some sort of string to char conversion?

p.s. 'm not very experienced with C++ so I'm not all that familiar with the difference between string and char.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:madmax61
ID: 34965659
As it turns out I can do a "man String" on HP and it tells me :

    Length adjustment
      void shrink(int n);
           If this String is longer that n, truncate it to that length.
           Otherwise leave the String unchanged.  Returns non-zero if the
           String is non-empty.  Runs in O(1).


   Conversion to char*
      The following functions provide an interface to C-style functions that

                                    - 6 -      Formatted:  February 24, 2011

 String(3C++)                                                   String(3C++)

      expect pointers to null-terminated character arrays.

      operator const char*()const;
           If length() is N, this operator works by stuffing a null
           character into cell N+1 and returning a pointer to the first
           cell.  Runs in O(length()) when space must be acquired to make
           room for the extra cell (worst case), and O(1) otherwise.

      char* dump(char* p)const;
           Copies the characters of this String into the character array
           pointed to by p, adding a null character at the end.  The array
           is assumed to be at least length()+1 characters in length, but
           this requirement is not checked for.  Runs in O(length()).


and

 NAME
      String - variable-length character strings

 SYNOPSIS OF String.h
      class istream;           // see iostream(3C++)
      class ostream;           // see iostream(3C++)

      //  Auxiliary types

      class Substring;
      class Tmpstring;

      class Stringsize{
      public:
          Stringsize(unsigned n);
          unsigned size()const;
      };

      class String{
      public:

      //  Constructors, destructor

          String();
          String(const char* p);
          String(const char* p,unsigned n);
          String(char c);
          String(const Substring& s);
          String(const Tmpstring& s);
          String(Stringsize n);
          ~String();

      //  Conversion to char*

          operator const char*()const;
          char* dump(char* p)const;

Open in new window



Can I simply substitute any mention of "String" on HP with "string" in G++?
0
 
LVL 11

Expert Comment

by:cup
ID: 34967615
You could try substituting String with std::string.  

Dump is just a string assignment.  Something like

String xxx;
xxx.dump ("fred");

can be replaced with

std::string xxx;
xxx = "fred";

Looks like shrink is just some form of truncation.  std::string takes care of that anyway so just comment out those statements.
0
 
LVL 33

Assisted Solution

by:sarabande
sarabande earned 400 total points
ID: 34970109
dump is no string assignment. it writes the contents of String into char buffer passed as pointer to char (what is unsafe if no size is given).

if the String was widely used in the app i would implement new String class which has a std::string as member and provides existing interface.

Sara
0
 

Author Comment

by:madmax61
ID: 34976599
Current status is I have 4 issues, which are all HP specific fromthe String.h header:

1. the String class (as opposed to std::string)
2. dump (as in stringvar.dump(charvar))
3. shrink (as in stringvar.shrink(num))
4. int_to_str (integer to string conversion)

Current solutions:

1. just use "string"
2. use c_str (http://www.cplusplus.com/reference/string/string/c_str/)
3. use resize (http://www.cplusplus.com/reference/string/string/resize/)
4. use this template to convert int to string(flexible solution which works on floats or doubles as well) (src : http://bytes.com/topic/c/answers/131431-how-do-simple-loop-store-strings )

template <class T>
inline std::string to_string (const T& t)
{
std::stringstream ss;
ss << t;
return ss.str();
}

Open in new window


(Also found this page useful:
http://www.fredosaurus.com/notes-cpp/strings/header-string.html)

I think I can manage to resolve most of my problems now with the above:

I have one remaining problem concerning resolving these "Strings".
Essentially the program takes textual input from the keyboard as a string and converts it to char, and uses these to char variables to pass into other routines, so I don't want to mess with how they are defined too much as my C++ understanding is fairly limited.

However, the char variables (which are full path filenames) are declared as, for example:
char filename[80];

but to use the c_str the filename (I believe) must be declared as:
const char * filename;

So if I go changing the many references to these filename character variables to const char * I'm not sure what effect it will have.



0
 

Author Comment

by:madmax61
ID: 34976761
P.S. Thankyou for contributing so far Sara and cup.
0
 
LVL 33

Accepted Solution

by:
sarabande earned 400 total points
ID: 34982094
c_str and dump is not the same.

you can add global function strdump like

  char * strdump(const std::string & str, char * ps, int siz)
  {
      if (ps == NULL || siz <= 0)
           return NULL;
      if (siz > (int)str.length())
          siz = (int)str.length()+1;

      strncpy(ps, str.c_str(), siz-1);
      ps[siz] = '\0';
      return ps;
  }

for converting int to str you could use stringstream

   std::ostringstream oss;
   oss << i;
   std::string str = oss.str();

Sara
0
 

Author Comment

by:madmax61
ID: 35022376
Thankyou again to both of you, apologies for not accepting as yet, but I've been busy with other parts of the program as well. For example, not only did it use something called String rather than string it also used something called Time instead of time, which puts time related information into String I think.

I've not gone ahead and implemented the above suggested changes.  Your strdump will probably help me avoid other changes I was going to need to make to accommodate c_str.

I'll assign some points now.

Cheers,
Max
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

813 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