Chinaunix首页 | 论坛 | 博客
  • 博客访问: 29347
  • 博文数量: 8
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2016-08-08 20:17
个人简介

一起学习

文章分类

全部博文(8)

文章存档

2017年(1)

2016年(7)

我的朋友
最近访客

分类: LINUX

2016-08-10 11:53:27

文件发送接收:

#ifndef __SRFILE_H_
#define __SRFILE_H_

#include "ulits.h"
#include "filelist.h"
#include "usrlist.h"
#include "commands.h"
#include <sys/stat.h>

extern int makeFileList(char *arg1[]);
int sendFileList(int sockfd,USERINFO *destusr,FILENODE *filelist);
#endif

#include "srfile.h"

//=========================================================================================

//函数名称:makeFileList(char *rfbuf)

//功能描述:生成发送文件链表

//函数参数:arg1[],发送文件名的指针数组

//返回值:

//=========================================================================================

int makeFileList(char *arg1[])
{
    
//char *arg[10];

    
    char filename[50];
    char filesize[50];
    char mtime[50];
    char filetype[5];
    int i=2;
    struct stat filetypebuf;
    
    initFileList();
    while(arg1[i])
    {    
        memset(filename,0,sizeof(filename));
        memset(filesize,0,sizeof(filesize));
        memset(mtime,0,sizeof(mtime));
        memset(filetype,0,sizeof(filetype));
        printf("arg1[%i]=%s\n",i,arg1[i]);
        if(lstat(arg1[i],&filetypebuf)<0)
        {
            printf("file:'%s' is err!\n",arg1[i]);    
            i++;
            continue;
        }
        
        if(S_ISREG(filetypebuf.st_mode))
        {
                sprintf(filetype,"%d",1);
        }
        else if(S_ISDIR(filetypebuf.st_mode))
        {
                sprintf(filetype,"%d",2);
        }
        else
        {
            
//filetype=;

            continue;
        }
        
        strcpy(filename,arg1[i]);
        sprintf(filesize,"%u",filetypebuf.st_size);
        sprintf(mtime,"%u",filetypebuf.st_mtime);
        printf("file[%d]=%s\n",i,filename);
        
        addFile(filename,filesize,mtime,filetype);
        i++;
    }    
    return i;
}
//============================================================================================

//函数名称:sendFileLIst(int sockfd,FILENODE *filelist)

//功能描述:发送文件链表

//函数参数:发送的sockfd对象,filelist发送的文件链表

//返 回 值:

//============================================================================================

int sendFileList(int sockfd,USERINFO *destusr,FILENODE *filelist)
{
        char sendbuf[600];
        FILENODE *tmpNode;
        int i=0;
        tmpNode=filelist;
        MSG sfMsg;
        
        while(tmpNode)
        {
            memset(sendbuf,0,sizeof(sendbuf));
            sprintf(sendbuf+1,"%d:%s:%x:%x:%x:\\a",tmpNode->fileno
                                                                                        ,tmpNode->filename
                                                                                        ,tmpNode->filesize
                                                                                        ,tmpNode->mtime
                                                                                        ,tmpNode->filetype);
//            len=len+strlen(tmpNode->fileno)+1;

//            strcpy(sendbuf+len,tmpNode->filename);

//            len=len+strlen(tmpNode->filename)+1;

//            strcpy(sendbuf+len,tmpNode->filesize);

//            len=len+strlen(tmpNode->mtime)+1;

//            strcpy(sendbuf+len,tmpNode->mtime);

//            len=len+strlen(tmpNode->filetype)+1;

//            strcpy(sendbuf+len,tmpNode->filetype);

            
            makeSendMsg(&sfMsg,sendbuf,destusr->ipaddr,IPMSG_SENDMSG|IPMSG_FILEATTACHOPT);
//

            
            sendToMsg(sockfd,&sfMsg);
            tmpNode=tmpNode->next;
            i++;
            printf("i=%d in sendfilelist\n",i);
        
        }    
        return i;
}

//====================================================================================

//    The information contained herein is the exclusive property of

//    Sunnnorth Technology Co. And shall not be distributed, reproduced,

//    or disclosed in whole in part without prior written permission.

//    (C) COPYRIGHT 2003 SUNNORTH TECHNOLOGY CO.

//    ALL RIGHTS RESERVED

//    The entire notice above must be reproduced on all authorized copies.

//====================================================================================


//=============================================================

// 文件名称:demo.h

// 功能描述:链表使用示例程序

// 维护记录:2009-05-08 V1.1 by lijian

//=============================================================

#pragma once
#ifdef __cplusplus
extern "C" {
#endif

typedef struct filenode
{
    struct filenode *next;
    unsigned int fileno;
    char *filename;
    char *filesize;
    char *mtime;
    char *filetype;
} FILENODE, *PFILENODE;


extern void initFileList(void);
int addFile(char *name, char *size,char *time,char *type);
int delFile(PFILENODE file);
int listFile(FILE *file);

PFILENODE findFileByName(char *name);
PFILENODE findFileByNum(unsigned int number);

#ifdef __cplusplus
};
#endif


//=============================================================

// 文件名称:本通用链表的快速使用方法及步骤(可用查找替换命令):

// 一、在头文件中根据自己需要增加/删除成员(除整数外,成员变量必须为指针类型)

// 二、把"filelist.h"替换成自己的头文件名

// 三、把"myFileList"替换成自己的文件列表名

// 四、在 根据自己的成员名称修改static void delFileEx()

// 五、在 根据自己的成员名称修改int addFile()

// 六、把其他成员变量修改一直

//包含文件:filelist.h lnklst.h

// 维护记录:2009-05-10 V1.2 by dengke mend some bugs!

//=============================================================

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include "lnklst.h"
#include "filelist.h"
//========================================================================================

PFILENODE myFileList = NULL;
//链表头在这里定义

static unsigned int count;
//========================================================================================

//函数名称:delFileEx(PFILENODE file)

//功能描述:释放节点所占用的堆内存空间

//函数参数:file要释放的节点指针

//返 回 值:

//===========================================================================================

static void delFileEx(PFILENODE file)
//****此函数要根据成员数目来添加/删除成员

{
    if(file->filename)
        free(file->filename);
    if(file->filesize)
        free(file->filesize);
    if(file->mtime)
        free(file->mtime);
    if(file->filetype)
        free(file->filetype);
    free(file);
}
//==========================================================================================

//函数名称:destroyFileList(void)

//功能描述:链表初始化,所有内存空间清0

//函数参数:void

//返回值:

//=============================================================================================

static void destroyFileList(void)
{
    PFILENODE tmpNod = myFileList;
    while(tmpNod)
    {
        PFILENODE tmp = tmpNod->next;
        delFileEx(tmpNod);
//清空节点所占的内存

        tmpNod = tmp;
    }
}


/***********************************以下为用户使用接口****************************************/


//=============================================================================================

//函数名称:initFileList(void)

//功能描述:调用destroyFileList来清空链表,完成初始化

//函数参数:

//返 回 值:

//============================================================================================

void initFileList(void)
{
    if(myFileList)
    {
        destroyFileList();
    }
    myFileList = NULL;
}
//=============================================================================================

//函数名称:addFile(char *name, char *host,char *ip)

//功能描述:在链表中插入一个节点

//函数参数:name、host、ip节点信息指针

//返回值:

//============================================================================================

int addFile(char *name, char *size,char *time,char *type)
//使用通用链表时此函数必须修改

{
    PFILENODE newfile = (PFILENODE)malloc(sizeof(FILENODE));
    if(newfile == NULL)
        return -ENOMEM;
        
    newfile->filename = (char *)malloc(strlen(name) + 1);
    if(newfile->filename == NULL)
    {
        free(newfile);
        return -ENOMEM;
    }
    
    newfile->filesize = (char *)malloc(strlen(size) + 1);
    if(newfile->filesize == NULL)
    {
        free(newfile->filename);
        free(newfile);
        return -ENOMEM;
    }
    
    newfile->mtime = (char *)malloc(strlen(time) + 1);
    if(newfile->mtime == NULL)
    {
        free(newfile->mtime);
        free(newfile);
        return -ENOMEM;
    }
    newfile->filetype = (char *)malloc(strlen(type) + 1);
    if(newfile->mtime == NULL)
    {
        free(newfile->filetype);
        free(newfile);
        return -ENOMEM;
    }
    
    newfile->fileno=count;

    strcpy(newfile->filename, name);
    strcpy(newfile->filesize, size);
    strcpy(newfile->mtime, time);
    strcpy(newfile->filetype,type);
    count++;
    
//pthread_mutext_lock();

    add_node((void**)&myFileList, newfile);
//*****************这里要修改*****************

    
//pthread_mutext_unlock();

    return 0;
}
//=============================================================================================

//函数名称:delFile(PFILENODE file)

//功能描述:调用函数del_node从链表中删除一个节点

//函数参数:file要从链表中删除节点

//返回值: 成功返回0,失败返回-1

//==============================================================================================

int delFile(PFILENODE file)
{
    if(file==NULL)
    {
     printf("node is not found!\n");    
     return -1;
    }
    del_node((void**)&myFileList, file);
//这里要注意修改

    delFileEx(file);
    count--;
    return 0;
}
//===============================================================================================

//函数名称:findFileByName(char *name)

//功能描述:通过name查找节点

//函数参数:name要查找的节点关键字

//返 回 值:找到的节点的结构体指针,没找到为null

//===============================================================================================

PFILENODE findFileByName(char *name)
{
    PFILENODE ret = NULL;
    PFILENODE tmpNod = myFileList;
    while(tmpNod)
    {
        if(strcmp(tmpNod->filename, name) == 0)
        {
            ret = tmpNod;
            break;
        }
        tmpNod = tmpNod->next;
    }
    return ret;
}
//=================================================================================================

//函数名称:

//功能描述:通过num查找节点

//函数参数:

//返回值:

//=================================================================================================

PFILENODE findFileByNum(unsigned int number)
{
    
//printf("find ok\n");

    PFILENODE ret = NULL;
    PFILENODE tmpNod = myFileList;
    while(tmpNod)
    {
        if(tmpNod->fileno == number)
        {
            ret = tmpNod;
            break;
        }
        tmpNod = tmpNod->next;
    }
    return ret;
}
//=================================================================================================

//函数名称:

//功能描述:打印输出链表

//函数参数:FIlE *file输出终端,显示器为,stdout

//返 回 值:输出链表个数

//=================================================================================================

int listFile(FILE *file)
{
    int counter = 1;
    char temp[10];
    PFILENODE fnode = myFileList;
    if(fnode == NULL)
        fprintf(file, "User List is empty!\n");
    else
    {
        fprintf(file, "%6s %20s %20s %20s %20s\n", "fileNo", "fileName", "filesize","mtime","filetype");
        while(fnode)
        {
            fprintf(file,"%6d %20s %20s %20s %20s\n", fnode->fileno, fnode->filename, fnode->filesize,fnode->mtime,fnode->filetype);
            fnode = fnode->next;
        }
        
//fprintf(file, "%d users found!\n", count);

    }
    return counter;
}
/*
int main()
{
    addFile("filename0", "size0","mtime1","0");
    listFile(stdout);
    addFile("filename1", "size1","mtime1","1");
    addFile("filename2", "size2","mtime1","2");
    addFile("filename3", "size3","mtime1","3");
    addFile("filename4", "size4","mtime1","4");
    addFile("filename5", "size5","mtime1","5");
    
    listFile(stdout);
    //delFile(findFileByName("filename3"));
    delFile(findFileByNum(3));
    delFile(findFileByName("filename4"));
    listFile(stdout);
    printf("init list...\n");
    initFileList();
    listFile(stdout);
    return 0;
}

*/

字符串处理函数:

 

#ifndef __ULITS_H_
#define __ULITS_H_

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

extern void trim(char *dest, int flag);
extern int strSplit(char *split,const char *sour,char *result[]);

#endif

//////////////////////////////////////////////////

#include "ulits.h"
//===============================================================================

//函数名称:trim

//功能描述:字符串处理,去掉2端的空格,变成全小写(flag==1)

//函数参数:dest被处理的字符串指针变量,flag标志(flag==1)将字符串转换为小写

//返回值: 无

//================================================================================

void trim(char *dest, int flag)
{
  char *ptr;
  int len;
  assert(dest!=NULL);
  ptr = dest;
  
  while (isspace(*ptr))
    ptr++;

  len = strlen(ptr);
  if (ptr > dest)
    memmove(dest, ptr, len+1);
  
  ptr = dest+len-1;

  while (isspace(*ptr))
    ptr--;

  *(ptr+1) = '\0';

  ptr = dest;

  if (flag == 1)
    while (*ptr!='\0')
    {
      *ptr = tolower(*ptr);
      ptr++;
    }
}
//=================================================================================

//函数名称:strSplit

//功能描述:将一个被字符串split,按分割符sour,分割成段,每段首地址存放于指针数组result

//函数参数:split被分割字符串的指针变量,sour分割符指针变量,result分割结果存放的指针数组

//返回值: int 分割的段数;

//==================================================================================

int strSplit(char *split,const char *sour,char *result[])
{
    char *pos,*start;
    int i=0,counter=0;
    
    assert((split!=NULL)&&(sour!=NULL));

    pos=start=split;
    while(pos!=NULL)
    {
        pos=strstr(start,sour);
//ls |

        if(pos==NULL)
        {
            result[i]=start;
            counter++;
            break;
        }
        
        *pos='\0';
        if(*start)
        {
            result[i]=start;
            i++;
            counter++;
        }
        start=pos+strlen(sour);
    }
    
    result[counter]=NULL;
    return counter;
}

通用链表:

 


//====================================================================================

//=============================================================

// 文件名称:lnklst.h

// 功能描述:通用链表管理程序

// 维护记录:2008-12-25     V1.0

// 2009-05-08 V1.1 by dengek fix some bug

//=============================================================

#pragma once
#ifdef __cplusplus
extern "C" {
#endif

typedef enum {NOD_BEFORE = 0, NOD_AFTER = 1} NOD_POSITION;

void add_node_to(void **head, void *node, void *to, NOD_POSITION before_or_after);
void move_node(void **head, void *moved, void *to, NOD_POSITION before_or_after);
void add_node(void **head, void *node);
void del_node(void **head, void *node);
typedef int (*CMP_FUNC)(void *t1, void *t2);
void sort_list(void **head, CMP_FUNC nodcmp);
void add_node_sorted(void **head, void *node, CMP_FUNC nodcmp);
int get_node_count(void **head);
void *next_node(void *current);
void *get_node_byID(void *head, int ID);

#ifdef __cplusplus
};
#endif


//=============================================================

// 文件名称:lnklst.c

// 功能描述:通用链表管理程序

// 维护记录:2008-12-25     V1.0

// 2009-05-08 V1.1 by dengke fix some bug

//=============================================================

#include <stdlib.h>
#include "lnklst.h"

#define ONE_WAY_LINKED_LIST
//#define TWO_WAY_LINKED_LIST


typedef struct basic_nod_t {
#ifdef TWO_WAY_LINKED_LIST
    struct basic_nod_t *prev;
#endif
    struct basic_nod_t *next;
} BASICNOD, *PBASICNOD;

static inline struct basic_nod_t *prev_node(PBASICNOD head, PBASICNOD nod)
{
#ifdef ONE_WAY_LINKED_LIST
    PBASICNOD n = head;
    if((nod == NULL) || ( nod == head))
        return NULL;
    while(n)
    {
        if(n->next == nod)
            return n;
        n = n->next;
    }
    return NULL;
#endif
#ifdef TWO_WAY_LINKED_LIST
    return nod->prev;
#endif
}

void *next_node(void *current)
{
    return (void *)((PBASICNOD)current)->next;
}

void *last_node(void *head)
{
    PBASICNOD p = (PBASICNOD)head;
    while(p && p->next)
    {
        p = p->next;
    }
    return p;
}

void add_node_to(void **head, void *node, void *to, NOD_POSITION before_or_after)
{
    struct basic_nod_t **h = (struct basic_nod_t**)head;
    struct basic_nod_t *n = (struct basic_nod_t*)node;
    struct basic_nod_t *tt = (struct basic_nod_t*)to;
    if(node == NULL)
        return;
    if(*h == NULL)
    {
        
// the link list is empty

        *h = n;
#ifdef TWO_WAY_LINKED_LIST
        n->prev = NULL;
#endif
        n->next = NULL;
    }
    else if(before_or_after == NOD_BEFORE)
    {
        
// move the 'moved' node to the place before 'to'

        
// if 'to' is NULL, then move the 'moved' node to the head

#ifdef ONE_WAY_LINKED_LIST
        PBASICNOD prev_nod;
#endif
        if(tt == NULL)
            tt = *h;
        if(*h == tt)
            *h = n;
#ifdef TWO_WAY_LINKED_LIST
        n->prev = tt->prev;
#endif
        n->next = tt;
#ifdef TWO_WAY_LINKED_LIST
        tt->prev = n;
        if(n->prev != NULL)
        {
            n->prev->next = n;
        }
#endif
#ifdef ONE_WAY_LINKED_LIST
        prev_nod = prev_node(*h, tt);
        if(prev_nod)
        {
            prev_nod->next = n;
        }
#endif
    }
    else if(before_or_after == NOD_AFTER)
    {
        
//move the 'moved' node to the place after 'to'

        if(tt == NULL)
            tt = last_node(*h);
        n->next = tt->next;
#ifdef TWO_WAY_LINKED_LIST
        n->prev = tt;
#endif
        tt->next = n;
#ifdef TWO_WAY_LINKED_LIST
        if(n->next != NULL)
            n->next->prev = n;
#endif
    }
}

void add_node(void **head, void *node)
{
    add_node_to(head, node, NULL, NOD_AFTER);
}

void del_node(void **head, void *node)
{
    PBASICNOD *h = (struct basic_nod_t**)head;
    PBASICNOD n = (struct basic_nod_t*)node;
#ifdef ONE_WAY_LINKED_LIST
    PBASICNOD tmp;
#endif
    if(h == NULL)
        return;
    if(*h == NULL)
        return;
    if(n == *h)
    {
        *h = n->next;
    }
#ifdef TWO_WAY_LINKED_LIST
    if(n->prev)
        n->prev->next = n->next;
    if(n->next)
        n->next->prev = n->prev;
#endif
#ifdef ONE_WAY_LINKED_LIST
    tmp = prev_node(*h, n);
    if(tmp)
        tmp->next = n->next;
#endif
}

void move_node(void **head, void *moved, void *to, NOD_POSITION before_or_after)
{
    PBASICNOD *h = (PBASICNOD *)head;
    PBASICNOD m = (PBASICNOD)moved;
    PBASICNOD tt = (PBASICNOD)to;
    if(    (h == NULL) || (*h == NULL) || (m == tt))
        return;
    del_node(head, moved);
    add_node_to(head, moved, to, before_or_after);
}

void sort_list(void **head, CMP_FUNC nodcmp)
{
    PBASICNOD *h = (PBASICNOD *)head;
    PBASICNOD nod1 = *h;
    PBASICNOD nod2 = nod1->next;
    int swaped = 1;
    if(nod2 == NULL)
        return;
    while(swaped)
    {
        swaped = 0;
        while(1)
        {
            if((*nodcmp)(nod1, nod2) > 0)
            {
                move_node(head, nod1, nod2, NOD_AFTER);
                nod2 = nod1->next;
                swaped = 1;
            }
            else
            {
                nod1 = nod2;
                nod2 = nod2->next;
            }
            if(nod2 == NULL)
            {
                nod1 = *h;
                nod2 = nod1->next;
                break;
            }
        }
    }
}

void add_node_sorted(void **head, void *node, CMP_FUNC nodcmp)
{
    int added = 0;
    PBASICNOD *h = (PBASICNOD *)head;
    PBASICNOD n = (PBASICNOD)node;
    PBASICNOD tmp = *h;
    if(*h == NULL)
    {
        add_node_to(head, node, NULL, NOD_AFTER);
        return;
    }
    if((*nodcmp)(n, *h) < 0)
    {
        add_node_to(head, node, *h, NOD_BEFORE);
        return;
    }
//    if((*nodcmp)(n, last_node(*head,*h)) >= 0)

//    {

//        add_node(head, node);

//        return;

//    }

    while(tmp)
    {
        if((*nodcmp)(n, tmp) < 0)
        {
            add_node_to(head, node, tmp, NOD_BEFORE);
            added = 1;
            break;
        }
        else
            tmp = tmp->next;
    }
    if(added == 0)
        add_node_to(head, node, NULL, NOD_AFTER);
}

int get_node_count(void **head)
{
    PBASICNOD *h = (PBASICNOD *)head;
    PBASICNOD tmp = *h;
    int count = 0;
    if(h == NULL)
        return 0;
    if(*h == NULL)
        return 0;
    while(tmp)
    {
        tmp = tmp->next;
        count++;
    }
    return count;
}

void *get_node_byID(void *head, int ID)
{
    PBASICNOD h = (PBASICNOD)head;
    PBASICNOD tmp;
    int count = 0;
    if(ID == 0)
        return head;
    for(tmp = h; tmp && (count < ID); tmp = tmp->next, count++);
    return tmp;
}

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