replace string in byte stream

Discussion in 'C++' started by Ian Collins, Mar 25, 2008.

  1. Ian Collins

    Ian Collins Guest

    Carmen Sei wrote:
    > I have a function that read data byte and drmp to a file
    >
    > how can I change the string <givenname xsi:nil='true'/> to
    > <givenname>.</givenname>
    >


    Learn how to use std::string.

    --
    Ian Collins.
     
    Ian Collins, Mar 25, 2008
    #1
    1. Advertising

  2. Ian Collins

    Carmen Sei Guest

    I have a function that read data byte and drmp to a file

    how can I change the string <givenname xsi:nil='true'/> to
    <givenname>.</givenname>

    ======================
    bool dump;
    std::eek:fstream dumpfile;


    int my_block_reader(void *userdata, const char *buf, size_t len)
    {
    if (dump == false) {
    if (strnicmp(buf, "<?xml", 5) == 0) {
    dump = true;
    }
    }
    if (dump == true) {
    char *szBuffer = new char[len + 1];
    memcpy(szBuffer, buf, len);
    szBuffer[len] = 0;
    dumpfile << szBuffer;
    delete[] szBuffer;
    }
    return 0;
    }
     
    Carmen Sei, Mar 25, 2008
    #2
    1. Advertising

  3. Ian Collins

    James Kanze Guest

    On Mar 25, 10:08 am, Ian Collins <> wrote:
    > Carmen Sei wrote:
    > > I have a function that read data byte and drmp to a file


    > > how can I change the string <givenname xsi:nil='true'/> to
    > > <givenname>.</givenname>


    > Learn how to use std::string.


    If I understand correctly, he's dealing with an input stream,
    not strings. Of course, one simple solution would be to read
    the input line by line, using string functions on each line to
    search for the string and replace it. But in such cases, I'll
    generally use an std::deque<char>, something like:

    void
    fillQueue(
    std::deque< char >& queue,
    std::istream& source,
    size_t targetLength )
    {
    assert( queue.size() <= targetLength ) ;
    while ( queue.size() != targetLength
    && source.peek() != EOF ) {
    queue.push_back( source.get() ) ;
    }
    }

    void
    globalSearchAndReplace(
    std::istream& source,
    std::eek:stream& dest,
    std::string const& search,
    std::string const& replace )
    {
    std::deque< char > window ;
    fillQueue( window, source, search.size() ) ;
    while ( window.size() != search.size() ) {
    if ( std::equal( window.begin(), window.end(),
    search.begin() ) ) {
    dest << replace ;
    window.clear() ;
    } else {
    dest << window.front() ;
    window.pop_front() ;
    }
    fillQueue( window, source, search.size() ) ;
    }
    std::copy( window.begin(), window.end(),
    std::eek:stream_iterator< char >( dest ) ) ;
    }

    Note that this will work even if the search string contains a
    newline. (On the other hand, it cannot easily be adapted to use
    regular expressions for the search, where the size of the string
    to be matched may be unbound.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Mar 25, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter
    Replies:
    3
    Views:
    734
    Michael Borgwardt
    Aug 5, 2004
  2. Deep
    Replies:
    6
    Views:
    505
    Nick Keighley
    Feb 28, 2007
  3. dolphin
    Replies:
    6
    Views:
    569
    Thomas Fritsch
    Mar 18, 2007
  4. TBass
    Replies:
    5
    Views:
    493
    Jim Langston
    Jan 4, 2008
  5. Roedy Green

    byte stream vs char stream buffer

    Roedy Green, May 7, 2014, in forum: Java
    Replies:
    20
    Views:
    225
    Silvio
    May 18, 2014
Loading...

Share This Page