filename_list.cpp

Go to the documentation of this file.
00001 #ifndef FILENAME_LIST_IMPLEMENTATION_FILE
00002 #define FILENAME_LIST_IMPLEMENTATION_FILE
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : filename_list                                                     *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 2005-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "filename_list.h"
00019 
00020 #include <basis/log_base.h>
00021 #include <basis/string_array.h>
00022 #include <data_struct/amorph.cpp>
00023 
00024 filename_list::filename_list() : amorph<file_info>() {}
00025 
00026 filename_list &filename_list::operator =(const filename_list &to_copy)
00027 {
00028   if (this == &to_copy) return *this;
00029   reset();
00030   for (int i = 0; i < to_copy.elements(); i++) {
00031     append(new file_info(*to_copy.get(i)));
00032   }
00033   return *this;
00034 }
00035 
00036 int filename_list::total_files() const { return elements(); }
00037 
00038 void filename_list::pack(byte_array &packed_form) const
00039 { amorph_pack(packed_form, *this); }
00040 
00041 bool filename_list::unpack(byte_array &packed_form)
00042 { return amorph_unpack(packed_form, *this); }
00043 
00044 double filename_list::total_size() const
00045 {
00046   double to_return = 0;
00047   for (int i = 0; i < elements(); i++)
00048     to_return += get(i)->_file_size;
00049   return to_return;
00050 }
00051 
00052 bool filename_list::calculate_progress(const filename &file,
00053     double current_offset, int &current_file, double &current_size)
00054 {
00055   current_file = 0;
00056   current_size = 0;
00057   int posn = locate(file);
00058   if (negative(posn)) {
00059     if (file.raw().t()) return false;  // not a member.
00060     // they're at the start of the run.
00061     current_file = 1;
00062     return true;
00063   }
00064   current_file = posn + 1;  // position zero in array means file number 1.
00065   double size_finished = 0;
00066   // iterate on all files before the current one.
00067   for (int i = 0; i < posn; i++) {
00068     size_finished += get(i)->_file_size;
00069   }
00070   current_size = size_finished + current_offset;
00071   return true;
00072 }
00073 
00074 filename_list &filename_list::operator = (const string_array &to_copy)
00075 {
00076   reset();
00077   for (int i = 0; i < to_copy.length(); i++) {
00078     append(new file_info(to_copy[i]));
00079   }
00080   return *this;
00081 }
00082 
00083 void filename_list::fill(string_array &to_fill)
00084 {
00085   to_fill.reset();
00086   for (int i = 0; i < elements(); i++) {
00087     to_fill += get(i)->raw();
00088   }
00089 }
00090 
00091 const file_info *filename_list::find(const filename &to_check) const
00092 {
00093   for (int i = 0; i < elements(); i++) {
00094 #if defined(__WIN32__) || defined(__VMS__)
00095     if (to_check.raw().iequals(get(i)->raw())) return get(i);
00096 #else
00097     if (to_check.raw() == get(i)->raw()) return get(i);
00098 #endif
00099   }
00100   return NIL;
00101 }
00102 
00103 int filename_list::locate(const filename &to_find) const
00104 {
00105   for (int i = 0; i < elements(); i++) {
00106 #if defined(__WIN32__) || defined(__VMS__)
00107     if (to_find.raw().iequals(get(i)->raw())) return i;
00108 #else
00109     if (to_find.raw() == get(i)->raw()) return i;
00110 #endif
00111   }
00112   return common::NOT_FOUND;
00113 }
00114 
00115 bool filename_list::member(const filename &to_check) const
00116 {
00117   for (int i = 0; i < elements(); i++) {
00118 #if defined(__WIN32__) || defined(__VMS__)
00119     if (to_check.raw().iequals(get(i)->raw())) return true;
00120 #else
00121     if (to_check.raw() == get(i)->raw()) return true;
00122 #endif
00123   }
00124   return false;
00125 }
00126 
00127 bool filename_list::member(const file_info &to_check, bool check_size,
00128     bool check_checksum) const
00129 {
00130   for (int i = 0; i < elements(); i++) {
00131 #if defined(__WIN32__) || defined(__VMS__)
00132     if (to_check.raw().iequals(get(i)->raw())) {
00133 #else
00134     if (to_check.raw() == get(i)->raw()) {
00135 #endif
00136       // once we have matched a name, the other checks will cause us to
00137       // reject any other potential matches after this one if the requested
00138       // check fails.
00139       if (check_size && (to_check._file_size != get(i)->_file_size) )
00140         return false;
00141       if (check_checksum && (to_check._checksum != get(i)->_checksum) )
00142         return false;
00143       return true;
00144     }
00145   }
00146   return false;
00147 }
00148 
00149 istring filename_list::text_form() const
00150 {
00151   istring to_return;
00152 //hmmm: a length limit might be nice?
00153   for (int i = 0; i < elements(); i++) {
00154     to_return += isprintf("%d. ", i + 1);
00155     if (get(i))
00156       to_return += get(i)->text_form();
00157     if (i != elements() - 1)
00158       to_return += log_base::platform_ending();
00159   }
00160   return to_return;
00161 }
00162 
00163 
00164 #endif //FILENAME_LIST_IMPLEMENTATION_FILE
00165 

Generated on Fri Nov 28 04:29:24 2008 for HOOPLE Libraries by  doxygen 1.5.1