unix2dos跟dos2unix文件转换的实现

unix2dos和dos2unix文件转换的实现
typedef struct link_node
{
    char data;
    struct link_node *next;
    //long length;
}LINKNODE,*linklist;
void linklist_traverse_from_head(linklist head)
{
#if 0
    linklist p = head;
    long i = 0L;
    long k = 0L;
    for(p = p->next; NULL != p; p = p->next)
    {
        if(0 == i%32)
        {
            i = 0;
            printf("\n");
        }
        printf("%2x ",p->data);
        printf("#%ld ",k);
        k++;
        i++;
    }
#endif
}
/*save the buffer in the reversed order to list,
*firstly insert new point after the aim,so turn them*/
linklist creat_buff2list_from_head(char *buffer,long len)
{//将BUFF中的文件字符集创建为带头结点的链表
    long list_size = 0L;
    linklist head;
    LINKNODE *p;
    head = (LINKNODE *)malloc(sizeof(LINKNODE));
    head->data = 0x0;
    head->next = NULL; //初始化头结点

    for (list_size = 0; list_size < len; list_size++)
    {
        p = (LINKNODE *)malloc(sizeof(LINKNODE)); //根据BUFF长度开始创建链表
        memcpy(&(p->data), (buffer + list_size), sizeof(char));
        //p->data = buffer[list_size];

        p->next = head->next;
        head->next = p;
    }
    //diag_printf("\nlist_size=%d,len=%d,L=%d\n",list_size,len,__LINE__);
    return head;
}
void creat_list2buff(linklist head,long len,char *buffer)
{/*you must malloc the buffer of len length first,and free them after all*/
    long list_size = 0L;
    linklist p = head->next;

    for(list_size = (len-1); list_size >= 0; list_size--)
    {
        if(NULL != p)
        {
            memcpy((buffer + list_size), &(p->data), sizeof(char));
            //buffer[list_size] = p->data;
            p = p->next;
        }
    }
}
void destroy_linklist(linklist head)
{/*!!!OK,check the buffer size twice after delete&insert*/
    linklist p = NULL;

    while(head != NULL)
    {
        p = head->next;

        free(head);
        head = p;
    }
}
void linklist_insert_node_back_full(linklist head,char aim,char insert,long *len)
{
    linklist p = NULL;
    p = head->next;
    int i = 0;
    while(p != NULL)
    {
        if(p->data == aim)
        {
            linklist newp = NULL;
            newp = (LINKNODE *)malloc(sizeof(LINKNODE));
            newp->data = insert;
            newp->next = p->next;
            p->next = newp;
            (*len)++;
            //diag_printf("\np=%2x,n=%2x,len=%ld, L=%d,i=%d\n",p->data,newp->data,*len,__LINE__,i);
        }
        p = p->next;
        i++;
    }
}
void linklist_delete_node_full(linklist head,char value,long *len)
{
    linklist p = NULL, q = head;
    p = head->next;
    while(p != NULL)
    {
        //if(strcmp(p->data,value)==0)
        if(value == p->data)
        {
            q->next = p->next;
            free(p);
            (*len)++;
            p = q->next;
        }
        else
        {
            q = p;
            p = p->next;
        }
    }
    //if(strcmp(head->data,value)==0)
    if(value == head->data)
    {
        q = head;
        head = head->next;
        free(q);
        (*len)++;
    }
}
/*UNIX '\n'=0x0a;WINDOWS(DOS) '\r\n'=0x0d+0x0a*/
int file_unix2dos(char* buffer, int *len)
{
    int old_len = *len;
    int new_len = 0;
    char *buf_1;
    buf_1 = (char *)malloc(old_len);
    if(NULL==buf_1)
    {
        diag_printf("malloc error\n");
        return(-1);
    }
    memset(buf_1, 0 , old_len);
    memcpy(buf_1, buffer, old_len);
    long count = 0L;

    linklist head = NULL;
    head = creat_buff2list_from_head(buf_1,old_len);
    free(buf_1);

    //find all
    linklist p = head;
    linklist_insert_node_back_full(p,'\n','\r',&count);
    new_len = old_len + count;

    char *buf_2;
    buf_2 = (char *)malloc(new_len);//realloc
    if(NULL==buf_2)
    {
        diag_printf("malloc error\n");
        return(-1);
    }
    memset(buf_2, 0 , new_len);

    creat_list2buff(head, new_len, buf_2);

    memset(buffer, 0 , old_len);
    *len = new_len;
    memcpy(buffer, buf_2, new_len);
    //linklist_traverse_from_head(head);
    //diag_printf("\nL=%d, len=%d, c=%ld\n\n",__LINE__,new_len,count);
    destroy_linklist(head);
    free(buf_2);
    return 0;
}
int file_dos2unix(char* buffer, int *len)
{
    int old_len = *len;
    int new_len = 0;
    char *buf_1;
    buf_1 = (char *)malloc(old_len);
    if(NULL==buf_1)
    {
        diag_printf("malloc error\n");
        return(-1);
    }
    memset(buf_1, 0 , old_len);
    memcpy(buf_1, buffer, old_len);
    long count = 0L;
    linklist head = NULL;
    head = creat_buff2list_from_head(buf_1,old_len);
    free(buf_1);

    //find all
    linklist p = head;
    linklist_delete_node_full(p,'\r',&count);
    new_len = old_len - count;

    char *buf_2;
    buf_2 = (char *)malloc(new_len);
    if(NULL==buf_2)
    {
        diag_printf("malloc error\n");
        return(-1);
    }
    memset(buf_2, 0 , new_len);
    creat_list2buff(head, new_len, buf_2);

    memset(buffer, 0 , old_len);
    *len = new_len;
    memcpy(buffer, buf_2, new_len);
    //linklist_traverse_from_head(head);
    //diag_printf("\nL=%d, len=%d, c=%ld\n\n",__LINE__,new_len,count);
    destroy_linklist(head);
    free(buf_2);
    return 0;
}


/* !!!Notes:注意将UNIX格式文件转化为DOS格式文件,BUFF长度 会增加*/