Logo
~Apps~
~Projects~
~Contact~


bstr.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 #include <stdio.h>
00024 #include <openssl/sha.h>
00025 
00026 #include "BTDictionary.h"
00027 #include "BString.h"
00028 #include "Exception.h"
00029 
00030 
00031 int main(int argc,char *argv[])
00032 {
00033   try
00034   {
00035   BString meta;
00036   if (argc > 1)
00037   {
00038     FILE *fil = fopen(argv[1], "rb");
00039     if (fil)
00040     {
00041       meta.read_file(fil);
00042       fclose(fil);
00043     }
00044   }
00045   // show info
00046   printf("file          : %s\n", argv[1]);
00047   printf("info hash     : %s\n", meta.GetHashAsString("info").c_str());
00048   {
00049     BTString *p = meta.GetString("announce");
00050     if (p)
00051       printf("announce url  : %s\n", p -> GetValue().c_str());
00052   }
00053   {
00054     BTString *p = meta.GetString("info.name");
00055     if (p)
00056       printf("info.name     : %s\n", p -> GetValue().c_str());
00057   }
00058   BTInteger *piecelength = meta.GetInteger("info.piece length");
00059   int64_t pl = 0;
00060   if (piecelength)
00061   {
00062     printf("info.piece length : %s\n", piecelength -> GetValue().c_str());
00063     pl = piecelength -> GetVal();
00064   }
00065   BTInteger *length = meta.GetInteger("info.length");
00066   int64_t sz = 0;
00067   if (length)
00068   {
00069     printf("info.length   : %s\n", length -> GetValue().c_str());
00070     sz = length -> GetVal();
00071   }
00072   else // check files
00073   {
00074     BTObject *p = meta.GetBTObject("info.files");
00075     BTList *files = dynamic_cast<BTList *>(p);
00076     if (files)
00077     {
00078       btobject_v& ref = files -> GetList();
00079       for (btobject_v::iterator it = ref.begin(); it != ref.end(); it++)
00080       {
00081         BTDictionary *p = dynamic_cast<BTDictionary *>(*it);
00082         if (p)
00083         {
00084           BTInteger *length = dynamic_cast<BTInteger *>(p -> Find("length"));
00085           BTList *path = dynamic_cast<BTList *>(p -> Find("path"));
00086           if (path)
00087           {
00088             btobject_v& ref = path -> GetList();
00089             printf("path          : .");
00090             for (btobject_v::iterator it = ref.begin(); it != ref.end(); it++)
00091             {
00092               BTString *p = dynamic_cast<BTString *>(*it);
00093               if (p)
00094                 printf("/%s", p -> GetValue().c_str());
00095             }
00096             printf("\n");
00097           }
00098           if (length)
00099           {
00100             printf("length        : %s\n", length -> GetValue().c_str());
00101             sz += length -> GetVal();
00102           }
00103         }
00104       }
00105     }
00106   }
00107   BTString *pieces = meta.GetString("info.pieces");
00108   if (pieces)
00109   {
00110     int64_t q = sz / pl; // valid
00111     printf("size : %lld ( %lld x %lld + %lld )\n", sz, q, pl, sz - q * pl);
00112   }
00113   }
00114   catch (const Exception& e)
00115   {
00116     printf("Exception: %s\n", e.GetText().c_str());
00117   }
00118 }
00119 
00120 
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