separate out rot13(1)
This commit is contained in:
-41
@@ -1,41 +0,0 @@
|
||||
#include <ctype.h> /* isalpha(3), isupper(3) */
|
||||
#include <stdio.h> /* getc(3), putc(3) */
|
||||
#include <stddef.h> /* stdin, stdout, EOF */
|
||||
#include <string.h> /* strchr(3), strlen(3) */
|
||||
#include <sysexits.h> /* EX_OK, EX_USAGE */
|
||||
#include <unistd.h> /* write(2) */
|
||||
#include "libio.h" /* fdputs(3) */
|
||||
#include "usefulmacros.h" /* ARRAYLEN */
|
||||
|
||||
static char *program_name = "rot13";
|
||||
|
||||
/* Assumes this is a character in the provided glyph system.
|
||||
* Probably not Unicode-capable but doesn't assume Alphabet.
|
||||
* Returns `c` rotated around `a` by `r` characters. */
|
||||
static int
|
||||
caesar(char c, int r, const char *a){
|
||||
return a[(strchr(a, c) - a + r) % strlen(a)];
|
||||
}
|
||||
|
||||
/* It's faster to assume Alphabet. */
|
||||
static char alpha_upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLM";
|
||||
static char alpha_lower[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklm";
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
char *a;
|
||||
int c;
|
||||
|
||||
if(argc > 1){
|
||||
write(2, "Usage: ", 7);
|
||||
fdputs(2, argv[0] == NULL ? program_name : argv[0]);
|
||||
return EX_USAGE;
|
||||
}
|
||||
|
||||
while((c = getc(stdin)) != EOF){
|
||||
if(isalpha(c)){
|
||||
a = isupper(c) ? alpha_upper : alpha_lower;
|
||||
putc(a[c - a[0] + 13], stdout);
|
||||
}else
|
||||
putc(c, stdout);
|
||||
}
|
||||
}
|
||||
@@ -1,113 +0,0 @@
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "arraylen.h"
|
||||
#include "bool.h"
|
||||
|
||||
#define TAGSTART '<'
|
||||
#define TAGEND '>'
|
||||
#define ETAG '/'
|
||||
|
||||
#define LF '\n'
|
||||
|
||||
/*
|
||||
start-tag end-tag
|
||||
| |
|
||||
V V
|
||||
<TAG>CONTENT</TAG>
|
||||
^ ^ ^
|
||||
| | |
|
||||
| TAGEND ETAG
|
||||
|
|
||||
TAGSTART
|
||||
|
||||
empty-element tag
|
||||
|
|
||||
| optional whitespace
|
||||
| |
|
||||
| | TAGEND
|
||||
| | |
|
||||
V V V
|
||||
<TAG />
|
||||
^ ^
|
||||
| |
|
||||
| ETAG
|
||||
|
|
||||
TAGSTART
|
||||
|
||||
*/
|
||||
|
||||
/* a lot of this program's design is weird and kind of overengineered.
|
||||
this is to drastically limit the memory used. */
|
||||
|
||||
|
||||
enum state{
|
||||
error = -1, /* for future use */
|
||||
init = 0, /* waiting for a root tag */
|
||||
starttagreading,
|
||||
tagreading,
|
||||
eetagreading, /* throw away everything until state change */
|
||||
endtagreading,
|
||||
contentreading /* printing */
|
||||
};
|
||||
|
||||
void
|
||||
usage(char *name){
|
||||
fprintf(stdout, "Usage: %s [tag...]\n", name);
|
||||
return;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
if(argc < 2){
|
||||
usage(argv[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool enable = 0;
|
||||
char c;
|
||||
int linenumber = 0;
|
||||
enum state s = init;
|
||||
FILE *input = stdin;
|
||||
FILE *output = stdout;
|
||||
int level = 1; /* 1-indexed because args start at 1 */
|
||||
size_t p = 0;
|
||||
|
||||
while(c = getc(input) != EOF){
|
||||
if(c == LF){
|
||||
++linenumber;
|
||||
continue;
|
||||
}
|
||||
switch(s){
|
||||
case init:
|
||||
if(c == TAGSTART)
|
||||
s = starttagreading;
|
||||
break;
|
||||
case starttagreading:
|
||||
if(c == ETAG)
|
||||
s = endtagreading;
|
||||
else if(!isblank(c))
|
||||
s = tagreading;
|
||||
break;
|
||||
case tagreading:
|
||||
if(c == ETAG)
|
||||
s = eetagreading;
|
||||
break;
|
||||
/* case eetagreading: */
|
||||
case endtagreading:
|
||||
if(c == ETAG)
|
||||
fprintf(stderr, "%s:%d: Stray \'%c\' in an end tag?\n", argv[0], linenumber, ETAG);
|
||||
break;
|
||||
case contentreading:
|
||||
if(enable)
|
||||
putc(c, output);
|
||||
else if(c == TAGSTART)
|
||||
s = starttagreading;
|
||||
else if(c == TAGEND)
|
||||
fprintf(stderr, "%s:%d: Possible state error or XML syntax error; \'%c\' in mode contentreading\n", argv[0], linenumber, TAGEND);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user