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

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;

                cout << "Do you wish to accept this location name (y,n) : ";
                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.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
madmax61Author Commented:
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 {
                s[d->len]='\0'; return s;

Open in new window


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?


madmax61Author Commented:
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.
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

madmax61Author Commented:
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()).


      String - variable-length character strings

      class istream;           // see iostream(3C++)
      class ostream;           // see iostream(3C++)

      //  Auxiliary types

      class Substring;
      class Tmpstring;

      class Stringsize{
          Stringsize(unsigned n);
          unsigned size()const;

      class String{

      //  Constructors, destructor

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

      //  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++?
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.
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.

madmax61Author Commented:
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:

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.

madmax61Author Commented:
P.S. Thankyou for contributing so far Sara and cup.
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();


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
madmax61Author Commented:
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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.