mixed.cc

Mixing C and C++ operations
//
// $Id: mixed.cc,v 1.6 2003/06/21 13:40:24 cholm Exp $
//
//  simple.cc 
//  Copyright (C) 2003  Christian Holm <cholm@linux.HAL3000> 
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public License
//  as published by the Free Software Foundation; either version 2.1
//  of the License, or (at your option) any later version.
//
//  This library 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
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA
//
/** @file   tests/mixed.cc
    @author Christian Holm
    @date   Tue Jan 28 01:46:50 2003
    @brief   */
#ifdef HAVE_CONFIG_H
# include "config.hh"
#endif
#ifndef FILEMM_streambuf
# include <filemm/streambuf.hh>
#endif
#ifndef FILEMM_util
# include "util.hh"
#endif

/** A program to illustrate mixing of C and C++ operations on
    filemm::streambuf objects. 
    @param argc Number of arguments from runtime system
    @param argv The command line arguments from runtime system
    @return always 0 
    @ingroup examples 
*/
int main(int argc, char** argv) 
{
  // Get the command line arguments 
  std::string file;
  getargs(argc, argv, file);

  // Open the file 
  filemm::streambuf sb(fopen(file.c_str(), "r"));
    
  // Wrap the file in a std::istream object 
  std::istream in(&sb);
    
  // Read the file.  Every second line is read by the C function
  // fgetc, while the other lines are read by the C++ function 
  // std::getline 
  int lineno = 0;
  while (true) {
    // Object to read into 
    std::string line;
    // If this is an even numbered line, let C++ std::getline read one
    // line 
    if (lineno % 2 == 0) {
      std::getline(in, line);
      if (in.eof()) break;
      std::cout << "C++: " << line << std::endl;
    }
    // If it's an odd numbered line, let C fgetc read one line 
    else {
      while (true) {
        char cc[2] = { '\0', '\0' };
        cc[0] = fgetc(sb);
        if (feof((FILE*)sb)) break;
        if (cc[0] == '\n')   break;
        line.append(cc);
      }
      std::cout << "C:   " << line << std::endl;
    }
    // Increment the line number 
    lineno++;
  }
  // Close the file
  fclose(sb);

  return 0;
}

// 
// EOF
//
Top of page
Last update Wed Apr 13 07:51:02 2005
Christian Holm
Created by DoxyGen 1.4.2