Follow this link to skip to the main content

bits.cc

Go to the documentation of this file.
00001 // -*-c++-*-
00002 //----------------------------< /-/ CLARAty /-/ >------------------------------
00003 /**
00004  * @file  bits.cc
00005  *
00006  * Bit field manipulations. Bits are used for Digital I/O
00007  *
00008  * <br>@b Designer(s):  Issa A.D. Nesnas, Won S. Kim
00009  * <br>@b Author(s):    Won S. Kim, Issa A.D. Nesnas
00010  * <br>@b Date:         August 16, 2002
00011  *
00012  * <b>Software License:</b><br>
00013  * <code> http://claraty.jpl.nasa.gov/license/open_src/  or 
00014  *        file: license/open_src.txt </code> 
00015  *
00016  * &copy; 2006, Jet Propulsion Laboratory, California Institute of Technology<br>
00017  *
00018  * $Revision: 1.5 $
00019  */
00020 //-----------------------------------------------------------------------------
00021 
00022 #include "claraty/bits.h"
00023 #include <stdio.h>              // for sprintf()
00024 
00025 using namespace std;
00026 using namespace claraty;
00027 
00028 //-------------------------------------------------------------------------
00029 //------------------------<< Bits Functions >>---------------------------
00030 //-------------------------------------------------------------------------
00031 
00032 namespace claraty {
00033 
00034 string Bits::to_str() const
00035 {
00036   // Print out the bit pattern of the Bits object
00037   //
00038   char buffer[128], *output;
00039   output = new char[256];               // for contiguous printing on VxWorks
00040 
00041   sprintf(buffer, "[%2d] {", static_cast<unsigned int>(_Nbits));
00042   strcpy(output, buffer);
00043 
00044 
00045   // Convert the Bits to hex ascii
00046   //
00047   int n_bytes = (_Nbits + 7)/8;         // number of bytes
00048   for (int i = n_bytes - 1; i >= 0; --i) {
00049     sprintf(buffer, "%2x ", static_cast<unsigned int>((_data >> i*8) & 0xff));
00050     strcat(output, buffer);
00051   }
00052   strcat(output, "}  ");
00053 
00054   // Print the bit pattern --00 0110 0010 ...
00055   //
00056   int n = (_Nbits + 3) / 4 * 4; // total number of bits in 4-bit period
00057   int j=0;                      // buffer index for buffer[j];
00058 
00059   for (size_t i = n - 1; i >= 0; i--) {    // i = bit index for b[i]
00060     if (i >= _Nbits) {
00061       buffer[j++] = '-';
00062       continue;
00063     }
00064 
00065     // if (_unchecked_test(i))
00066     if (test(i))
00067       buffer[j++] = '1';
00068     else
00069       buffer[j++] = '0';
00070 
00071     if ( (i % 4) == 0  &&  i != 0)      // == and != have precedence over &&
00072       buffer[j++] = ' ';
00073   }
00074   buffer[j] = 0;                // null character ('\0' = 0)
00075 
00076   strcat(output, buffer);
00077 
00078   string s(output);
00079   delete [] output;
00080 
00081   return s;
00082 }
00083 //-----------------------------------------------------------------------------
00084 /**
00085  * Print function.
00086  *
00087  * @param [in]   msg
00088  */
00089 
00090 void  Bits::print(const char* msg) const
00091 {
00092   cout << msg << "\n  " << to_str().c_str() << endl;    // c_str() will not be
00093                                                         // needed in the future
00094 }
00095 
00096 ostream& operator<<(ostream& ostr, const Bits& rhs)
00097 {
00098   ostr << rhs.to_str().c_str();
00099 
00100   return ostr;
00101 }
00102 //-------------------------------------------------------------------------
00103 
00104 } // namespace claraty