美文网首页
常用编程惯例

常用编程惯例

作者: jiangling500 | 来源:发表于2019-04-21 13:14 被阅读0次

    获取2字节整数的高8位和低8位

    unsigned short port = 8888;
    printf("high Byte:%u,low Byte:%u\n", port >> 8, port & 0xFF);
    

    拷贝字符串

    char buf[512] = {0};
    snprintf(buf, sizeof(buf), "%s", "hello world"); // 需要格式化字符串
    snprintf(buf, sizeof(buf), "hello world"); // 不需要格式化字符串,直接拷贝字符串的话,可直接使用这种方式
    

    避免僵尸进程

    在父进程中,通过忽略SIGCHLD即可避免僵尸进程:

    signal(SIGCHLD, SIG_IGN);
    

    封装perror与exit

    #define ERR_EXIT(m) \
      do \
      { \
        perror(m); \
        exit(EXIT_FAILURE); \
      } while(0)
    

    打开一个文件,并按行读取

    C语言方式

    FILE *fp = fopen("miniftpd.conf", "r");
    if (NULL == fp)
    {
      ERR_EXIT("fopen");
    }
    
    char setting_line[1024] = {0};
    while (NULL != fgets(setting_line, sizeof(setting_line), fp))
    {
      // TODO:使用setting_line
      bzero(setting_line, sizeof(setting_line));
    }
    
    fclose(fp);
    

    C++方式

    
    

    Java方式

    
    

    计算较小/大值

    #define MIN(a, b) ((a) > (b) ? (b) : (a))
    
    #define MAX(a, b) ((a) > (b) ? (a) : (b))
    

    将错误信息输出到标准错误中

    fprintf(stderr, "error: %s", "404");
    

    计算一段代码执行所需要的时间

    time_t startTime = clock();
    // TODO:添加需要执行的代码
    time_t endTime = clock();
    
    cout << "time : " << double(endTime - startTime) / CLOCKS_PER_SEC << " s" << endl;
    

    计算数组大小的宏函数

    #define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
    

    使用命令映射代替if-else和switch

    typedef struct ftpcmd
    {
        const char *cmd;
        void (*cmd_handler)(session_t *sess);
    } ftpcmd_t;
    
    ftpcmd_t ctrl_cmds[] = {
            {"USER", do_user},
            {"PASS", do_pass},
    };
    
    int main()
    {
        int size = ARRAY_SIZE(ctrl_cmds);
        for (int i = 0; i < size; ++i)
        {
            if (0 == strcmp(ctrl_cmds[i].cmd, sess->cmd))
            {
                if (NULL != ctrl_cmds[i].cmd_handler)
                {
                    ctrl_cmds[i].cmd_handler(sess);
                }
            }
        }
        
        return 0;
    }
    

    可读性以及可扩展性较好的结构体数组初始化方式

    typedef struct person
    {
        const char *name;
        int age;
        bool gender;
        double salary;
    } person_t;
    
    person_t students[] = {
            [0] = {
                    .name = "A",
                    .age = 20,
                    .gender = true,
                    .salary = 10000.0,
                  },
            [1] = {
                    .name = "B",
                    .age = 20,
                    .gender = true,
                    .salary = 10000.0,
            },
    };
    
    int main()
    {
        int size = ARRAY_SIZE(students);
        for (int i = 0; i < size; ++i)
        {
            printf("%s\n", students[i].name);
        }
    
        return 0;
    }
    

    从特定格式字符串中读取数据

    const char *ip_str = "127,0,0,1";
    unsigned char ip_int[4];
    sscanf(ip_str, "%u,%u,%u,%u", (unsigned int *)ip_int, (unsigned int *)(ip_int + 1), (unsigned int *)(ip_int + 2), (unsigned int *)(ip_int + 3));
    
    struct sockaddr_in addr;
    bzero(&addr, sizeof(addr));
    unsigned char *p = (unsigned char *)&addr.sin_addr.s_addr;
    p[0] = ip_int[0];
    p[1] = ip_int[1];
    p[2] = ip_int[2];
    p[3] = ip_int[3];
    
    char ip[16] = {0};
    inet_ntop(AF_INET, &addr.sin_addr.s_addr, ip, sizeof(ip));
    printf("ip = %s\n", ip); // 127.0.0.1
    

    相关文章

      网友评论

          本文标题:常用编程惯例

          本文链接:https://www.haomeiwen.com/subject/srdlgqtx.html