StatFile.pm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. ################################################################################
  2. # $Id: StatFile.pm 2241 2007-01-10 02:27:55Z b4rt $
  3. # $Date: 2007-01-09 20:27:55 -0600 (Tue, 09 Jan 2007) $
  4. # $Revision: 2241 $
  5. ################################################################################
  6. # #
  7. # LICENSE #
  8. # #
  9. # This program is free software; you can redistribute it and/or #
  10. # modify it under the terms of the GNU General Public License (GPL) #
  11. # as published by the Free Software Foundation; either version 2 #
  12. # of the License, or (at your option) any later version. #
  13. # #
  14. # This program is distributed in the hope that it will be useful, #
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of #
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  17. # GNU General Public License for more details. #
  18. # #
  19. # To read the license please visit http://www.gnu.org/copyleft/gpl.html #
  20. # #
  21. # #
  22. ################################################################################
  23. package StatFile;
  24. use strict;
  25. use warnings;
  26. ################################################################################
  27. ################################################################################
  28. # fields #
  29. ################################################################################
  30. # version in a var
  31. my $VERSION = do {
  32. my @r = (q$Revision: 2241 $ =~ /\d+/g); sprintf "%d"."%02d" x $#r, @r };
  33. # state
  34. # -1 error
  35. # 0 null
  36. # 1 initialized (stat-file loaded)
  37. my $state = 0;
  38. # message, error etc. keep it in one string for simplicity atm.
  39. my $message = "";
  40. # stat-file
  41. my $statFile = "";
  42. # stat-file-data-hash, keys 1 : 1 StatFile-class of TF
  43. my %data;
  44. # running
  45. # percent_done
  46. # time_left
  47. # down_speed
  48. # up_speed
  49. # transferowner
  50. # seeds
  51. # peers
  52. # sharing
  53. # seedlimit
  54. # uptotal
  55. # downtotal
  56. # size
  57. ################################################################################
  58. # constructor + destructor #
  59. ################################################################################
  60. #------------------------------------------------------------------------------#
  61. # Sub: new #
  62. # Arguments: null or path to stat-file #
  63. # Returns: object reference #
  64. #------------------------------------------------------------------------------#
  65. sub new {
  66. my $class = shift;
  67. my $self = bless ({}, ref ($class) || $class);
  68. # initialize file now if name supplied in ctor
  69. $statFile = shift;
  70. if (defined($statFile)) {
  71. $self->initialize($statFile);
  72. }
  73. return $self;
  74. }
  75. #------------------------------------------------------------------------------#
  76. # Sub: destroy #
  77. # Arguments: null #
  78. # Returns: null #
  79. #------------------------------------------------------------------------------#
  80. sub destroy {
  81. # set state
  82. $state = 0;
  83. # strings
  84. $message = "";
  85. $statFile = "";
  86. # undef
  87. undef %data;
  88. }
  89. ################################################################################
  90. # public methods #
  91. ################################################################################
  92. #------------------------------------------------------------------------------#
  93. # Sub: initialize. this is separated from constructor to call it independent #
  94. # from object-creation. #
  95. # Arguments: path to stat-file #
  96. # Returns: 0|1 #
  97. #------------------------------------------------------------------------------#
  98. sub initialize {
  99. shift; # class
  100. # path-to-stat-file
  101. $statFile = shift;
  102. if (!(defined $statFile)) {
  103. # message
  104. $message = "path-to-stat-file not defined";
  105. # set state
  106. $state = -1;
  107. # return
  108. return 0;
  109. }
  110. # read in stat-file + set fields
  111. if (-f $statFile) {
  112. # sep + open file
  113. my $lineSep = $/;
  114. undef $/;
  115. open(STATFILE,"<$statFile");
  116. # read data
  117. my $content = <STATFILE>;
  118. # close file + sep
  119. close STATFILE;
  120. $/ = $lineSep;
  121. # process data
  122. my @contentary = split(/\n/, $content);
  123. %data = ();
  124. $data{"running"} = shift @contentary;
  125. $data{"percent_done"} = shift @contentary;
  126. $data{"time_left"} = shift @contentary;
  127. $data{"down_speed"} = shift @contentary;
  128. $data{"up_speed"} = shift @contentary;
  129. $data{"transferowner"} = shift @contentary;
  130. $data{"seeds"} = shift @contentary;
  131. $data{"peers"} = shift @contentary;
  132. $data{"sharing"} = shift @contentary;
  133. $data{"seedlimit"} = shift @contentary;
  134. $data{"uptotal"} = shift @contentary;
  135. $data{"downtotal"} = shift @contentary;
  136. $data{"size"} = shift @contentary;
  137. # set state
  138. $state = 1;
  139. # return
  140. return 1;
  141. } else {
  142. # message
  143. $message = "stat-file no file";
  144. # set state
  145. $state = -1;
  146. # return
  147. return 0;
  148. }
  149. }
  150. #------------------------------------------------------------------------------#
  151. # Sub: getVersion #
  152. # Arguments: null #
  153. # Returns: VERSION #
  154. #------------------------------------------------------------------------------#
  155. sub getVersion {
  156. return $VERSION;
  157. }
  158. #------------------------------------------------------------------------------#
  159. # Sub: getState #
  160. # Arguments: null #
  161. # Returns: state #
  162. #------------------------------------------------------------------------------#
  163. sub getState {
  164. return $state;
  165. }
  166. #------------------------------------------------------------------------------#
  167. # Sub: getMessage #
  168. # Arguments: null #
  169. # Returns: message #
  170. #------------------------------------------------------------------------------#
  171. sub getMessage {
  172. return $message;
  173. }
  174. #------------------------------------------------------------------------------#
  175. # Sub: get #
  176. # Arguments: key #
  177. # Returns: value #
  178. #------------------------------------------------------------------------------#
  179. sub get {
  180. shift; # class
  181. my $key = shift;
  182. return $data{$key};
  183. }
  184. #------------------------------------------------------------------------------#
  185. # Sub: set #
  186. # Arguments: key,value #
  187. # Returns: null #
  188. #------------------------------------------------------------------------------#
  189. sub set {
  190. shift; # class
  191. my $key = shift;
  192. $data{$key} = shift;
  193. }
  194. #------------------------------------------------------------------------------#
  195. # Sub: getFilename #
  196. # Arguments: null #
  197. # Returns: string #
  198. #------------------------------------------------------------------------------#
  199. sub getFilename {
  200. return $statFile;
  201. }
  202. #------------------------------------------------------------------------------#
  203. # Sub: getData #
  204. # Arguments: null #
  205. # Returns: hash #
  206. #------------------------------------------------------------------------------#
  207. sub getData {
  208. return %data;
  209. }
  210. #------------------------------------------------------------------------------#
  211. # Sub: write #
  212. # Arguments: null #
  213. # Returns: 1|0 #
  214. #------------------------------------------------------------------------------#
  215. sub write {
  216. # open file
  217. open(STATFILE,">$statFile") or return 0;
  218. my $retVal = 1;
  219. # content
  220. my $content = "";
  221. $content .= $data{"running"}."\n";
  222. $content .= $data{"percent_done"}."\n";
  223. $content .= $data{"time_left"}."\n";
  224. $content .= $data{"down_speed"}."\n";
  225. $content .= $data{"up_speed"}."\n";
  226. $content .= $data{"transferowner"}."\n";
  227. $content .= $data{"seeds"}."\n";
  228. $content .= $data{"peers"}."\n";
  229. $content .= $data{"sharing"}."\n";
  230. $content .= $data{"seedlimit"}."\n";
  231. $content .= $data{"uptotal"}."\n";
  232. $content .= $data{"downtotal"}."\n";
  233. $content .= $data{"size"};
  234. # print
  235. print STATFILE $content or $retVal = 0;
  236. # close file
  237. close(STATFILE);
  238. # return
  239. return $retVal;
  240. }
  241. ################################################################################
  242. # make perl happy #
  243. ################################################################################
  244. 1;