"Fossies" - the Fresh Open Source Software Archive

Member "muscle/html/muscle-by-example/examples/dataio/example_4_idioms.cpp" (8 Jun 2019, 1329 Bytes) of package /linux/privat/muscle7.30.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #include "system/SetupSystem.h"  // for CompleteSetupSystem
    2 #include "dataio/FileDataIO.h"
    3 #include "util/ByteBuffer.h"
    4 
    5 using namespace muscle;
    6 
    7 static void PrintExampleDescription()
    8 {
    9    printf("\n");
   10    printf("This program demonstrates some handy idioms/tricks using the DataIO classes\n");
   11    printf("\n");
   12 }
   13 
   14 int main(int argc, char ** argv)
   15 {
   16    CompleteSetupSystem css;
   17 
   18    PrintExampleDescription();
   19 
   20    printf("Read the entire contents of a file into a ByteBuffer, in two lines:\n");
   21    const char * inputFileName = "example_4_idioms.cpp";
   22    FileDataIO inputFdio(fopen(inputFileName, "r"));
   23    ByteBufferRef bbRef = GetByteBufferFromPool(inputFdio);
   24    if (bbRef())
   25    {
   26       printf("Here are the contents of this program's source file, as a hex dump:\n");
   27       bbRef()->PrintToStream();
   28    } 
   29    else 
   30    {
   31       printf("Error, couldn't read input file [%s]\n", inputFileName);
   32       return 10;
   33    }
   34 
   35    printf("\n\n");
   36    printf("Write a ByteBuffer out to a file in two lines:\n");
   37    const char * outputFileName = "example_4_output.txt";
   38    FileDataIO outputFdio(fopen(outputFileName, "w"));
   39    const uint32 numBytesWritten = outputFdio.WriteFully(bbRef()->GetBuffer(), bbRef()->GetNumBytes());
   40    printf("Wrote " UINT32_FORMAT_SPEC " bytes of data to [%s]\n", numBytesWritten, outputFileName);
   41   
   42    return 0;
   43 }