File: test-str-cvt.cc

package info (click to toggle)
animals 201007161925-8
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 200 kB
  • sloc: cpp: 1,809; makefile: 69
file content (136 lines) | stat: -rw-r--r-- 3,573 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright (c) 1997 by Jim Lynch.
// Copyright (c) 2010 by Philipp Schafft.
// This software comes with NO WARRANTY WHATSOEVER.
//
// This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; version 2 dated June, 1991, or, at your
//    option, any LATER version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program;  if not, write to the Free Software
//    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
//    02110-1301 USA
//
// On Debian Linux systems, the complete text of the GNU General
// Public License can be found in `/usr/doc/copyright/GPL' (on some
// installations) or /usr/share/common-licenses/GPL (on newer 
// ones).

#include <iostream>
#include <sstream>
#include <string>

#include "str-conversions.h"

void readline(std::istream &in, std::string &it)
{
  getline(in, it);
}

int IsSpace(const char theChar)
{
  int result = 0;
  
  switch(theChar)
    {
    case ' ':
    case '\n':
    case '\t':
    case '\r':
    case '\v':
    case '\f':
      result = 1;
      break;
    }
  
  return result;
}

void ChopTrailingWhiteSpace(std::string &it)
{
  int n = it.size() - 1;

  while(IsSpace( it[n] ))
    n--;

  it = it.substr(0, n + 1);
}

void ChopQuestionMark(std::string &it)
{
  ChopTrailingWhiteSpace(it);

  if(*(it.end() - 1) == '?')
    {
      it = it.substr(0, it.size() - 1);
      ChopTrailingWhiteSpace(it);
    }
}

std::string progName;
std::string line;

// void ChopQuestionMark(std::string &it)
// void ChopTrailingWhiteSpace(std::string &it)
// int IsSpace(const char theChar)
// void readline(std::istream &in, std::string &it)

int main(int argc, char *argv[])
{
  progName = argv[0];

  std::cout << "I'm reading a line" << std::endl;
  readline(std::cin, line);

  std::cout << "the line I just read was" << std::endl;
  std::cout << '|' << line << '|' << std::endl;

  ChopTrailingWhiteSpace(line);

  std::cout << "the line without trailing spaces" << std::endl;
  std::cout << '|' << line << '|' << std::endl;

  ChopQuestionMark(line);

  std::cout << "the line without the last trailing '?'" << std::endl;
  std::cout << '|' << line << '|' << std::endl;

  std::cout << "===== end of my own functions =====" << std::endl;

  std::cout << "Enter a long:" << std::endl;

  std::string theLongStr;
  long theLong;

  readline(std::cin, theLongStr);
  // will a string be converted automatically to a const char*? No.
  const char *theLongCStr = theLongStr.c_str();
  theLong = CStringToLong(theLongCStr);
  const char *theLongCStr2 = LongToNewCString(theLong);

  std::cout << "the long was " << theLongCStr2 << std::endl;

  char *s1 = MakeCStringCopy(theLongStr);
  char *s2 = MakeCStringCopy(theLongCStr);

  std::ostringstream ss3(theLongStr);

  char *s3 = MakeCStringCopy(ss3);

  std::cout << "s1 was |" << s1 << "|" << std::endl;
  std::cout << "s2 was |" << s2 << "|" << std::endl;
  std::cout << "s3 was |" << s3 << "|" << std::endl;

  delete theLongCStr2; // note!! placement of this seems critical...
                       // it can't be right after its last use for some
                       // reason

  return 0;
}
//  std::cout << "" << std::endl;