Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1327175
  • 博文数量: 268
  • 博客积分: 10698
  • 博客等级: 上将
  • 技术积分: 2867
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-14 22:21
文章分类

全部博文(268)

文章存档

2012年(19)

2011年(13)

2010年(29)

2009年(26)

2008年(99)

2007年(82)

我的朋友

分类: C/C++

2008-11-11 02:19:20

这是对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




阅读(1286) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~