General discussion

  • Creator
    Topic
  • #2080835

    STL string class and fixed buffer

    Locked

    by dfarrell ·

    Hi,
    I’m writing a class in C++ to access an Oracle database. Oracle provides this thing called VARCHAR which gets translated into this complex structure and a regular char buffer to hold the string I’m interested in the database. The class I’m writing would wrap this VARCHAR thing and provide access to the string like a normal STL style string class. So here is my question: is there a way I can assign a fixed length char buffer to be used as the buffer in a string object?

    Thanks in advance,
    Doug Farrell
    dfarrell@grolier.com

All Comments

  • Author
    Replies
    • #3745816

      STL string class and fixed buffer

      by donq ·

      In reply to STL string class and fixed buffer

      VARCHAR isn’t just an Oracle thing, VarChar is used by most ODBC databases to represent a “Varient Data Type”. It is used by programmers when they reach out (on the net) to grab some information and they don’t know what Data Type will be returned (text, character, date, long, single, money, currency, number, byte, binary, logical, etc.) I am not that familiar with Oracle however if your incoming (Oracle) Data Type is consistent I am sure values could be converted “on the fly” as each new record is iterated. Within Access, SQL Server, and FoxPro VarChar is a memory HOG, but an extremely useful “data bucket” when you don’t know if incoming data is text, video, graphic, sound, or whatever. Your routine should transpose (or convert) incoming VARCHAR to something less greedy (for disk space) based on what you are importing and/or appending.

    • #3743837

      STL string class and fixed buffer

      by wayne m. ·

      In reply to STL string class and fixed buffer

      I think it will be easier just to copy the value into an STL std::string than to try and reuse the buffer. The other approach is to extract the string as a const char * and use the basic C str functions with it.

      If you look at the template for std::basic_string, you can probably reverse engineer it to reuse your buffer, but it will probably be very implementation dependent. I am not sure whether it is worth the effort.

      Unfortunately, the C++ templates do not work well as wrapper classes. I’ve faced the same issue in other applications and have just found it easiest to work with a const char *.

    • #3743790

      STL string class and fixed buffer

      by dfarrell ·

      In reply to STL string class and fixed buffer

      Thank you both for your replies. If you hadn’t guessed I’m not a database/oracle guy, just a programmer trying to get along. I didn’t realize that VARCHAR was so generalized as our application just uses it for strings. I’ve looked at the code generated by the Oracle preprocessor and I have to agree, it’s huge!!

      One of the things I was trying to do with my wrapper class was to automate some of the ‘clean up’ required when accessing VARCHAR after a database query. We have to append a ‘\0’ to the end of every VARCHAR as Oracle doesn’t do this. It’s just plain awkward to see my team doing this everywhere in their code, yuck! Perhaps what I’ll do is add an std::string object to my class and copy the VARCHAR string into it. The string object will be the only publicly accessible member of the class. I will have to think about how to free the memory hogged up by a VARCHAR after I’ve gotten the data into a std:string object. Any thoughts on this?

      I had two concerns with my original question: how to reuse the already allocated memory of the VARCHAR. And preventing the buffer from growing, as would be the case if I just copied it to a std::string.

      Thanks for your input,
      Doug Farrell

    • #3742965

      STL string class and fixed buffer

      by lastorck ·

      In reply to STL string class and fixed buffer

      maybe it ll help u:
      //———————————-
      class mystr : public std::string
      {
      public:
      mystr(const char* s)
      {
      allocator.allocate(strlen(s)+1, s);
      //…?
      }
      ~mystr(){}
      private:
      //hide here all other constructors!!!
      };
      //—————————————-
      maybe im mistaken – i havent try it…
      meybe u ll have to make pure ur own class with neccessary functionality of std::string…
      Regards

    • #3748206

      STL string class and fixed buffer

      by dfarrell ·

      In reply to STL string class and fixed buffer

      This question was closed by the author

Viewing 4 reply threads