As we all know there is a
seq (seq.exe for windows), but we can do more with sseq.
main.h
fc.c
process_alpha.c
process_num.c
process_stdin.c
str_util.c
main.c
main.h
#include
<
stdio.h
>
#include < stdlib.h >
#define MAX 9
#define LINE_LEN 1024
#define WORD_LEN 80
#define BLOCK_LEN 25535
#define ELEMENT_MAX 20
#define CR ' '
#define LF ' '
#define EOS ' '
#include < stdlib.h >
#define MAX 9
#define LINE_LEN 1024
#define WORD_LEN 80
#define BLOCK_LEN 25535
#define ELEMENT_MAX 20
#define CR ' '
#define LF ' '
#define EOS ' '
fc.c
#include
"
main.h
"
/**
* reads a line from input file to parameter line,
* returns char read count, -1 for EOF
*/
int fread_line(FILE * fi, char * line)
{
int bc = 0;
line[bc] = EOS;
int flag;
flag = -1;
int ch;
while ( (ch = fgetc(fi)) != EOF)
{
if (ch == CR || ch == LF)
{
// not EOF
flag = 0;
break;
}
line[bc++] = (char) ch;
}
line[bc] = EOS;
if (bc > 0) return bc;
else return flag;
}
/**
* read a string of word from given file,
* returns the length of the word read in
*/
int fread_word(FILE * fi, char * word)
{
int count = 0;
int ch;
while ((ch = fgetc(fi)) != EOF)
{
if (isalnum(ch))
{
word[count ++] = (char) ch;
}
else
{
break;
}
}
word[count] = EOS;
return count;
}
/**
* reads a line from input file to parameter line,
* returns char read count, -1 for EOF
*/
int fread_line(FILE * fi, char * line)
{
int bc = 0;
line[bc] = EOS;
int flag;
flag = -1;
int ch;
while ( (ch = fgetc(fi)) != EOF)
{
if (ch == CR || ch == LF)
{
// not EOF
flag = 0;
break;
}
line[bc++] = (char) ch;
}
line[bc] = EOS;
if (bc > 0) return bc;
else return flag;
}
/**
* read a string of word from given file,
* returns the length of the word read in
*/
int fread_word(FILE * fi, char * word)
{
int count = 0;
int ch;
while ((ch = fgetc(fi)) != EOF)
{
if (isalnum(ch))
{
word[count ++] = (char) ch;
}
else
{
break;
}
}
word[count] = EOS;
return count;
}
process_alpha.c
#include
"
main.h
"
char * map = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ " ;
int map_len = 52 ;
int start = 0 ;
int end = 0 ;
int len = 0 ;
int init_range( char * a, char * b)
{
int i;
for(i = 0; i < map_len; i ++)
{
if (map[i] == a[0])
{
start = i;
}
if (map[i] == b[0])
{
end = i;
}
}
if (start < end)
{
len = end - start + 1;
}
else
{
len = start - end + 1;
}
}
int process_alpha( char * raw, int ps, int pm, int pe)
{
conlog("process alpha()");
int cursor = 0;
char pre[LINE_LEN];
char sub[LINE_LEN];
substr (pre, raw, 0, ps);
substrr(sub, raw, pe + 1);
char sa[MAX], sb[MAX];
substr(sa, raw, ps + 1, pm);
substr(sb, raw, pm + 1, pe);
init_range(sa, sb);
//printf("%s~%s ", sa, sb);
char buf[LINE_LEN];
int i;
char sinc [WORD_LEN];
cursor = start;
if (start < end)
{
conlog("ascending");
for(i = 0; i < len; i ++)
{
sprintf(sinc, "%c", map[cursor++]);
if (cursor > end) cursor = start;
strcpy(buf, pre);
strcat(buf, sinc);
strcat(buf, sub);
process_alnum(buf);
}
}
else
{
conlog("descending");
for(i = 0; i < len; i ++)
{
sprintf(sinc, "%c", map[cursor--]);
if (cursor < end) cursor = start;
strcpy(buf, pre);
strcat(buf, sinc);
strcat(buf, sub);
process_alnum(buf);
}
}
return 1;
}
char * map = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ " ;
int map_len = 52 ;
int start = 0 ;
int end = 0 ;
int len = 0 ;
int init_range( char * a, char * b)
{
int i;
for(i = 0; i < map_len; i ++)
{
if (map[i] == a[0])
{
start = i;
}
if (map[i] == b[0])
{
end = i;
}
}
if (start < end)
{
len = end - start + 1;
}
else
{
len = start - end + 1;
}
}
int process_alpha( char * raw, int ps, int pm, int pe)
{
conlog("process alpha()");
int cursor = 0;
char pre[LINE_LEN];
char sub[LINE_LEN];
substr (pre, raw, 0, ps);
substrr(sub, raw, pe + 1);
char sa[MAX], sb[MAX];
substr(sa, raw, ps + 1, pm);
substr(sb, raw, pm + 1, pe);
init_range(sa, sb);
//printf("%s~%s ", sa, sb);
char buf[LINE_LEN];
int i;
char sinc [WORD_LEN];
cursor = start;
if (start < end)
{
conlog("ascending");
for(i = 0; i < len; i ++)
{
sprintf(sinc, "%c", map[cursor++]);
if (cursor > end) cursor = start;
strcpy(buf, pre);
strcat(buf, sinc);
strcat(buf, sub);
process_alnum(buf);
}
}
else
{
conlog("descending");
for(i = 0; i < len; i ++)
{
sprintf(sinc, "%c", map[cursor--]);
if (cursor < end) cursor = start;
strcpy(buf, pre);
strcat(buf, sinc);
strcat(buf, sub);
process_alnum(buf);
}
}
return 1;
}
process_num.c
#include
"
main.h
"
int process_num( char * raw, int ps, int pm, int pe)
{
conlog("process num()");
char pre[LINE_LEN];
char sub[LINE_LEN];
substr(pre, raw, 0, ps);
substrr(sub, raw, pe + 1);
int width;
char sa[MAX], sb[MAX];
int a,b;
substr(sa, raw, ps + 1, pm);
a = atoi(sa);
substr(sb, raw, pm + 1, pe);
b=atoi(sb);
if (a < b)
{
width = pm - ps - 1;
}
else
{
width = pe - pm - 1;
}
int i;
char buf[LINE_LEN];
char sint[MAX];
if (a < b)
{
conlog("ascending");
for(i = a; i <= b; i ++)
{
strcpy(buf, pre);
int2str(i, sint, width);
strcat(buf, sint);
strcat(buf, sub);
process_alnum(buf);
}
}
else
{
conlog("descending");
for(i = a; i >= b; i --)
{
strcpy(buf, pre);
int2str(i, sint, width);
strcat(buf, sint);
strcat(buf, sub);
process_alnum(buf);
}
}
}
int process_num( char * raw, int ps, int pm, int pe)
{
conlog("process num()");
char pre[LINE_LEN];
char sub[LINE_LEN];
substr(pre, raw, 0, ps);
substrr(sub, raw, pe + 1);
int width;
char sa[MAX], sb[MAX];
int a,b;
substr(sa, raw, ps + 1, pm);
a = atoi(sa);
substr(sb, raw, pm + 1, pe);
b=atoi(sb);
if (a < b)
{
width = pm - ps - 1;
}
else
{
width = pe - pm - 1;
}
int i;
char buf[LINE_LEN];
char sint[MAX];
if (a < b)
{
conlog("ascending");
for(i = a; i <= b; i ++)
{
strcpy(buf, pre);
int2str(i, sint, width);
strcat(buf, sint);
strcat(buf, sub);
process_alnum(buf);
}
}
else
{
conlog("descending");
for(i = a; i >= b; i --)
{
strcpy(buf, pre);
int2str(i, sint, width);
strcat(buf, sint);
strcat(buf, sub);
process_alnum(buf);
}
}
}
process_stdin.c
#include
"
main.h
"
int process_stdin( char * raw)
{
conlog("process stdin()");
//printf("raw=%s ", raw);
char os[LINE_LEN];
char line[LINE_LEN];
FILE * f;
f = stdin;
int c = 0;
while (fread_line (f, line) != -1)
{
to_now(s_now);
cleanstr(os);
c ++;
replace_all(os, raw, "${1}", line);
print_out(os);
}
return 1;
}
int process_stdin( char * raw)
{
conlog("process stdin()");
//printf("raw=%s ", raw);
char os[LINE_LEN];
char line[LINE_LEN];
FILE * f;
f = stdin;
int c = 0;
while (fread_line (f, line) != -1)
{
to_now(s_now);
cleanstr(os);
c ++;
replace_all(os, raw, "${1}", line);
print_out(os);
}
return 1;
}
str_util.c
#include
"
main.h
"
int conlog( char * s)
{
//printf("[%s] ", s);
return 0;
}
int cleanstr( char * s)
{
int len = strlen(s);
int i;
for(i = 0; i < len; i ++)
{
s[i] = EOS;
}
return len;
}
/**
* re define string to given length
*/
int str_redim( char * s, int size)
{
int len = strlen (s);
return size - len;
}
void int2str( int raw, char oc[], int length)
{
switch(length)
{
case 2:
sprintf(oc, "%02d", raw);
break;
case 3:
sprintf(oc, "%03d", raw);
break;
case 4:
sprintf(oc, "%04d", raw);
break;
case 5:
sprintf(oc, "%05d", raw);
break;
case 6:
sprintf(oc, "%06d", raw);
break;
case 7:
sprintf(oc, "%07d", raw);
break;
default:
sprintf(oc, "%d", raw);
}
}
/*
* sub string from the start point of raw, store into oc
*/
void substrr( char oc[], char raw[], int start)
{
int length = strlen(raw);
if (start >= length)
{
conlog("substrr()::start point >= raw length");
oc[0] = '';
}
int i;
int counter = 0;
for( i = start; i < length; i ++)
{
oc[counter++] = raw[i];
}
oc [ counter ] = EOS;
}
/**
* sub string from raw to oc, from start to end, not including
*/
void substr( char oc[], char raw[], int start, int end)
{
int length = strlen(raw);
if (start >= end || end > length)
{
oc[0] = '';
}
int len = end - start;
int i;
int cursor = start;
for (i = 0; i < len; i ++)
{
oc[i] = raw[cursor];
cursor ++;
if (cursor > length)
{
break;
}
}
oc[i] = EOS;
}
/**
* return first index of sample from raw
*/
int index_of( char raw[], char sample[])
{
int len = strlen (raw);
int pt = -1;
int i = 0;
int sc = 0;
int slen = strlen(sample);
for(; i < len; i ++)
{
if (raw[i] == sample[sc])
{
if (sc == 0)
{
pt = i;
}
// found match
sc ++;
}
else
{
// rewind sample
sc = 0;
}
//check compare end
if (sc == slen)
{
// compare end
break;
}
}
return pt;
}
/**
* return whether the raw is end with ext
*/
int ends_with( char raw[], char ext[])
{
int len = strlen(raw);
int slen = strlen(ext);
int i = 0;
int oi = -1;
for(; i < slen; i ++)
{
char ch1 = raw[len - i - 1];
char ch2 = ext[slen - i - 1];
if (ch1 == ch2)
{
//printf("%c ? %c ",ch1, ch2);
oi = 1;
}
else
{
oi = -1;
break;
}
}
return oi;
}
/**
* compare two string
*/
int equals( char raw[], char sample[])
{
int len = strlen(raw);
int slen = strlen (sample);
if (slen != len)
{
return 0;
}
int i = 0;
for (; i < len; i ++)
{
if (raw[i] != sample[i])
{
return -1;
}
}
}
/**
* fill the oc with raw replace the find with replacement
*/
int replace_first( char oc[], char raw[], char find[], char replacement[])
{
//conlog("replace_first()");
int raw_len = strlen(raw);
int find_len = strlen(find);
int rep_len = strlen (replacement);
int oc_len = raw_len - find_len + rep_len;
int rep_count = 0;
int pt;
pt = index_of (raw, find);
//printf("found=%d len=%d ", pt, find_len);
char pre[LINE_LEN];
char suf [LINE_LEN];
if (pt >= 0)
{
substr (pre, raw, 0, pt);
substrr(suf, raw, pt + find_len);
//printf("pre=%s, suf=%s| ", pre, suf);
strcpy(oc, pre);
strcat(oc, replacement);
strcat(oc, suf);
rep_count ++;
}
//printf("FIRST %s (%s -> %s) %s ", raw, find, replacement, oc);
return rep_count;
}
/**
* fill oc with raw repalce the find with replacement
*/
int replace_all( char oc[], char raw[], char find[], char replacement[])
{
int rep_count = 0;
char oc0[LINE_LEN];
cleanstr(oc0);
cleanstr(oc);
strcpy(oc0, raw);
while (replace_first(oc, oc0, find, replacement) > 0)
{
rep_count ++;
cleanstr(oc0);
strcpy(oc0, oc);
}
//printf("ALL %s (%s->%s) %s ", raw, find, replacement, oc);
return rep_count;
}
int conlog( char * s)
{
//printf("[%s] ", s);
return 0;
}
int cleanstr( char * s)
{
int len = strlen(s);
int i;
for(i = 0; i < len; i ++)
{
s[i] = EOS;
}
return len;
}
/**
* re define string to given length
*/
int str_redim( char * s, int size)
{
int len = strlen (s);
return size - len;
}
void int2str( int raw, char oc[], int length)
{
switch(length)
{
case 2:
sprintf(oc, "%02d", raw);
break;
case 3:
sprintf(oc, "%03d", raw);
break;
case 4:
sprintf(oc, "%04d", raw);
break;
case 5:
sprintf(oc, "%05d", raw);
break;
case 6:
sprintf(oc, "%06d", raw);
break;
case 7:
sprintf(oc, "%07d", raw);
break;
default:
sprintf(oc, "%d", raw);
}
}
/*
* sub string from the start point of raw, store into oc
*/
void substrr( char oc[], char raw[], int start)
{
int length = strlen(raw);
if (start >= length)
{
conlog("substrr()::start point >= raw length");
oc[0] = '';
}
int i;
int counter = 0;
for( i = start; i < length; i ++)
{
oc[counter++] = raw[i];
}
oc [ counter ] = EOS;
}
/**
* sub string from raw to oc, from start to end, not including
*/
void substr( char oc[], char raw[], int start, int end)
{
int length = strlen(raw);
if (start >= end || end > length)
{
oc[0] = '';
}
int len = end - start;
int i;
int cursor = start;
for (i = 0; i < len; i ++)
{
oc[i] = raw[cursor];
cursor ++;
if (cursor > length)
{
break;
}
}
oc[i] = EOS;
}
/**
* return first index of sample from raw
*/
int index_of( char raw[], char sample[])
{
int len = strlen (raw);
int pt = -1;
int i = 0;
int sc = 0;
int slen = strlen(sample);
for(; i < len; i ++)
{
if (raw[i] == sample[sc])
{
if (sc == 0)
{
pt = i;
}
// found match
sc ++;
}
else
{
// rewind sample
sc = 0;
}
//check compare end
if (sc == slen)
{
// compare end
break;
}
}
return pt;
}
/**
* return whether the raw is end with ext
*/
int ends_with( char raw[], char ext[])
{
int len = strlen(raw);
int slen = strlen(ext);
int i = 0;
int oi = -1;
for(; i < slen; i ++)
{
char ch1 = raw[len - i - 1];
char ch2 = ext[slen - i - 1];
if (ch1 == ch2)
{
//printf("%c ? %c ",ch1, ch2);
oi = 1;
}
else
{
oi = -1;
break;
}
}
return oi;
}
/**
* compare two string
*/
int equals( char raw[], char sample[])
{
int len = strlen(raw);
int slen = strlen (sample);
if (slen != len)
{
return 0;
}
int i = 0;
for (; i < len; i ++)
{
if (raw[i] != sample[i])
{
return -1;
}
}
}
/**
* fill the oc with raw replace the find with replacement
*/
int replace_first( char oc[], char raw[], char find[], char replacement[])
{
//conlog("replace_first()");
int raw_len = strlen(raw);
int find_len = strlen(find);
int rep_len = strlen (replacement);
int oc_len = raw_len - find_len + rep_len;
int rep_count = 0;
int pt;
pt = index_of (raw, find);
//printf("found=%d len=%d ", pt, find_len);
char pre[LINE_LEN];
char suf [LINE_LEN];
if (pt >= 0)
{
substr (pre, raw, 0, pt);
substrr(suf, raw, pt + find_len);
//printf("pre=%s, suf=%s| ", pre, suf);
strcpy(oc, pre);
strcat(oc, replacement);
strcat(oc, suf);
rep_count ++;
}
//printf("FIRST %s (%s -> %s) %s ", raw, find, replacement, oc);
return rep_count;
}
/**
* fill oc with raw repalce the find with replacement
*/
int replace_all( char oc[], char raw[], char find[], char replacement[])
{
int rep_count = 0;
char oc0[LINE_LEN];
cleanstr(oc0);
cleanstr(oc);
strcpy(oc0, raw);
while (replace_first(oc, oc0, find, replacement) > 0)
{
rep_count ++;
cleanstr(oc0);
strcpy(oc0, oc);
}
//printf("ALL %s (%s->%s) %s ", raw, find, replacement, oc);
return rep_count;
}
main.c
#include
"
main.h
"
int print_count;
int process_alnum( char * raw)
{
int ps = index_of(raw, "[", 0);
int pm = index_of(raw, "-", ps + 1);
int pe = index_of(raw, "]", pm + 1);
if (ps >= 0 && pm > ps && pe > pm)
{
if (isdigit(raw[ps + 1]))
return process_num(raw, ps, pm, pe);
else if (isalpha(raw[ps + 1]))
return process_alpha(raw, ps, pm, pe);
else
print_out(raw);
}
else
print_out(raw);
return 1;
}
int print_out( char * s)
{
printf("%s ", s);
return 1;
}
int process( int argc, char * argv[])
{
if (index_of(argv[1], "[", 0) >= 0)
{
process_alnum(argv[1]);
}
else if (index_of(argv[1], "${1}") >= 0)
{
return process_stdin(argv[1]);
}
else
{
// unknow
printf("%s ", argv[1]);
}
}
int main( int argc, char * argv[])
{
print_count = 0;
if (argc < 2)
{
printf("usage: sseq string[01-09]... sseq abc${1} stdin sseq str[a-z] sseq str[10-01] ");
printf("${I} loop counter 1 2 3 ... ");
printf("${i} loop counter 0 1 2 ... ");
printf("${TODAY}, ${NOW} ... ");
return 0;
}
//printf("#process() %s ", raw);
process(argc, argv);
//system("PAUSE");
return 0;
}
int to_today( char * s)
{
struct tm * ptm;
time_t timer;
timer = time(NULL);
ptm = localtime(&timer);
//strftime(s, LINE_LEN + 1, "%m/%d/%Y", ptm);
strftime(s, LINE_LEN + 1, "%Y%m%d", ptm);
return 0;
}
int to_now( char * s)
{
struct tm * ptm;
time_t timer;
timer = time(NULL);
ptm = localtime(&timer);
//strftime(s, LINE_LEN + 1, "%m/%d/%Y %H:%M:%S", ptm);
strftime(s, LINE_LEN + 1, "%Y%m%d_%H%M%S", ptm);
return 0;
}
int print_count;
int process_alnum( char * raw)
{
int ps = index_of(raw, "[", 0);
int pm = index_of(raw, "-", ps + 1);
int pe = index_of(raw, "]", pm + 1);
if (ps >= 0 && pm > ps && pe > pm)
{
if (isdigit(raw[ps + 1]))
return process_num(raw, ps, pm, pe);
else if (isalpha(raw[ps + 1]))
return process_alpha(raw, ps, pm, pe);
else
print_out(raw);
}
else
print_out(raw);
return 1;
}
int print_out( char * s)
{
printf("%s ", s);
return 1;
}
int process( int argc, char * argv[])
{
if (index_of(argv[1], "[", 0) >= 0)
{
process_alnum(argv[1]);
}
else if (index_of(argv[1], "${1}") >= 0)
{
return process_stdin(argv[1]);
}
else
{
// unknow
printf("%s ", argv[1]);
}
}
int main( int argc, char * argv[])
{
print_count = 0;
if (argc < 2)
{
printf("usage: sseq string[01-09]... sseq abc${1} stdin sseq str[a-z] sseq str[10-01] ");
printf("${I} loop counter 1 2 3 ... ");
printf("${i} loop counter 0 1 2 ... ");
printf("${TODAY}, ${NOW} ... ");
return 0;
}
//printf("#process() %s ", raw);
process(argc, argv);
//system("PAUSE");
return 0;
}
int to_today( char * s)
{
struct tm * ptm;
time_t timer;
timer = time(NULL);
ptm = localtime(&timer);
//strftime(s, LINE_LEN + 1, "%m/%d/%Y", ptm);
strftime(s, LINE_LEN + 1, "%Y%m%d", ptm);
return 0;
}
int to_now( char * s)
{
struct tm * ptm;
time_t timer;
timer = time(NULL);
ptm = localtime(&timer);
//strftime(s, LINE_LEN + 1, "%m/%d/%Y %H:%M:%S", ptm);
strftime(s, LINE_LEN + 1, "%Y%m%d_%H%M%S", ptm);
return 0;
}