stdcpp/tsrc/Stdcpp_test/stlport/auto/stlport_fstream/src/fstream1.cpp
changeset 31 ce057bb09d0b
child 34 5fae379060a7
equal deleted inserted replaced
30:e20de85af2ee 31:ce057bb09d0b
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17  
       
    18 // Regression test for STL seekg
       
    19 
       
    20 #include <cstdio> // I really like formatted output.
       
    21 #include <cstdarg>
       
    22 #include <fstream>
       
    23 #include <vector>
       
    24 #include <iostream>
       
    25 //#include <stdio.h>
       
    26 
       
    27 
       
    28 
       
    29 #ifndef cstd
       
    30 #ifdef _MSC_VER
       
    31 #define cstd
       
    32 #else
       
    33 //#define cstd std
       
    34 #define cstd
       
    35 using namespace std;
       
    36 #endif
       
    37 #endif
       
    38 
       
    39 
       
    40 class Error : public std::exception
       
    41 {
       
    42 public:
       
    43 Error(const char *pszfmt, ...);
       
    44 const char *what() const;
       
    45 private:
       
    46 char mMessage[256];
       
    47 };
       
    48 
       
    49 #define countof(x) (sizeof(x)/sizeof((x)[0]))
       
    50 
       
    51 Error::Error(const char *pszfmt, ...)
       
    52 {
       
    53   using cstd::va_list;
       
    54   
       
    55   cstd::va_list ap;
       
    56 va_start(ap, pszfmt);
       
    57 cstd::vsnprintf(mMessage, countof(mMessage), pszfmt, ap);
       
    58 va_end(ap);
       
    59 }
       
    60 
       
    61 const char *Error::what() const
       
    62 {
       
    63 return mMessage;
       
    64 }
       
    65 
       
    66 
       
    67 // usage: testseek filename
       
    68 #ifdef __SYMBIAN32__
       
    69 int fstream_test(int, char**)
       
    70 #else
       
    71 int main(int argc, char **argv)
       
    72 #endif
       
    73 {
       
    74 try
       
    75 {
       
    76 #ifndef __SYMBIAN32__
       
    77 if (argc != 2) {
       
    78 throw Error ("Wrong number of arguments (Usage: testseek filename)");
       
    79 }
       
    80 #endif
       
    81 std::ifstream ifs;
       
    82 ifs.exceptions(std::ios::badbit);
       
    83 #ifndef __SYMBIAN32__
       
    84 ifs.open(argv[1]);
       
    85 #else
       
    86 ifs.open("c:\\testframework\\tstdcpp\\fstream1.input");
       
    87 #endif
       
    88 // First pass: read contents of file into character vector
       
    89 std::vector<char> charvec;
       
    90 cstd::printf("First pass:\n");
       
    91 for(;;)
       
    92 {
       
    93 char ch;
       
    94 if (!ifs.get(ch)) {
       
    95 break;
       
    96 }
       
    97 cstd::printf("%c", ch);
       
    98 charvec.push_back(ch);
       
    99 }
       
   100 cstd::printf("First pass done\n");
       
   101 
       
   102 // Second pass: get positions into pos vector, checking chars
       
   103 std::vector<std::ios::pos_type> posvec;
       
   104 ifs.clear(); // necessary?
       
   105 ifs.seekg(0);
       
   106 std::vector<char>::const_iterator char_it;
       
   107 cstd::printf("Second pass:\n");
       
   108 for(char_it = charvec.begin(); char_it != charvec.end(); ++char_it)
       
   109 {
       
   110 std::ios::pos_type pos = ifs.tellg();
       
   111 posvec.push_back(pos);
       
   112 char ch;
       
   113 if (!ifs.get(ch)) {
       
   114 break;
       
   115 }
       
   116 cstd::printf("%c", ch);
       
   117 if (ch != *char_it) {
       
   118 #ifdef __SYMBIAN32__
       
   119 Error e1("Character mismatch: got '%c', expected '%c'",ch, *char_it);
       
   120 throw e1;
       
   121 #else 
       
   122 throw Error("Character mismatch: got '%c', expected '%c'",ch, *char_it);
       
   123 #endif
       
   124 }
       
   125 }
       
   126 if (char_it != charvec.end()) {
       
   127 #ifdef __SYMBIAN32__
       
   128 Error e2("Unexpected end of file");
       
   129 throw e2;
       
   130 #else
       
   131 throw Error("Unexpected end of file");
       
   132 #endif
       
   133 }
       
   134 cstd::printf("Second pass done\n");
       
   135 
       
   136 // Third pass: seek to each saved position and read next char
       
   137 ifs.clear(); // necessary, but I don't like it
       
   138 std::vector<std::ios::pos_type>::const_iterator pos_it;
       
   139 cstd::printf("Third pass:\n");
       
   140 for(char_it = charvec.begin(), pos_it = posvec.begin();
       
   141 char_it != charvec.end(); ++char_it, ++pos_it)
       
   142 {
       
   143 ifs.seekg(*pos_it);
       
   144 char ch;
       
   145 if (!ifs.get(ch)) {
       
   146 #ifdef __SYMBIAN32__
       
   147 Error e3("End of file when expecting '%c'", *char_it);
       
   148 throw e3;
       
   149 #else
       
   150 throw Error("End of file when expecting '%c'", *char_it);
       
   151 #endif
       
   152 }
       
   153 cstd::printf("%c", ch);
       
   154 if (ch != *char_it) {
       
   155 Error e4("Character mismatch: got '%c', expected '%c'",ch, *char_it);
       
   156 throw e4;
       
   157 }
       
   158 }
       
   159 cstd::printf("Third pass done\n");
       
   160 cstd::printf("Test passed\n");
       
   161 std::cerr<<"Testing cerr...\n";
       
   162 std::cerr<<"Testing cerr"<<std::endl;
       
   163 std::cerr<<"Testing cerr with char "<<(char)'5'<<std::endl;
       
   164 std::cerr<<"Testing cerr with float "<<(float)5.555<<std::endl;
       
   165 std::cerr<<"Testing cerr with int "<<(int)5<<std::endl;
       
   166 
       
   167 }
       
   168 catch (std::exception &e)
       
   169 {
       
   170 cstd::printf("\nCaught exception: %s\n", e.what());
       
   171 cstd::printf("Test failed!\n");
       
   172 return 1;
       
   173 }
       
   174 return 0;
       
   175 }
       
   176 // end of program