home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ocl150a.zip / OCL / Source / OSysLevel.cpp < prev    next >
C/C++ Source or Header  |  1996-08-12  |  11KB  |  522 lines

  1. // OCL - OS/2 Class Library
  2. // (c) Cubus 1995
  3. // All Rights Reserved
  4. // OSysLevel.cpp
  5.  
  6.  
  7. /*
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Neither the name Cubus nor the name Team OCL may be used to
  14.  *    endorse or promote products derived from this software
  15.  *    without specific prior written permission.
  16.  * 3. See OCL.INF for a detailed copyright notice.
  17.  *
  18.  *              THIS SOFTWARE IS PROVIDED ``AS IS'' AND
  19.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28.  * SUCH DAMAGE.
  29.  */
  30.  
  31.  
  32. // $Header: W:/Projects/OCL/Source/rcs/OSysLevel.cpp 1.50 1996/08/11 23:49:33 B.STEIN Release $
  33.  
  34. #define __OCL_SOURCE__
  35.  
  36. #define OINCL_OSTRING
  37. #define OINCL_BASE
  38.  
  39. #include <ocl.hpp>
  40. #include <OSysLevel.hpp>
  41. #include <OMessage.hpp>
  42.  
  43.  
  44. OSysLevel::OSysLevel()
  45. {
  46.  memset(&slfheader, 0, sizeof(SLFHEADER));
  47.  strcpy((PSZ)&slfheader.achSignature, "SYSLEVEL");
  48.  slfheader.usSignature   = 1;
  49.  slfheader.usSlfVersion  = 1;
  50.  slfheader.ulTableOffset = sizeof(SLFHEADER);
  51.  memset(&systable, 0, sizeof(SYSTABLE));
  52. }
  53.  
  54.  
  55. OSysLevel::OSysLevel(SLFHEADER& header, SYSTABLE&  table)
  56. {
  57.  memcpy(&slfheader, &header, sizeof(SLFHEADER));
  58.  memcpy(&systable,  &table,  sizeof(SYSTABLE));
  59. }   
  60.  
  61.  
  62. OSysLevel::OSysLevel(const OSysLevel& sl)
  63. {
  64.  setHeader(sl.getHeader());
  65.  setSysTable(sl.getSysTable());
  66. }
  67.    
  68.  
  69. OSysLevel::OSysLevel(PCSZ fileName)
  70.  : slfile(fileName)
  71. {
  72.  memset(&slfheader, 0, sizeof(SLFHEADER));
  73.  strcpy((PSZ)&slfheader.achSignature, "SYSLEVEL");
  74.  slfheader.usSignature   = 1;
  75.  slfheader.usSlfVersion  = 1;
  76.  slfheader.ulTableOffset = sizeof(SLFHEADER);
  77.  memset(&systable, 0, sizeof(SYSTABLE));
  78. }
  79.  
  80.  
  81. OSysLevel::~OSysLevel()
  82.  {}
  83.  
  84.  
  85. PSZ OSysLevel::isOfType() const
  86. {
  87.  return("OSysLevel");
  88.  
  89.  
  90.  
  91. OSysLevel& OSysLevel::readFromFile(PCSZ fileName)
  92. {
  93. #ifdef __IBMCPP__
  94.  ifstream sysfile(fileName);
  95. #else
  96.  ifstream sysfile((PSZ)fileName);
  97. #endif
  98.  OString  stamp(10);
  99.  
  100.  if (!sysfile)
  101.    throw OVioException(OMessage(220, OCL::MSGFILE, (PSZ)fileName),
  102.                        ERROR_READ_FAULT);
  103.  
  104.  sysfile.read((PSZ)&slfheader, sizeof(SLFHEADER));
  105.  sysfile.read((PSZ)&systable, sizeof(SYSTABLE));
  106.  
  107.  memcpy(stamp.getText(), &slfheader.achSignature,
  108.         sizeof(slfheader.achSignature));
  109.  
  110.  if (stamp != "SYSLEVEL")
  111.    throw OVioException(OMessage(221, OCL::MSGFILE, (PSZ)fileName),
  112.                        ERROR_INVALID_DATA);
  113.  
  114.  return(*this);
  115. }
  116.  
  117. OSysLevel& OSysLevel::writeToFile(PCSZ fileName)
  118. {
  119. #ifdef __IBMCPP__
  120.  ofstream sysfile(fileName);
  121. #else
  122.  ofstream sysfile((PSZ)fileName);
  123. #endif
  124.  
  125.  if (!sysfile)
  126.    throw OVioException(OMessage(220, OCL::MSGFILE, (PSZ)fileName),
  127.                        ERROR_WRITE_FAULT);
  128.  
  129.  sysfile.write((PSZ)&slfheader, sizeof(SLFHEADER));
  130.  sysfile.write((PSZ)&systable, sizeof(SYSTABLE));
  131.  
  132.  return(*this);
  133. }
  134.  
  135.  
  136. OSysLevel& OSysLevel::read()
  137. {
  138.  if (!slfile)
  139.    throw OVioException(OCL::error(222), 0);
  140.  
  141.  return(readFromFile(slfile));
  142. }
  143.  
  144.  
  145. OSysLevel& OSysLevel::write()
  146. {
  147.  if (!slfile)
  148.    throw OVioException(OCL::error(222), 0);
  149.  
  150.  return(writeToFile(slfile));
  151. }
  152.  
  153.  
  154. OSysLevel& OSysLevel::setSysTable(const SYSTABLE& stable)
  155. {
  156.  memcpy(&systable,  &stable, sizeof(SYSTABLE));
  157.  return(*this);
  158. }
  159.  
  160. OSysLevel& OSysLevel::setHeader(const SLFHEADER& slh)
  161. {
  162.  memcpy(&slfheader, &slh, sizeof(SLFHEADER));
  163.  return(*this);
  164. }
  165.  
  166. OSysLevel& OSysLevel::setFileName(PCSZ fileName)
  167. {
  168.  slfile << fileName;
  169.  return(*this);
  170. }
  171.  
  172.  
  173. OSysLevel& OSysLevel::setDate()
  174. {
  175.  DATETIME dt;
  176.  
  177.  DosGetDateTime(&dt);
  178.  sprintf((PSZ)&slfheader.achJulian, "%ld", DTToJulian(dt));
  179.  
  180.  return(*this);
  181. }
  182.  
  183. OSysLevel& OSysLevel::setDate(DATETIME& dt)
  184. {
  185.  sprintf((PSZ)&slfheader.achJulian, "%ld", DTToJulian(dt));
  186.  return(*this);
  187. }
  188.  
  189.  
  190. OSysLevel& OSysLevel::setDate(PSZ dateStr)
  191. {
  192.  if (!dateStr)
  193.    memset(&slfheader.achJulian, 0, sizeof(slfheader.achJulian));
  194.  else
  195.    memcpy(&slfheader.achJulian, dateStr,
  196.           strlen(dateStr) < sizeof(slfheader.achJulian) ?
  197.           strlen(dateStr) : sizeof(slfheader.achJulian));
  198.  return(*this);
  199. }
  200.  
  201.  
  202. OSysLevel& OSysLevel::setTableOffset(ULONG offset) 
  203. {
  204.  slfheader.ulTableOffset = offset;
  205.  return(*this);
  206. }
  207.  
  208.  
  209. OSysLevel& OSysLevel::setSysId(USHORT sysId)
  210. {
  211.  systable.usSysId = sysId;
  212.  return(*this);
  213. }
  214.  
  215.  
  216. OSysLevel& OSysLevel::setSysEdition(BYTE sysEd)
  217. {
  218.  systable.bSysEdition = sysEd;
  219.  return(*this);
  220. }
  221.  
  222.  
  223. OSysLevel& OSysLevel::setSysVersion(BYTE sysVer)
  224. {
  225.  systable.bSysVersion = sysVer;
  226.  return(*this);
  227. }
  228.  
  229.  
  230. OSysLevel& OSysLevel::setSysModify(BYTE sysMod)
  231. {
  232.  systable.bSysModify = sysMod;
  233.  return(*this);
  234. }
  235.  
  236.  
  237. OSysLevel& OSysLevel::setSysDate(USHORT usDate)
  238. {
  239.  systable.usSysDate = usDate;
  240.  return(*this);
  241. }
  242.  
  243.  
  244. OSysLevel& OSysLevel::setCsdLevel(PSZ level)
  245. {
  246.  if (!level)
  247.    memset(&systable.achCsdLevel, 0, sizeof(systable.achCsdLevel));
  248.  else
  249.    memcpy(&systable.achCsdLevel, level,
  250.           strlen(level) < sizeof(systable.achCsdLevel) ?
  251.           strlen(level) : sizeof(systable.achCsdLevel));
  252.  return(*this);
  253. }
  254.  
  255.  
  256. OSysLevel& OSysLevel::setCsdPrev(PSZ level)
  257. {
  258.  if (!level)
  259.    memset(&systable.achCsdPrev, 0, sizeof(systable.achCsdPrev));
  260.  else
  261.    memcpy(&systable.achCsdPrev, level,
  262.           strlen(level) < sizeof(systable.achCsdPrev) ?
  263.           strlen(level) : sizeof(systable.achCsdPrev));
  264.  return(*this);
  265. }
  266.  
  267.  
  268. OSysLevel& OSysLevel::setSysName(PSZ sysname)
  269. {
  270.  if (!sysname)
  271.    memset(&systable.achSysName, 0, sizeof(systable.achSysName));
  272.  else
  273.    memcpy(&systable.achSysName, sysname,
  274.           strlen(sysname) < sizeof(systable.achSysName) ?
  275.           strlen(sysname) : sizeof(systable.achSysName));
  276.  return(*this);
  277. }
  278.  
  279.  
  280. OSysLevel& OSysLevel::setCompId(PSZ compid)
  281. {
  282.  if (!compid)
  283.    memset(&systable.achCompId, 0, sizeof(systable.achCompId));
  284.  else
  285.    memcpy(&systable.achCompId, compid,
  286.           strlen(compid) < sizeof(systable.achCompId) ?
  287.           strlen(compid) : sizeof(systable.achCompId));
  288.  return(*this);
  289. }
  290.  
  291.  
  292. OSysLevel& OSysLevel::setRefreshLevel(BYTE refLevel)
  293. {
  294.  systable.bRefreshLevel = refLevel;
  295.  return(*this);
  296. }
  297.  
  298.  
  299. OSysLevel& OSysLevel::setType(PSZ type)
  300. {
  301.  if (!type)
  302.    memset(&systable.achType, 0, sizeof(systable.achType));
  303.  else
  304.    memcpy(&systable.achType, type,
  305.           strlen(type) < sizeof(systable.achType) ?
  306.           strlen(type) : sizeof(systable.achType));
  307.  return(*this);
  308. }
  309.  
  310.  
  311. const OSysLevel& OSysLevel::getDate(DATETIME& dt) const
  312. {
  313.  memset(&dt, 0, sizeof(DATETIME));
  314.  JulianToDT(atol((PSZ)&slfheader.achJulian), dt);
  315.  return(*this);
  316. }
  317.  
  318.  
  319.  
  320. ULONG OSysLevel::getTableOffset() const
  321. {
  322.  return(slfheader.ulTableOffset);
  323. }
  324.  
  325.  
  326.  
  327. USHORT OSysLevel::getSysId() const
  328. {
  329.  return(systable.usSysId);
  330. }
  331.  
  332.  
  333.  
  334. USHORT OSysLevel::getSysDate() const
  335. {
  336.  return(systable.usSysDate);
  337. }
  338.  
  339.  
  340.  
  341. PCSZ OSysLevel::getFileName() const
  342. {
  343.  return(slfile.getText());
  344. }
  345.  
  346.  
  347.  
  348. PCSZ OSysLevel::getDateAsStr(OString& str) const
  349. {
  350.  DATETIME dt;
  351.  
  352.  str << "00.00.0000";
  353.  JulianToDT(atol((PSZ)&slfheader.achJulian), dt);
  354.  sprintf(str.getText(), "%02ld.%02ld.%ld", dt.day, dt.month, dt.year);
  355.  return(str.getText());
  356. }
  357.  
  358.  
  359.  
  360. PCSZ OSysLevel::getCsdLevel() const
  361. {
  362.  return((PCSZ)systable.achCsdLevel);
  363. }
  364.  
  365.  
  366.  
  367. PCSZ OSysLevel::getCsdPrev() const
  368. {
  369.  return((PCSZ)systable.achCsdPrev);
  370. }
  371.  
  372.  
  373.  
  374. PCSZ OSysLevel::getSysName() const
  375. {
  376.  return((PCSZ)systable.achSysName);
  377. }
  378.  
  379.  
  380.  
  381. PCSZ OSysLevel::getCompId() const
  382. {
  383.  return((PCSZ)systable.achCompId);
  384. }
  385.  
  386.  
  387.  
  388. PCSZ OSysLevel::getType() const
  389. {
  390.  return((PCSZ)systable.achType);
  391. }
  392.  
  393.  
  394.  
  395. BYTE OSysLevel::getSysEdition() const
  396. {
  397.  return(systable.bSysEdition);
  398. }
  399.  
  400.  
  401.  
  402. BYTE OSysLevel::getSysVersion() const
  403. {
  404.  return(systable.bSysVersion);
  405. }
  406.  
  407.  
  408.  
  409. BYTE OSysLevel::getSysModify() const
  410. {
  411.  return(systable.bSysModify);
  412. }
  413.  
  414.  
  415.  
  416. BYTE OSysLevel::getRefreshLevel() const
  417. {
  418.  return(systable.bRefreshLevel);
  419. }
  420.  
  421.  
  422.  
  423. const SLFHEADER& OSysLevel::getHeader() const
  424. {
  425.  return(slfheader);
  426. }
  427.  
  428.  
  429. const SYSTABLE& OSysLevel::getSysTable() const
  430. {
  431.  return(systable);
  432. }
  433.  
  434.  
  435. // conversion routines for julian dates
  436. // these funtions are strictly for internal use only
  437. // and will be moved to the ODate when this class
  438. // is ready for shipping
  439.  
  440. const INT daysToMonth[2][13] =
  441. {
  442. { 0,  31,  59,  90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
  443. { 0,  31,  60,  91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
  444. };
  445.  
  446. const LONG ReformJulianDay = 2361221;
  447. const INT  ReformDays      = 11;
  448. const INT  ReformYEAR      = 1752;
  449. const INT  ReformDAY       = 2;
  450. const INT  ReformMONTH     = 9;
  451.  
  452. void OSysLevel::JulianToDT(LONG julian, DATETIME& dt) const
  453. {
  454.  INT  century;
  455.  INT  days;
  456.  INT  j;
  457.  LONG fours = julian / 1461;
  458.  LONG years = fours * 4;
  459.  BOOL leap  = FALSE;
  460.  
  461.  if (julian > ReformJulianDay) {
  462.    century = (INT) ((julian - 1684595 ) / 36524.25);
  463.    julian += (century * 3L / 4L) - 2;  }
  464.  
  465.  days = julian % 1461;
  466.  
  467.  for (j = 1; j < 5; ++j) if (365 * j >= days) break;
  468.  --j;
  469.  
  470.  if (j == 0)
  471.   {
  472.    if (years <= 4712 + ReformYEAR) {
  473.      leap = TRUE;
  474.      ++days; }
  475.    else {
  476.      if ((years - 4712 ) % 100 != 0 || (years - 4712 ) % 400 == 0) {
  477.        leap = TRUE;
  478.        ++days; }}
  479.   }
  480.  else
  481.    days -= 365 * j, years += j;
  482.  
  483.  for (j = 1; j < 12; ++j)
  484.    if (days <= daysToMonth[leap][j]) break;
  485.  
  486.  dt.month = j;
  487.  dt.day   = days - daysToMonth[leap][j - 1];
  488.  dt.year  = years - 4712;
  489.  if (dt.year <= 0 ) dt.year--;
  490. }
  491.  
  492. LONG OSysLevel::DTToJulian (DATETIME& dt) const
  493. {
  494.  INT  month_days;
  495.  INT  century;
  496.  INT  years      = dt.year + 4712;
  497.  BOOL leap       = FALSE;
  498.  LONG year_days  = 365L * (LONG) years + (LONG)(years >> 2);
  499.  LONG julian_date;
  500.  
  501.  if (dt.year <= 0) dt.year++;
  502.  
  503.  if ((years & 3 ) == 0) {
  504.       leap = TRUE;
  505.       year_days--; }
  506.  
  507.  month_days  = daysToMonth[leap][dt.month - 1];
  508.  julian_date = year_days + (long int)(month_days + dt.day);
  509.  
  510.  if (julian_date > ReformJulianDay)
  511.   {
  512.    years = dt.year - 300;
  513.    if (dt.month <= 2) years--;
  514.    century = years / 100;
  515.    julian_date -= ((century * 3) / 4 + 1);
  516.   }
  517.  return(julian_date);
  518. }
  519.  
  520. // end of source
  521.