char line_assimilate (void) { /*---(locals)-----------+-----------+-*/ char rce = -10; int rc = 0; /*---(header)-------------------------*/ DEBUG_INPT yLOG_enter (__FUNCTION__); /*---(create file)--------------------*/ rc = file_create (); DEBUG_INPT yLOG_value ("file" , rc); --rce; if (rc < 0) { DEBUG_INPT yLOG_exitr (__FUNCTION__, rce); return rce; } /*---(open file)----------------------*/ DEBUG_INPT yLOG_info ("f_full" , my.f_full); rc = yPARSE_open_in (my.f_full); DEBUG_INPT yLOG_value ("open" , rc); --rce; if (rc < 0) { DEBUG_INPT yLOG_exitr (__FUNCTION__, rce); return rce; } /*---(lines)--------------------------*/ while (rc >= 0) { rc = yPARSE_read (&my.t_recdno, NULL); DEBUG_INPT yLOG_value ("yparse" , rc); if (rc == 0) { DEBUG_INPT yLOG_note ("end-of-file"); break; } --rce; if (rc < 0) { DEBUG_INPT yLOG_exitr (__FUNCTION__, rce); return rce; } rc = line_parse (); DEBUG_INPT yLOG_value ("parse" , rc); --rce; if (rc < 0) { DEBUG_INPT yLOG_exitr (__FUNCTION__, rce); return rce; } rc = line_create (); DEBUG_INPT yLOG_value ("data" , rc); --rce; if (rc < 0) { DEBUG_INPT yLOG_exitr (__FUNCTION__, rce); return rce; } } /*---(close file)---------------------*/ rc = yPARSE_close_in (); DEBUG_INPT yLOG_value ("close" , rc); --rce; if (rc < 0) { DEBUG_INPT yLOG_exitr (__FUNCTION__, rce); return rce; } /*---(complete)-----------------------*/ DEBUG_INPT yLOG_exit (__FUNCTION__); return 0; }
/** * @brief 分析客户端输入的数据 * * @param cli 客户端数据结构 * @param input 输入的数据 * * @return */ int client_parse_input(ChatClient *cli, char *input) { int ret = 0; int file_flag = 0; input = str_strip(input); //去掉字符串首尾的空格 if(input == NULL) { return -1; } char *to = NULL; char *msg = NULL; ServerCmd cmd = CMD_LAST; packet_free(cli->pktsnd); //清空发送数据包 cli->pktsnd = NULL; if(strncmp("to ", input, 3) == 0) //比较前三个字符 { input += 3; if(line_parse(input, ':', &to, &msg) == 0) //以:为分隔符,将字符串分成两段 { cli->pktsnd = packet_new(cli->name, to); } } else if(strncmp("file to ", input, 8) == 0) { input += 8; if(line_parse(input, ':', &to, &msg) == 0) //以:为分隔符,将字符串分成两段 { cli->pktsnd = packet_new(cli->name, to); } file_flag = 1; } else if(strcmp(input, "whoison") == 0) { cli->pktsnd = packet_new(cli->name, SERV_NAME); cmd = CMD_WHOISON; msg = (char *) g_cmd[cmd]; } else if(strcmp(input, "showuser") == 0) { cli->pktsnd = packet_new(cli->name, SERV_NAME); cmd = CMD_SHOWUSER; msg = (char *) g_cmd[cmd]; } else if(strcmp(input, "help") == 0) { printf("\rto usr: msg --------- send msg to 'usr'\n"); printf("file to usr: filepath --------- send file to 'usr'\n"); printf("whoison --------- check server who is online\n"); printf("howuser --------- check all users who are registered\n"); printf("logout/bye/exit --------- logout and exit client\n"); return 0; } else if(strcmp(input, "logout") == 0 || strcmp(input, "exit") == 0 || strcmp(input, "bye") == 0) { cli->pktsnd = packet_new(cli->name, SERV_NAME); cmd = CMD_LOGOUT; msg = (char *) g_cmd[cmd]; } if(file_flag) { cli->pktsnd->type = get_msg_type(MSG_FILE_SEND); //设置发送的是文件类型 packet_add_msg(cli->pktsnd, msg); FILE *fp = fopen(msg, "r"); char buffer[MAXLEN]; if (fp == NULL) { printf("File: %s Not Found!\n", msg); } else { bzero(buffer, MAXLEN); while(fgets(buffer, MAXLEN, fp) != NULL) { packet_add_msg(cli->pktsnd, buffer); bzero(buffer, MAXLEN); } fclose(fp); printf("File:\t%s Transfer Finished!\n", msg); } ret = client_flush(cli); } if(msg && cli->pktsnd) { cli->pktsnd->type = get_msg_type(MSG_TEXT_SEND); //设置发送的是文字类型 packet_add_msg(cli->pktsnd, msg); ret = client_flush(cli); if(cmd==CMD_LOGOUT) { exit(0); } } return ret; }