Solved

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

Posted on 2011-02-22
10
612 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:madmax61
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 32

Assisted Solution

by:sarabande
sarabande earned 400 total points
Comment Utility
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
Comment Utility
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
Comment Utility
P.S. Thankyou for contributing so far Sara and cup.
0
 
LVL 32

Accepted Solution

by:
sarabande earned 400 total points
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

762 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

12 Experts available now in Live!

Get 1:1 Help Now