version_record.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : version structures: version, version_record                       *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *******************************************************************************
00007 * Copyright (c) 1996-$now By Author.  This program is free software; you can  *
00008 * redistribute it and/or modify it under the terms of the GNU General Public  *
00009 * License as published by the Free Software Foundation; either version 2 of   *
00010 * the License or (at your option) any later version.  This is online at:      *
00011 *     http://www.fsf.org/copyleft/gpl.html                                    *
00012 * Please send any updates to: fred@gruntose.com                               *
00013 \*****************************************************************************/
00014 
00015 #include "string_array.h"
00016 #include "version_record.h"
00017 
00018 #include <basis/functions.h>
00019 
00020 #include <ctype.h>
00021 
00022 using namespace basis;
00023 
00024 namespace structures {
00025 
00026 void *version::__global_module_handle()
00027 {
00028   static void *__mod_hand = 0;
00029   return __mod_hand;
00030 }
00031 
00032 version::version()
00033 : _components(new string_array)
00034 {
00035   set_component(MAJOR, "0");
00036   set_component(MINOR, "0");
00037   set_component(REVISION, "0");
00038   set_component(BUILD, "0");
00039 }
00040 
00041 version::version(const string_array &version_info)
00042 : _components(new string_array(version_info))
00043 {
00044   for (int i = 0; i < _components->length(); i++) {
00045     if (!(*_components)[i]) {
00046       // this component is blank; replace it with a zero.
00047       (*_components)[i] = "0";
00048     }
00049   }
00050 }
00051 
00052 version::version(const astring &formatted_string)
00053 : _components(new string_array)
00054 {
00055   astring verstring = formatted_string;
00056   // scan through and replace bogus bits with reasonable bits.
00057   for (int j = 0; j < verstring.length(); j++) {
00058     if (verstring[j] == ',') verstring[j] = '.';
00059       // replace commas with periods.
00060   }
00061   // locate the pieces of the version string.
00062   for (int i = 0; i < verstring.length(); i++) {
00063     int perindy = verstring.find('.', i);
00064     if (negative(perindy)) {
00065       if (verstring.length() - i > 0) {
00066         // add any extra bits after the last period in the string.
00067         *_components += verstring.substring(i, verstring.end());
00068       }
00069       break;
00070     }
00071     // we found a period, so include anything between the current position and
00072     // that as a component.
00073     *_components += verstring.substring(i, perindy - 1);
00074     i = perindy;
00075       // set i to be at the next period; it will be incremented past that.
00076   }
00077 }
00078 
00079 version::version(int maj, int min, int rev, int build)
00080 : _components(new string_array)
00081 {
00082   *this = version(a_sprintf("%u.%u.%u.%u", basis::un_int(maj), basis::un_int(min), basis::un_int(rev),
00083       basis::un_int(build)));
00084 }
00085 
00086 version::version(const version &to_copy)
00087 : _components(new string_array)
00088 { *this = to_copy; }
00089 
00090 version::~version() { WHACK(_components); }
00091 
00092 version &version::operator =(const version &to_copy)
00093 {
00094   if (this != &to_copy) *_components = *to_copy._components;
00095   return *this;
00096 }
00097 
00098 astring version::text_form() const { return flex_text_form(); }
00099 
00100 int version::components() const { return _components->length(); }
00101 
00102 int version::v_major() const
00103 { return int(get_component(MAJOR).convert(0)); }
00104 
00105 int version::v_minor() const
00106 { return int(get_component(MINOR).convert(0)); }
00107 
00108 int version::v_revision() const
00109 { return int(get_component(REVISION).convert(0)); }
00110 
00111 int version::v_build() const
00112 { return int(get_component(BUILD).convert(0)); }
00113 
00114 astring version::get_component(int index) const
00115 {
00116   bounds_return(index, 0, _components->length() - 1, "0");
00117   return (*_components)[index];
00118 }
00119 
00120 void version::set_component(int index, const astring &to_set)
00121 {
00122   if (_components->length() <= index)
00123     _components->resize(index + 1);
00124   if (to_set.t())
00125     (*_components)[index] = to_set;
00126   else
00127     (*_components)[index] = "0";
00128 }
00129 
00130 bool version::equal_to(const equalizable &to_test) const
00131 {
00132   const version *cast = cast_or_throw(to_test, *this);
00133   return *_components == *cast->_components;
00134 }
00135 
00136 bool version::less_than(const orderable &to_test) const
00137 {
00138   const version *cast = cast_or_throw(to_test, *this);
00139   if (v_major() < cast->v_major()) return true;
00140   else if (v_major() > cast->v_major()) return false;
00141   if (v_minor() < cast->v_minor()) return true;
00142   else if (v_minor() > cast->v_minor()) return false;
00143   if (v_revision() < cast->v_revision()) return true;
00144   else if (v_revision() > cast->v_revision()) return false;
00145   if (v_build() < cast->v_build()) return true;
00146   return false;
00147 }
00148 
00149 bool version::compatible(const version &to_test) const
00150 {
00151 //fix to be more general
00152   return (v_major() == to_test.v_major())
00153       && (v_minor() == to_test.v_minor())
00154       && (v_revision() == to_test.v_revision());
00155 }
00156 
00157 bool version::bogus() const
00158 { return !v_major() && !v_minor() && !v_revision() && !v_build(); }
00159 
00160 #define SEPARATE \
00161   if (style != DOTS) to_return += ", "; \
00162   else to_return += "."
00163 
00164 astring version::flex_text_form(version_style style, int where) const
00165 {
00166   // note: the conversions below are to ensure proper treatment of the
00167   // size of the int16 types by win32.
00168   astring to_return;
00169 
00170   if (style == DETAILED) to_return += "major ";
00171   astring temp = get_component(MAJOR);
00172   if (!temp) temp = "0";
00173   to_return += temp;
00174   if (where == MAJOR) return to_return;
00175   SEPARATE;
00176 
00177   if (style == DETAILED) to_return += "minor ";
00178   temp = get_component(MINOR);
00179   if (!temp) temp = "0";
00180   to_return += temp;
00181   if (where == MINOR) return to_return;
00182   SEPARATE;
00183 
00184   if (style == DETAILED) to_return += "revision ";
00185   temp = get_component(REVISION);
00186   if (!temp) temp = "0";
00187   to_return += temp;
00188   if (where == REVISION) return to_return;
00189   SEPARATE;
00190 
00191   if (style == DETAILED) to_return += "build ";
00192   temp = get_component(BUILD);
00193   if (!temp) temp = "0";
00194   to_return += temp;
00195 
00196   // other components don't have handy names.
00197   if (where > BUILD) {
00198     for (int i = BUILD + 1; i < where; i++) {
00199       SEPARATE;
00200       if (style == DETAILED) to_return += a_sprintf("part_%d ", i + 1);
00201       temp = get_component(i);
00202       if (!temp) temp = "0";
00203       to_return += temp;
00204     }
00205   }
00206 
00207   return to_return;
00208 }
00209 
00210 version version::from_text(const astring &to_convert)
00211 { return version(to_convert); }
00212 
00213 int version::packed_size() const
00214 {
00215   return _components->packed_size();
00216 }
00217 
00218 void version::pack(byte_array &target) const
00219 { _components->pack(target); }
00220 
00221 bool version::unpack(byte_array &source)
00222 {
00223   if (!_components->unpack(source)) return false;
00224   return true;
00225 }
00226 
00228 
00229 #define VR_NEWLINE to_return += "\n"
00230 
00231 version_record::~version_record()
00232 {}
00233 
00234 astring version_record::text_form() const
00235 {
00236   astring to_return;
00237   to_return += "Description: ";
00238   to_return += description;
00239   VR_NEWLINE;
00240   to_return += "File Version: ";
00241   to_return += file_version.text_form();
00242   VR_NEWLINE;
00243   to_return += "Internal Name: ";
00244   to_return += internal_name;
00245   VR_NEWLINE;
00246   to_return += "Original Name: ";
00247   to_return += original_name;
00248   VR_NEWLINE;
00249   to_return += "Product Name: ";
00250   to_return += product_name;
00251   VR_NEWLINE;
00252   to_return += "Product Version: ";
00253   to_return += product_version.text_form();
00254   VR_NEWLINE;
00255   to_return += "Company Name: ";
00256   to_return += company_name;
00257   VR_NEWLINE;
00258   to_return += "Copyright: ";
00259   to_return += copyright;
00260   VR_NEWLINE;
00261   to_return += "Trademarks: ";
00262   to_return += trademarks;
00263   VR_NEWLINE;
00264 
00265   return to_return;
00266 }
00267 
00268 } //namespace.
00269 
Generated on Sat Jan 28 04:22:28 2012 for hoople2 project by  doxygen 1.6.3