File: percent-encode.cc

package info (click to toggle)
libzypp 17.38.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 27,744 kB
  • sloc: cpp: 132,661; xml: 2,587; sh: 518; python: 266; makefile: 27
file content (38 lines) | stat: -rw-r--r-- 1,234 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <zypp-core/base/IOStream.h>
#include <zypp-core/url/UrlUtils.h>

static std::string doEncode( const std::string & str_r )
{ return zypp::url::encode( str_r ); }

static std::string doDecode( const std::string & str_r )
{ return zypp::url::decode( str_r ); }

int main( int argc, const char * argv[] )
{
  bool encode( true );

  --argc,++argv;
  if ( argc )
  {
    if ( *argv == std::string( "-d" ) || *argv == std::string( "--decode" ) )
      encode = false;
    else if ( *argv == std::string( "-h" ) || *argv == std::string( "--help" ) )
    {
      std::cout << "Usage: percent-encode [OPTION]" << std::endl;
      std::cout << "Read lines from stdin and write them percent encoded to stdout." << std::endl;
      std::cout << "" << std::endl;
      std::cout << "Option:" << std::endl;
      std::cout << " -d, --decode  Decode lines read from stdin instead of encoding them." << std::endl;
      std::cout << " -h --help     Print this message." << std::endl;
      return 0;
    }
  }

  std::string (*coder)( const std::string & str_r ) = encode ? doEncode: doDecode;
  for( zypp::iostr::EachLine in( std::cin ); in; in.next() )
  {
    std::cout << coder( *in ) << std::endl;
  }
  return 0;
}