1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
/* jpnevulator - serial reader/writer
* Copyright (C) 2006-2014 Freddy Spierenburg
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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 GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#ifndef __USE_ISOC99
#define __USE_ISOC99 /* for newly introduced isblank() */
#endif
#include <ctype.h>
#include "byte.h"
/* Convert an ASCII character into a binary form. */
static char nibbleGet(char c) {
return(isdigit(c)?c-'0':toupper(c)-'A'+0xA);
}
enum byteState {
byteStateComplete=0,
byteStateNibbleFirst,
byteStateHexNotation,
byteStateNibbleSecond,
byteStateError
};
int byteGet(FILE *fd) {
int byte;
enum byteState state;
/* No real reason to do this, but it makes the compiler happy. If this next line is not
here the compiler generates this warning below:
byte.c:76:16: warning: ‘byte’ may be used uninitialized in this function [-Wuninitialized]
So let's initialize byte anyway.
*/
byte=0;
for(state=byteStateNibbleFirst;state!=byteStateComplete;) {
static int character;
switch(state) {
case byteStateNibbleFirst: {
character=fgetc(fd);
if(isxdigit(character)) {
byte=nibbleGet(character);
if(character=='0') {
state=byteStateHexNotation;
} else {
state=byteStateNibbleSecond;
}
} else if(!isblank(character)) {
state=byteStateError;
}
break;
}
case byteStateHexNotation: {
character=fgetc(fd);
if(tolower(character)=='x') {
state=byteStateNibbleFirst;
} else {
if(ungetc(character,fd)!=EOF) {
state=byteStateNibbleSecond;
} else {
state=byteStateError;
}
}
break;
}
case byteStateNibbleSecond: {
character=fgetc(fd);
if(isxdigit(character)) {
byte=(byte<<4)+nibbleGet(character);
state=byteStateComplete;
} else {
state=byteStateError;
}
break;
}
case byteStateError: {
switch(character) {
case EOF: {
byte=byteRtrnEOF;
break;
}
case '\n': {
byte=byteRtrnEOL;
break;
}
default: {
byte=byteRtrnUnknown;
break;
}
}
state=byteStateComplete;
break;
}
case byteStateComplete: {
/* Do nothing, since it's impossible to reach this stage. Just
* make the compiler happy. */
break;
}
}
}
return(byte);
}
|