/*
   <p>Test harness for <i>bitsout()</i>.</p>

   <kbd>
   <b>environ - C programming support functions</b><br>
   Copyright (C) 2005  Martin C Gregorie
   
   This program is free software; you can redistribute it and/or
   modify it under the terms of the Lesser GNU General Public License.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the Lesser GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   </kbd>
  
   <p>Contact Martin Gregorie at <i>martin@gregorie.org</i></p>
*/

#include <stdio.h>
#include <string.h>
#include <environ.h>

int main(int argc, char **argv)
{
   char           *arg;
   int            val;
   unsigned int   uval;
   int            bits;
   char           *result;
   
   if (strcmp(argv[1], "-?") == 0)
   {
      printf("testbitsout val bits\n");
      printf("    Test harness for bitsout()\n");
      printf("    Val is the test value in hex or decimal\n");
      printf("    Bits is the number of bits to be output,\n");
      printf("    from the lsb end of the value, i.e., if\n");
      printf("    bits is 8 and a 16 bit value is supplied then\n");
      printf("    to lowest valued 8 bits are output, msb first.\n");
      return 0;
   }

   if (argc != 3)
   {
      error("testbitsout", "must supply two arguments", "");
   }

   arg = argv[1];
   if (strstr(arg, "0x") == arg ||
       strstr(arg, "0X") == arg)
   {
      sscanf(arg, "%x", &uval);
      val = uval;
   }
   else
   {
      sscanf(arg, "%d", &val);
   }

   sscanf(argv[2], "%d", &bits);

   printf("Outputting %d bits from %d (0X%04X)\n", bits, val, val);
   result = bitsout(val, bits);
   printf("Result is [%s]\n", result);   
}
