bug-commoncpp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

FTP-class for CommonC++


From: Tommi Mäkitalo
Subject: FTP-class for CommonC++
Date: Wed, 28 Nov 2001 16:00:17 +0100
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:0.9.6) Gecko/20011120

Hi,

I wrote a ftp-class based on CommonC++. I feel that this fits fine into the library itself and would like to publish it as open source. I'm just adding some legal-stuff (GPL).

Please let me know, if you are interested.

I attach you a example how to use it.


Tommi Mäkitalo
Dr. Eckhardt + Partner GmbH
#include "ftp.h"
#include <stdexcept>

using namespace std;

/**
 * Testapplikation für net::ftp-Klasse
 */
class FtpTestApp
{
    net::ftp ftp;

  public:
    FtpTestApp() { }
    int Run(int argc, char* argv[]);

    void testdir();
    void test();
    void teststream();
};

/**
 * Hauptprogramm der Applikation
 */
int FtpTestApp::Run(int argc, char* argv[])
{
  try
  {
    const char* addr = argc > 1 ? argv[1] : "localhost";
    cout << "host=" << addr << endl;

    ftp.open(addr, "ftp", "address@hidden");

    cout << "pwd=" << ftp.pwd() << endl;
    testdir();
    teststream();

    ftp.quit();

    return 0;
  }
  catch(net::ftpresponse r)
  {
    cerr << "ftpresponse; code=" << r.getcode() << endl
        << "  msg: " << r.what() << endl;
    return -1;
  }
  catch(ost::Socket* s)
  {
    cerr << "socket error " << s->getErrorNumber() << '\n'
         << s->getErrorString() << endl;
    return -2;
  }
}

/**
 * testet Put-Funktion
 */
void FtpTestApp::test()
{
  cout << "mkdir /incoming/uhu" << endl;
  ftp.mkdir("/incoming/uhu");
  cout << "OK" << endl;

  cout << "set binary mode" << endl;
  ftp.binary();
  cout << "OK" << endl;

  cout << "change dir to /incoming" << endl;
  ftp.cwd("/incoming");
  cout << "OK" << endl;

  cout << "put uhu.txt to /incoming/uhu/uhu2.txt" << endl;
  ftp.put("uhu.txt", "/incoming/uhu2.txt");
  cout << "OK" << endl;

  cout << "rename /incoming/uhu/uhu2.txt to uhu/uhu.txt" << endl;
  ftp.ren("/incoming/uhu2.txt", "uhu/uhu.txt");
  cout << "OK" << endl;

  cout << "get /incoming/uhu/uhu.txt to uhu2.txt" << endl;
  ftp.get("/incoming/uhu/uhu.txt", "uhu2.txt");
  cout << "OK" << endl;

  cout << "delete /incoming/uhu/uhu.txt" << endl;
  ftp.del("/incoming/uhu/uhu.txt");
  cout << "OK" << endl;

  cout << "rmdir /incoming/uhu" << endl;
  ftp.rmdir("/incoming/uhu");
  cout << "OK" << endl;
}

void FtpTestApp::testdir()
{
  cout << "directory" << endl;
  net::ftp::dir_type d = ftp.dir();
  for(net::ftp::dir_type::iterator i = d.begin(); i != d.end(); ++i)
    cout << (*i).user()  << '\t'
         << (*i).group() << '\t'
         << (*i).name()  << ':' << endl;

  cout << "directory of /incoming" << endl;
  d = ftp.dir("/incoming");
  for(net::ftp::dir_type::iterator i = d.begin(); i != d.end(); ++i)
    cout << (*i).user()  << '\t'
         << (*i).group() << '\t'
         << (*i).name()  << ':' << endl;

  cout << "OK" << endl;
}

void FtpTestApp::teststream()
{
  cout << "write out Data to /incoming/ttt.txt" << endl;
  net::oftpstream o(ftp, "/incoming/ttt.txt", 1, 1000);
  o << "Hallo Welt!" << endl;
  o.close();
  cout << "OK" << endl;

  cout << "get /incoming/ttt.txt" << endl;
  net::iftpstream i(ftp, "/incoming/ttt.txt", 1, 1000);
  int ch;
  while( (ch = i.get()) != EOF )
    cout << (char)ch;
  cout << flush;
  i.close();
  cout << "OK" << endl;
}

int main(int argc, char* argv[])
{
  try
  {
    FtpTestApp app;
    app.Run(argc, argv);
  }
  catch(exception& e)
  {
    cerr << "EXCEPTION CATCHED: " << e.what() << endl;
  }
}


reply via email to

[Prev in Thread] Current Thread [Next in Thread]