libktorrent  2.1.1
functions.h
1 /***************************************************************************
2  * Copyright (C) 2005 by Joris Guisson *
3  * joris.guisson@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 #ifndef BTFUNCTIONS_H
21 #define BTFUNCTIONS_H
22 
23 
24 #include <ktorrent_export.h>
25 #include <QString>
26 #include "constants.h"
27 
28 
29 namespace bt
30 {
31  struct TorrentStats;
32 
33  KTORRENT_EXPORT double Percentage(const TorrentStats & s);
34 
35  inline void WriteUint64(Uint8* buf,Uint32 off,Uint64 val)
36  {
37  buf[off + 0] = (Uint8) ((val & 0xFF00000000000000ULL) >> 56);
38  buf[off + 1] = (Uint8) ((val & 0x00FF000000000000ULL) >> 48);
39  buf[off + 2] = (Uint8) ((val & 0x0000FF0000000000ULL) >> 40);
40  buf[off + 3] = (Uint8) ((val & 0x000000FF00000000ULL) >> 32);
41  buf[off + 4] = (Uint8) ((val & 0x00000000FF000000ULL) >> 24);
42  buf[off + 5] = (Uint8) ((val & 0x0000000000FF0000ULL) >> 16);
43  buf[off + 6] = (Uint8) ((val & 0x000000000000FF00ULL) >> 8);
44  buf[off + 7] = (Uint8) ((val & 0x00000000000000FFULL) >> 0);
45  }
46 
47  inline Uint64 ReadUint64(const Uint8* buf,Uint64 off)
48  {
49  Uint64 tmp =
50  ((Uint64)buf[off] << 56) |
51  ((Uint64)buf[off+1] << 48) |
52  ((Uint64)buf[off+2] << 40) |
53  ((Uint64)buf[off+3] << 32) |
54  ((Uint64)buf[off+4] << 24) |
55  ((Uint64)buf[off+5] << 16) |
56  ((Uint64)buf[off+6] << 8) |
57  ((Uint64)buf[off+7] << 0);
58 
59  return tmp;
60  }
61 
62  inline void WriteUint32(Uint8* buf,Uint32 off,Uint32 val)
63  {
64  buf[off + 0] = (Uint8) ((val & 0xFF000000) >> 24);
65  buf[off + 1] = (Uint8) ((val & 0x00FF0000) >> 16);
66  buf[off + 2] = (Uint8) ((val & 0x0000FF00) >> 8);
67  buf[off + 3] = (Uint8) (val & 0x000000FF);
68  }
69 
70  inline Uint32 ReadUint32(const Uint8* buf,Uint32 off)
71  {
72  return (buf[off] << 24) | (buf[off+1] << 16) | (buf[off+2] << 8) | buf[off + 3];
73  }
74 
75  inline void WriteUint16(Uint8* buf,Uint32 off,Uint16 val)
76  {
77  buf[off + 0] = (Uint8) ((val & 0xFF00) >> 8);
78  buf[off + 1] = (Uint8) (val & 0x000FF);
79  }
80 
81  inline Uint16 ReadUint16(const Uint8* buf,Uint32 off)
82  {
83  return (buf[off] << 8) | buf[off + 1];
84  }
85 
86 
87  inline void WriteInt64(Uint8* buf,Uint32 off,Int64 val)
88  {
89  buf[off + 0] = (Uint8) ((val & 0xFF00000000000000ULL) >> 56);
90  buf[off + 1] = (Uint8) ((val & 0x00FF000000000000ULL) >> 48);
91  buf[off + 2] = (Uint8) ((val & 0x0000FF0000000000ULL) >> 40);
92  buf[off + 3] = (Uint8) ((val & 0x000000FF00000000ULL) >> 32);
93  buf[off + 4] = (Uint8) ((val & 0x00000000FF000000ULL) >> 24);
94  buf[off + 5] = (Uint8) ((val & 0x0000000000FF0000ULL) >> 16);
95  buf[off + 6] = (Uint8) ((val & 0x000000000000FF00ULL) >> 8);
96  buf[off + 7] = (Uint8) ((val & 0x00000000000000FFULL) >> 0);
97  }
98 
99  inline Int64 ReadInt64(const Uint8* buf,Uint32 off)
100  {
101  Int64 tmp =
102  ((Int64)buf[off] << 56) |
103  ((Int64)buf[off+1] << 48) |
104  ((Int64)buf[off+2] << 40) |
105  ((Int64)buf[off+3] << 32) |
106  ((Int64)buf[off+4] << 24) |
107  ((Int64)buf[off+5] << 16) |
108  ((Int64)buf[off+6] << 8) |
109  ((Int64)buf[off+7] << 0);
110 
111  return tmp;
112  }
113 
114  inline void WriteInt32(Uint8* buf,Uint32 off,Int32 val)
115  {
116  buf[off + 0] = (Uint8) ((val & 0xFF000000) >> 24);
117  buf[off + 1] = (Uint8) ((val & 0x00FF0000) >> 16);
118  buf[off + 2] = (Uint8) ((val & 0x0000FF00) >> 8);
119  buf[off + 3] = (Uint8) (val & 0x000000FF);
120  }
121 
122  inline Int32 ReadInt32(const Uint8* buf,Uint32 off)
123  {
124  return (Int32)(buf[off] << 24) | (buf[off+1] << 16) | (buf[off+2] << 8) | buf[off + 3];
125  }
126 
127  inline void WriteInt16(Uint8* buf,Uint32 off,Int16 val)
128  {
129  buf[off + 0] = (Uint8) ((val & 0xFF00) >> 8);
130  buf[off + 1] = (Uint8) (val & 0x000FF);
131  }
132 
133  inline Int16 ReadInt16(const Uint8* buf,Uint32 off)
134  {
135  return (Int16)(buf[off] << 8) | buf[off + 1];
136  }
137 
138  KTORRENT_EXPORT void UpdateCurrentTime();
139 
140  KTORRENT_EXPORT extern TimeStamp global_time_stamp;
141 
142  inline TimeStamp CurrentTime() {return global_time_stamp;}
143 
144  KTORRENT_EXPORT TimeStamp Now();
145 
146  KTORRENT_EXPORT QString DirSeparator();
147  KTORRENT_EXPORT bool IsMultimediaFile(const QString & filename);
148 
152  KTORRENT_EXPORT bool MaximizeLimits();
153 
155  KTORRENT_EXPORT Uint32 MaxOpenFiles();
156 
158  KTORRENT_EXPORT Uint32 CurrentOpenFiles();
159 
161  KTORRENT_EXPORT bool OpenFileAllowed();
162 
164  KTORRENT_EXPORT void SetNetworkInterface(const QString & iface);
165 
167  KTORRENT_EXPORT QString NetworkInterface();
168 
170  KTORRENT_EXPORT QString NetworkInterfaceIPAddress(const QString & iface);
171 
173  KTORRENT_EXPORT QStringList NetworkInterfaceIPAddresses(const QString & iface);
174 
176  KTORRENT_EXPORT QString CurrentIPv6Address();
177 
178  const double TO_KB = 1024.0;
179  const double TO_MEG = (1024.0 * 1024.0);
180  const double TO_GIG = (1024.0 * 1024.0 * 1024.0);
181 
182  KTORRENT_EXPORT QString BytesToString(bt::Uint64 bytes);
183  KTORRENT_EXPORT QString BytesPerSecToString(double speed);
184  KTORRENT_EXPORT QString DurationToString(bt::Uint32 nsecs);
185 
186  template<class T> int CompareVal(T a,T b)
187  {
188  if (a < b)
189  return -1;
190  else if (a > b)
191  return 1;
192  else
193  return 0;
194  }
195 
196  template<class T> QString hex(T val)
197  {
198  return QStringLiteral("0x%1").arg(val,0,16);
199  }
200 
201  struct KTORRENT_EXPORT RecursiveEntryGuard
202  {
203  bool* guard;
204 
205  RecursiveEntryGuard(bool* g) : guard(g)
206  {
207  *guard = true;
208  }
209 
210  ~RecursiveEntryGuard()
211  {
212  *guard = false;
213  }
214  };
215 
219  KTORRENT_EXPORT bool InitLibKTorrent();
220 }
221 
222 #endif