Logo
~Apps~
~Projects~
~Contact~


bstr.cpp File Reference

#include <stdio.h>
#include <openssl/sha.h>
#include "BTDictionary.h"
#include "BString.h"
#include "Exception.h"

Include dependency graph for bstr.cpp:

Go to the source code of this file.


Functions

int main (int argc, char *argv[])

Function Documentation

int main ( int  argc,
char *  argv[] 
)

File ......... bstr.cpp Published .... 2005-06-14 Author ....... grymse@alhem.net

Definition at line 31 of file bstr.cpp.

References BString::GetBTObject(), BString::GetHashAsString(), BString::GetInteger(), BString::GetString(), Exception::GetText(), and BString::read_file().

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 }

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