www.digitalmars.com         C & C++   DMDScript  

c++.stlsoft - platformstl::memory_mapped_file + stlsoft::basic_string_view example

Chris, et al.

Here's an example project demonstrating the use of PlatformSTL's
memory_mapped_file with STLSoft's string_view.

The bit to note is that, in the code extract shown below, there is not a single
memory allocation (or no call to 
::operator new(),  at least), outside the call to the operating system's mmap()
(or equiv.). That's pretty good, eh? :-)

You'll also need to put in the new integer_to_string.hpp in
"$(STLSOFT_INCLUDE)/inprogress/stlsoft".

Cheers

Matthew

<code>
  /* . */
  if(NULL == inputFileName)
  {
    usage(1, "Must specify file to process", -1, argc, argv);
  }
  else
  {
    // Define the local string type: a string_view on char type
    typedef ::stlsoft::basic_string_view<char>        string_view_t;

    // Declare the mem-map file and string view variables
    ::platformstl::memory_mapped_file mmf(inputFileName);
    string_view_t                     sv(static_cast<char const
*>(mmf.memory()), mmf.size());

    // Now we'll do things with it

    // 0. Define the delimiter for the ambient operating system
#if defined(PLATFORMSTL_OS_IS_UNIX)
    const string_view_t         delimiter("\n", 1);
#elif defined(PLATFORMSTL_OS_IS_WIN32)
    const string_view_t         delimiter("\r\n", 2);
#else

#endif /* platform */

    // 1. Count the number of chars
    {
      size_t  numChars  = std::distance(sv.begin(), sv.end());

      cout << "Number of characters:            " << numChars << endl;
    }

    // 2. Count the number of lines (using stlsoft::string_tokeniser)
    {
      typedef ::stlsoft::string_tokeniser<  string_view_t
                        , string_view_t
                        , ::stlsoft::string_tokeniser_ignore_blanks<false>
                        >           tokeniser_keep_blanks_t;

      tokeniser_keep_blanks_t lines(sv.begin(), sv.end(), delimiter);
      size_t                  numLines  = ::std::distance(lines.begin(),
lines.end());

      cout << "Number of lines:                 " << numLines << endl;
    }

    // 3. Count the number of non-blank lines (using stlsoft::string_tokeniser)
    {
      typedef ::stlsoft::string_tokeniser<  string_view_t
                        , string_view_t
                        >           tokeniser_t;

      tokeniser_t lines(sv.begin(), sv.end(), delimiter);
      size_t      numLines  = ::std::distance(lines.begin(), lines.end());

      cout << "Number of lines (non-blank):     " << numLines << endl;
    }

    // 4. Count the number of lines (using stlsoft::string_tokeniser + rangelib
stuff)
    {
      typedef ::stlsoft::string_tokeniser<  string_view_t
                        , string_view_t
                        , ::stlsoft::string_tokeniser_ignore_blanks<false>
                        >           tokeniser_keep_blanks_t;

      size_t  numLines =
::rangelib::r_distance(rangelib::make_sequence_range(tokeniser_keep_
lanks_t(sv.begin(), 
sv.end(), delimiter)));

      cout << "Number of lines (R):             " << numLines << endl;
    }

    // 5. Count the number of lines (using stlsoft::string_tokeniser + rangelib
stuff)
    {
      typedef ::stlsoft::string_tokeniser<  string_view_t
                        , string_view_t
                        >           tokeniser_t;

      size_t  numLines =
::rangelib::r_distance(rangelib::make_sequence_range(tokeniser_t(sv.begin(),
sv.end(), 
delimiter)));

      cout << "Number of lines (non-blank) (R): " << numLines << endl;
    }

    // 6. Display each line, indexed (using stlsoft::string_tokeniser +
rangelib stuff)
    {
      typedef ::stlsoft::string_tokeniser<  string_view_t
                        , string_view_t
                        , ::stlsoft::string_tokeniser_ignore_blanks<false>
                        >             tokeniser_t;
      typedef ::rangelib::sequence_range<   tokeniser_t>     
tokenised_sequence_t;
      typedef ::rangelib::indexed_range<    tokenised_sequence_t
                        , size_t
                        >             indexed_tokenised_sequence_t;

      indexed_tokenised_sequence_t 
ir(tokenised_sequence_t(tokeniser_t(sv.begin(), sv.end(), delimiter)));

      cout << "Lines:" << endl;
      for(; ir; ++ir)
      {
        cout << ir.index() << ": " << *ir << endl;
      }
    }
  }
</code>


begin 666 integer_to_string.hpp
H(VEN8VQU9&4 /'-T;'-O9G1?:6YT96=E<E]T;U]S=')I;F<N:#X-" ``
`
end

begin 666 string_view_test.zip


M-+LT2W<!TJ3(E]L.32'0$FUSD42-I.RX6_[W>X^49$F6Y:3)=CC<"'2+J<?W
M]?,>GRCN;).=YQPO7Y!M\HZ';$"RH;3D\<2;<3;W-%/:]9,$J0SEAU0F0F7$
M)U$2LHC%FFHN8C(&+F0L)-%3ML*%)%+\S'SM%JR.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2Y;2D*8;^HO?WF6&6&F4X&J_U)3)P
MN:A%BR0Q]*PFCJ-P$<[DL!))ZI.4>VF_[- $L%*B"F%%86/ K6773WO_^O79
MD:6 S+1'D5,;4G-BR0%_GZP;&R(TV:S&%"07(HD9JHO*L7Z6E6_^JEH%3 <S
M46DG-MH)[!W[!?K_9)D3SO]R_I?SOX3SOQP<'!P<'!P<'!P<'!RW+O+QV%I(
M=N6?SYM'GIQ0XK_+OWX]6? `*:(/IB'22U[TT/=[B$2/M^/O_D,X?L ZKL'?
MSPL)*6C<91X?)CWD(LHK4.XA+G(0]G^-8Q'E])>%(OA;?= ^SB-'">4IEKDL

M;: KJZCP-Y;5-595E_E\M;4^7Y6_HK*A\GYR0S^7=*^;D'YT;^XLATM&/V)N





MN):C'[5LSZ_-V/MW36^%L0W?\<.&,[K [(AY9CEBWX35L\RWK6W<Y +;;^UC
M,[+ [%W_OQ"J-P7ZMM[LANOKL/H6\VUK&[L^SJX_8YN^?3MVCD7,/<L1H]\\
MV\%\V]K&S2ZPEH:6MHY[9O0>;("%RS7+X1I%'PK9M;$PXQIYL^'"AM_4NGM&
M-_P3+%S"+(<+QO0S&"9L;6.:X<KXN-",G(R9MQ.9]]5_Z'"]!JM"=AMA:QN?

M2P,$" ``````O&!], ```````````````!$```!S=')I;F=?=FEE=U]T97-T
M+U!+`0(4`!0````(`+Q ?3),P4?G: P``,LR```E``````````$`( "V 0``
M``!S=')I;F=?=FEE=U]T97-T+W-T<FEN9U]V:65W7W1E<W0N8W!P4$L!`A0`
M" ``````V&!], ```````````````!4````````````0`/]!K0P``'-T<FEN
M9U]V:65W7W1E<W0O=F,V+U!+`0(4``H``````*A ?3(````````````````;


M<W1R:6YG7W9I97=?=&5S="]V8S8O<W1R:6YG7W9I97=?=&5S="YD<W!02P$"
M% `4````" !"6'TR;<,DA=H````K` ``*0`````````!`" `MH$S$P``<W1R
M:6YG7W9I97=?=&5S="]V8S8O<W1R:6YG7W9I97=?=&5S="YD<W=02P$"% `4

M7W9I97=?=&5S="]V8S8O<W1R:6YG7W9I97=?=&5S="YO<'102P$"% `*````
M``"\8'TR````````````````$0```````````! `_T'I) ``<W1R:6YG7W9I
97=?=&5S="]02P4&``````<`!P`C` ``&"4`````
` end
Mar 28 2005