⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 codec_writedevparam.h

📁 修改配置文件的经典程序
💻 H
📖 第 1 页 / 共 2 页
字号:
// use g++ to compile#ifndef _CODEC_WRITE_DEV_PARAM_H_#define _CODEC_WRITE_DEV_PARAM_H_#include "./types.h"#include "./CODEC_API.h"#include "./CodecParamStruct.h"#include "./ConfigParamStruct.h"#include "./ComParamStruct.h"#include <stdio.h>#include <iostream>#include <stdlib.h>#include <string.h>#include <string>using namespace std;#define MAX_NET_ADDR_LEN 30#define MAX_LINE_VAL_LEN 200#define MAX_FILE_LINE 100int API_WriteConfigFile(const char* filename, ConfigParamStruct *pParam) {    cout<<"++API_WriteConfigFile()"<<endl;    FILE *file = fopen(filename, "r"); // open the config file    if (file==NULL) {        printf("Cannot find config file: %s\n",filename);        return FAILURE;    }    int i;    string line, name,val;    int eq_pos;    char lineTemp[8*1024];    char lineCover[8*1024];    char lineVal[MAX_LINE_VAL_LEN];    char netAddr[MAX_NET_ADDR_LEN];    char sPort[10];    char fileContent[MAX_FILE_LINE][8*1024];    int lineCounter=0;    // read the file content line by line    // fgets() return when encounter enter    while (fgets(lineTemp, sizeof(lineTemp), file)!=NULL) {        if (lineTemp[strlen(lineTemp)-1]=='\n') lineTemp[strlen(lineTemp)-1] = '\0';        strcpy(fileContent[lineCounter],lineTemp);        lineCounter++;        // string class: add "using namespace std" and include <string>        // member func: swap(),erase(),insert(),clear(),replace()...        line.erase(); // clear character string        line.insert(0, lineTemp); // insert character string on position index 0        // find function returns the index. If nothing's found, string::npos returned        if (line.find_first_not_of(" \t\n\r")==string::npos) continue; // skip ahead if line is empty        if (line[0]=='#') continue; // skip ahead if the line is a comment        // use line informaton to splits up name and value        eq_pos = line.find("=");        // check to see if entry is valid, can't find "="        if (eq_pos == string::npos) continue;        // get name and value        name.assign(line,0,eq_pos);        val = line.substr(eq_pos+1);        memset(lineCover, 0, sizeof(lineCover));        strcpy(lineCover,name.c_str());        strcat(lineCover,"=");        memset(lineVal, 0, sizeof(lineVal));        if (name=="media_type") {            if (MEDIA_VES == pParam->MediaType) {                strcpy(lineVal, "VES");            } else if (MEDIA_PS == pParam->MediaType) {                strcpy(lineVal, "PS");            } else if (MEDIA_TS == pParam->MediaType) {                strcpy(lineVal, "TS");            } else {                printf("unknow media type\n");        	continue;            }        } else if (name=="transport_protocol") {            if (PROTO_UDP == pParam->TransProto) {                strcpy(lineVal, "UDP");            } else if (PROTO_RTP == pParam->TransProto) {                strcpy(lineVal, "RTP");            } else {        	printf("unknow transport protocol\n");        	continue;            }        } else if (name=="local_network_ipaddr") {	    strcpy(lineVal, pParam->local_network_ipaddr);        } else if (name=="local_network_mask") {	    strcpy(lineVal, pParam->local_network_mask);        } else if (name=="local_network_gateway") {	    strcpy(lineVal, pParam->local_network_gateway);        } else if (name=="my_port") {            sprintf(lineVal, "%d", pParam->my_port);         } else if (name=="server_address") {            strcpy(netAddr, pParam->server_address.sIP);            sprintf(sPort, "%d", pParam->server_address.nPort);            strcat(netAddr, ":");            strcat(netAddr, sPort);            strcpy(lineVal, netAddr);        } else if (name=="control_ipaddr") {            strcpy(lineVal, pParam->control_ipaddr);        } else if (name=="doMulticast") {            if (true == pParam->doMulticast) {                strcpy(lineVal, "TRUE");            } else {                strcpy(lineVal, "FALSE");            }        } else if (name=="multicast_address") {            strcpy(netAddr, pParam->multicast_address.sIP);            sprintf(sPort, "%d", pParam->multicast_address.nPort);            strcat(netAddr, ":");            strcat(netAddr, sPort);            strcpy(lineVal, netAddr);        } else if (name=="doNetSend") {            if (true == pParam->doNetSend) {                strcpy(lineVal, "TRUE");            } else {                strcpy(lineVal, "FALSE");            }        } else if (name=="send_target_list") {            for (i=0; i<pParam->iSendNum; i++) {                strcpy(netAddr, pParam->send_target_list[i].sIP);                sprintf(sPort, "%d", pParam->send_target_list[i].nPort);                strcat(netAddr, ":");                strcat(netAddr, sPort);                strcat(lineVal, netAddr);                strcat(lineVal, ",");            }            i = strlen(lineVal);            lineVal[i-1] = '\0'; // the end ','        } else if (name=="doFileWrite") {            if (true == pParam->doFileWrite) {                strcpy(lineVal, "TRUE");            } else {                strcpy(lineVal, "FALSE");            }        } else {        	continue;        }        strcat(lineCover, lineVal);        strcpy(fileContent[lineCounter-1], lineCover);    }    if (fclose(file)!=0) printf("Cannot close config file\n");    //write fileContent to file    file=fopen(filename,"w");    for(i=0;i<lineCounter;i++) {//        cout<<"write line "<<i<<": "<<fileContent[i]<<endl;        fprintf(file,"%s\n",fileContent[i]);    }    fclose(file);    return SUCCESS;}int API_WriteCodecFile(const char* filename, CodecParamStruct *pParam) {    cout<<"++API_WriteCodecFile()"<<endl;    FILE *file = fopen(filename, "r"); // open the config file    if (file==NULL) {        printf("Cannot find codec file: %s\n",filename);        return FAILURE;    }    bool bPsFile=false, bTsFile=false,bEsFile=false;    if(strstr(filename,"ps")!=NULL) {        bPsFile=true;    } else if (strstr(filename,"ts")!=NULL) {        bTsFile=true;    } else if (strstr(filename,"es")!=NULL) {        bEsFile=true;    } else {        printf("codec file name format uncorrect, e.g codec_ps.ini\n");        return FAILURE;    }    string line, name,val;    int eq_pos;    char lineTemp[8*1024];    char lineCover[8*1024];    char lineVal[MAX_LINE_VAL_LEN];    char fileContent[MAX_FILE_LINE][8*1024];    int lineCounter=0;    // read the file content line by line    // fgets() return when encounter enter    while (fgets(lineTemp, sizeof(lineTemp), file)!=NULL) {        if (lineTemp[strlen(lineTemp)-1]=='\n') lineTemp[strlen(lineTemp)-1] = '\0';        strcpy(fileContent[lineCounter],lineTemp);        lineCounter++;        // string class: add "using namespace std" and include <string>        // member func: swap(),erase(),insert(),clear(),replace()...        line.erase(); // clear character string        line.insert(0, lineTemp); // insert character string on position index 0        // find function returns the index. If nothing's found, string::npos returned        if (line.find_first_not_of(" \t\n\r")==string::npos) continue; // skip ahead if line is empty        if (line[0]=='#') continue; // skip ahead if the line is a comment        // use line informaton to splits up name and value        eq_pos = line.find("=");        // check to see if entry is valid, can't find "="        if (eq_pos == string::npos) continue;        // get name and value        name.assign(line,0,eq_pos);        val = line.substr(eq_pos+1);        strcpy(lineCover,name.c_str());        strcat(lineCover,"=");        memset(lineVal, 0, sizeof(lineVal));        if (bPsFile || bEsFile) {            if (name=="vid_horizontal_size_value") {                sprintf(lineVal, "%d", pParam->HResolu);            } else if (name=="vid_vertical_size_value") {                 sprintf(lineVal, "%d", pParam->VResolu);            } else if (name=="vid_bit_rate") {                sprintf(lineVal, "%d", pParam->VidBitRate);            } else if (name=="vid_vbv_buffer_size") {                sprintf(lineVal, "%d", pParam->VBVSize);            } else if (name=="vid_video_format") {                sprintf(lineVal, "%d", pParam->VidFmt);            } else if (name=="vid_stream_type") {                sprintf(lineVal, "%d", pParam->VidMpeg);            } else if (name=="aud_sampling_rate") {                sprintf(lineVal, "%d", pParam->AudSamRate);            } else if (name=="aud_bit_rate") {                sprintf(lineVal, "%d", pParam->AudBitRate);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -