/* * testField.c - simple driver to test methods of field.h. * * "Copyright (c) 2013-15 by Fritz Sieker." * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose, without fee, and without written * agreement is hereby granted, provided that the above copyright notice * and the following two paragraphs appear in all copies of this software. * * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHOR * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" * BASIS, AND THE AUTHOR NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, * UPDATES, ENHANCEMENTS, OR MODIFICATIONS." */ #include #include #include #include "field.h" /** @mainpage cs270 Programming Assignment PAx - Bit Fields in C * \htmlinclude "FIELD.html" */ /** @file: testField.c * @brief Driver to test functions of field.c (do not modify) * * @details This is a driver program to test the functions * defined in field.h and implemented in field.c. The program takes one * or more command line parameters and calls one of the methods, then * prints the results. To see how to use the program, execute * testField in a terminal window. This will print a usage * statement defining how to run the program. The first parameter of the * program is always a key defining which function to run. The * options are: *
    *
  • bin print the next parameter in decimal, hex and binary
  • *
  • fits check if value will fit in a field
  • *
  • get get a field from a value
  • *
  • getB get a bit from a value
  • *
  • set set a field in a value
  • *
  • setB set a bit in a value
  • *
*

* A sample execution might be: testField get 0xABCD 9 4 0 *

* which prints

dec: 60  hex: 0x3C  bin: 0000-0000-0000-0000-0000-0000-0011-1100
*

* All values may be entered as signed decimal numbers or as hex values * by beginning it with 0x, or as binary numbers by beginning with * 0b. *

* @author Fritz Sieker */ /** Print the binary representation of a value starting at the specified * bit position. A separator is printed every 4 bits for easy reading. * @param value the value to be printed * @param msb the bit position to begin printing (31 to 0) */ void printBinaryMSB (int value, int msb) { while (msb >= 0) { putchar(((value & (1 << msb)) ? '1' : '0')); if (msb && ((msb & 0x3) == 0)) putchar('-'); msb--; } } /** Print a 32 bit binary representation of a value. * @param value the value to be printed */ void printBinary (int value) { printBinaryMSB(value, 31); } /** Print a usage statement, then exit the program returning a non zero * value, the Linux convention indicating an error */ static void usage() { puts("Usage: testField bin value"); puts(" testField fits value width isSigned"); puts(" testField get value hi lo isSigned"); puts(" testField getB value index"); puts(" testField set value hi lo newValue"); puts(" testField setB value index newValue"); exit(1); } /** print the value in decimal, hex and binary. * @param result the value to be printed. */ static void printResult (int result) { printf("dec: %d hex: 0x%X bin: ", result, result); printBinary(result); } /** Get int value from argument, allowing binary input like 0b0111001 * @param arg string containing value (decimal, octal, hex, binary) * @return value */ static int getArg (char* arg) { char* junk; int value = (int) strtol(arg, &junk, 0); if ((*junk == 'B') || (*junk == 'b')) { /* user entered binary value */ value = (int) strtol(junk + 1, &junk, 2); /* skip over 'B/b' */ if (arg[0] == '-') { value = -value; } } if (*junk) { printf("ERROR - bad format: %s\n", arg); } return value; } /** Entry point of the program * @param argc count of arguments, will always be at least 1 * @param argv array of parameters to program argv[0] is the name of * the program, so additional parameters will begin at index 1. * @return 0 the Linux convention for success. */ int main (int argc, char* argv[]) { int value, hi, lo, isSigned, width; if (argc < 3) usage(); char* op = argv[1]; if ((strcmp(op, "bin") == 0) && (argc == 3)) { printResult(getArg(argv[2])); } else if ((strcmp(op, "fits") == 0) && (argc == 5)) { value = getArg(argv[2]); width = getArg(argv[3]); isSigned = getArg(argv[4]); printf("%s", (fieldFits(value, width, isSigned) ? "true" : "false")); } else if ((strcmp(op, "get") == 0) && (argc == 6)) { value = getArg(argv[2]); hi = getArg(argv[3]); lo = getArg(argv[4]); isSigned = getArg(argv[5]); printResult(getField(value, hi, lo, isSigned)); } else if ((strcmp(op, "getB") == 0) && (argc == 4)) { value = getArg(argv[2]); hi = getArg(argv[3]); printf("%d\n", getBit(value, hi)); } else if ((strcmp(op, "set") == 0) && (argc == 6)) { value = getArg(argv[2]); hi = getArg(argv[3]); lo = getArg(argv[4]); printResult(setField(value, hi, lo, getArg(argv[5]))); } else if ((strcmp(op, "setB") == 0) && (argc == 5)) { value = getArg(argv[2]); hi = getArg(argv[3]); printResult(setBit(value, hi, getArg(argv[4]))); } else usage(); printf("\n"); return 0; }