程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> bl2.c - 對grep的模仿

bl2.c - 對grep的模仿

編輯:關於C語言

這是對grep功能的一個簡單模仿,但不是單純的模仿,它的獨特之處在於能夠保持文本整體的視覺效果。

myst_color.c

/* myst_color.c
* Written by Myst Shen on Nov. 11, 2008.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "myst_color.h"

int c, num;
char *rcolor;
NODE * init (FILE *fp){
head = (NODE *)malloc(sizeof(NODE));
head->next = NULL;
tail = head;
while (1){
c = fgetc(fp);
if( c == EOF ) break;
p = (NODE *)malloc(sizeof(NODE));
p->val = c;
p->color = "\33[1;30m";
p->next = NULL;
tail->next = p;
p->prev = tail;
tail = p;
}
return head;
}
char * rand_color (int num) {
int num1, num2;
char *color[] = {
"\33[1;31m",
"\33[1;32m",
"\33[1;33m",
"\33[1;34m",
"\33[1;35m",
"\33[1;36m",
"\33[0;31m",
"\33[0;32m",
"\33[0;33m",
"\33[0;34m",
"\33[0;35m",
"\33[0;36m",
};
srand((unsigned)time(NULL));
num1 = rand()%11 +1;
num2 = num1 - num;
rcolor = color[num2];
return rcolor;
}
int print (NODE *pNODE) {
while(1){
pNODE = pNODE->next;
if ( pNODE == NULL ) break;
printf("%s", pNODE->color);
printf("%c", pNODE->val);
}
putchar ('\n');
return 0;
}

myst_color.h

/* myst_color.h
* Written by Myst Shen on Nov. 11, 2008.
*/

#include <stdio.h>
#include <stdlib.h>

typedef struct data {
int val;
char *color;
struct data *prev, *next;
} NODE;

NODE *head, *tail, *p;
FILE *fp;
extern NODE * init(FILE *fp);
extern char * rand_color(int num);
extern int print(NODE *p);

bl2.c

/* bl2.c
* Written by Myst Shen on Nov. 11, 2008.
*/

#include "stdlib.h"
#include "stdio.h"
#include "myst_color.h"

NODE *h, *node, *node2;
char *color1;
int main (int argc, char **argv){
char *ch, *ch2;
if (argc != 3) {
printf ("Illegal arguments.\n");
exit(0);
}
fp = fopen(argv[2],"r");
if (!fp) {
printf ("Sorry, cannot open this file !.\n");
exit(0);
}
color1 = rand_color(1);
h = init(fp);
node = h;
ch = argv[1];
while (1){
ch = argv[1];
ch2 = argv[1];
if( node == NULL ) break;
if (node->val == *ch) {
node2 = node;
while(node2->val == *ch && *ch != '\0'){
node2 = node2->next;
ch++;
}
}
if (*ch == '\0'){
while (*ch2 != '\0'){
node2 = node2->prev;
node2->color = color1;
ch2++;
}
}
node = node->next;
}
print(h);
return 0;
}

Makefile

CC=cc
CFLAGS = -g -Wall -Wextra -ansi -pedantic -O3
OBJ = myst_color.o bl2.o
bl2: $(OBJ)
$(CC) $(CFLAGS) -obl2 $(OBJ)
myst_color.o: myst_color.h myst_color.c
bl2.o: myst_color.h bl2.c
clean:
rm -f bl2 myst_color.o bl2.o
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved