Logo
~Apps~
~Projects~
~Contact~


inject.cpp

Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 2005  Anders Hedstrom
00008 
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 */
00023 #ifdef _WIN32
00024 #pragma warning(disable:4786)
00025 #endif
00026 #include <SocketHandler.h>
00027 #include <StdoutLog.h>
00028 #include "BString.h"
00029 #include "Exception.h"
00030 #include "MetainfoSocket.h"
00031 
00032 
00033 bool validate(const std::string& );
00034 
00035 int main(int argc,char *argv[])
00036 {
00037         std::string host = "localhost";
00038         port_t port = 16969;
00039         std::list<std::string> files;
00040         for (int i = 1; i < argc; i++)
00041         {
00042                 if (!strcmp(argv[i], "-host") && i < argc - 1)
00043                         host = argv[++i];
00044                 else
00045                 if (!strcmp(argv[i], "-port") && i < argc - 1)
00046                         port = atoi(argv[++i]);
00047                 else
00048                         files.push_back(argv[i]);
00049         }
00050         for (std::list<std::string>::iterator it = files.begin(); it != files.end(); it++)
00051         {
00052                 std::string filename = *it;
00053                 if (filename.size())
00054                 {
00055                         if (validate(filename))
00056                         {
00057                                 StdoutLog log;
00058                                 SocketHandler h(&log);
00059                                 MetainfoSocket x(h, filename);
00060                                 x.SetFlushBeforeClose();
00061                                 x.Open(host, port);
00062                                 h.Add(&x);
00063                                 h.Select(1,0);
00064                                 while (h.GetCount())
00065                                 {
00066                                         h.Select(1,0);
00067                                 }
00068                         }
00069                 }
00070                 else
00071                 {
00072                         printf("Usage: %s [-host <host>] [-port <port>] <file>\n", *argv);
00073                 }
00074         }
00075         return 0;
00076 }
00077 
00078 
00079 bool validate(const std::string& file)
00080 {
00081         try
00082         {
00083                 FILE *fil = fopen(file.c_str(), "rb");
00084                 if (fil)
00085                 {
00086                         BString meta;
00087                         meta.read_file(fil);
00088                         fclose(fil);
00089                 }
00090                 else
00091                 {
00092                         printf("Couldn't open '%s'...\n", file.c_str());
00093                 }
00094         }
00095         catch (const Exception& e)
00096         {
00097                 printf("Invalid metainfo file: %s\n", e.GetText().c_str());
00098                 return false;
00099         }
00100         return true;
00101 }
00102 
Page, code, and content Copyright (C) 2006 by Anders Hedström
Generated on Mon Aug 29 20:21:47 2005 for C++ Sockets by  doxygen 1.4.4