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

📄 codec_getdevparam.h

📁 修改配置文件的经典程序
💻 H
📖 第 1 页 / 共 2 页
字号:
// must use g++ to compile
#ifndef _CODEC_GET_DEV_PARAM_H_
#define _CODEC_GET_DEV_PARAM_H_

#include "./CODEC_API.h"
#include "./CodecParamStruct.h"
#include "./ConfigParamStruct.h"
#include "./ComParamStruct.h"
#include "./types.h"

#include <string> // string class
#include <string.h> // strcpy
#include <stdio.h>

using namespace std;

/**initialize Struct*/
void API_InitDevParamStruct (LPVOID pParam) {
    printf("++API_InitDevParamStruct()\n");
    DevParamStruct *param = (DevParamStruct *) pParam;
    memset(param, 0, sizeof(DevParamStruct));
}

void API_InitConfigParamStruct(LPVOID pParam) {
    printf("++API_InitConfigParamStruct()\n");
    ConfigParamStruct *param = (ConfigParamStruct *)pParam;
    memset(param, 0, sizeof(ConfigParamStruct));
}

void API_InitCodecParamStruct(LPVOID pParam) {
    printf("++API_InitCodecParamStruct()\n");
    CodecParamStruct *param = (CodecParamStruct *)pParam;
    memset(param, 0, sizeof(CodecParamStruct));
}

void API_InitComConfigStruct(LPVOID pParam) {
    printf("++API_InitComConfigStruct()\n");
    ComConfigStruct *param = (ComConfigStruct *)pParam;
    memset(param, 0, sizeof(ComConfigStruct));
}

/** given an entry in the format of <name>=<value>, parse it accordingly */
int API_ParseEntry(const string &entry, string &name, string &val) {
    int eq_pos = entry.find("=");

    // check to see if entry is valid, can't find "="
    if (eq_pos == string::npos) {
    	// entry.c_str() Conversion to C Style Strings
        printf("expected <name>=<value> entry, got %s instead\n",entry.c_str());
        return FAILURE;
    }

    // get name
    name.assign(entry,0,eq_pos);
    // get value, return the substring behind index eq_pos
    val = entry.substr(eq_pos+1);
    
    return SUCCESS;
}

/** given a val in the format of ipaddr:port,parse it accordingly */
int API_ParseColon (const string &val, char *pIpaddr, int &port) {
    string val_ipaddr, val_port;

    int colon_pos = val.find(":");

    // check to see if entry is valid, can't find "="
    if (colon_pos == string::npos) {
    	// entry.c_str() Conversion to C Style Strings
        printf("expected ipaddr:port entry, got %s instead\n",val.c_str());
        return FAILURE;
    }

    // get ipaddr and port
    val_ipaddr.assign(val,0,colon_pos);
    val_port = val.substr(colon_pos+1);

    sscanf(val_ipaddr.c_str(), "%s", pIpaddr);
    sscanf(val_port.c_str(), "%d", &port);

    return SUCCESS;
}

/** Given an entry in the format of <name>=<v1>,<v2>... parse it accordingly.*/
int API_ParseArray( const string &val_num,Net_Addr *pSendlist,int &send_nums) {

    string val = val_num;
    int colon_pos = val.find(":");

    // check to see if entry is valid
    if (colon_pos == string::npos) {
        printf("expected name:value entry, got %s instead\n",val.c_str());
        return FAILURE;
    }

    string str;
    send_nums = 0;
    int semicolon_pos = colon_pos; // init !=0

    while (semicolon_pos != string::npos && send_nums < MAX_UCAST_LIST) {
	semicolon_pos = val.find(",");
	str.assign(val, 0, semicolon_pos);
	API_ParseColon(str,pSendlist[send_nums].sIP,pSendlist[send_nums].nPort);
        send_nums++;
        val = val.substr(semicolon_pos+1);
    }

//    printf("it finds %d destinations in send target list.\n",send_nums);
    
    return SUCCESS;
}

/** parse a given config file */
int API_ParseConfigFile(const char* filename, ConfigParamStruct *pParam) {
    printf("++API_ParseConfigFile()\n");
	
    FILE *file = fopen(filename, "r");	// open the config file
    if (file==NULL) {
  	printf("Cannot find config file: %s\n",filename);
	return FAILURE;
    }
	
    ConfigParamStruct *appparam = NULL;
    appparam = pParam;

    string line, name,val,valTemp1,valTemp2;
    char lineTemp[8*1024];

    // 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';

	// 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
        
        API_ParseEntry(line,name,val);	// use line informaton to splits up name and value

        if (name=="device_type") {
            if (val == "CODEC") {
                appparam->DevType=DEV_CODEC;
            } else if (val == "ENCODER"){
                appparam->DevType=DEV_ENCODER;
            } else if (val == "DECODER"){
                appparam->DevType=DEV_DECODER;
	    } else {
       	        printf("unknow device type definition detected!\n");
        	continue;
            }
        } else if (name=="device_ID") {
            sscanf(val.c_str(), "%d", &appparam->DevID);
        } else if (name=="media_type") {
            if (val == "VES") {
                appparam->MediaType = MEDIA_VES;
            } else if (val == "PS") {
            	appparam->MediaType = MEDIA_PS;
            } else if (val == "TS") {
                appparam->MediaType = MEDIA_TS;
            } else {
                printf("unknow media type definition detected!\n");
        	continue;
            }
        } else if (name=="transport_protocol") {
            if (val == "UDP") {
         	appparam->TransProto=PROTO_UDP;
            } else if (val == "RTP"){
         	appparam->TransProto=PROTO_RTP;
            } else {
        	printf("unknow transport protocol definition detected!\n");
        	continue;
            }
        } else if (name=="local_network_ipaddr") {
	    strcpy(appparam->local_network_ipaddr,val.c_str());
        } else if (name=="local_network_mask") {
	    strcpy(appparam->local_network_mask,val.c_str());
        } else if (name=="local_network_gateway") {
	    strcpy(appparam->local_network_gateway,val.c_str());
        } else if (name=="my_port") {
             sscanf(val.c_str(), "%d", &appparam->my_port); 
        } else if (name=="server_address") {
	    API_ParseColon (val,appparam->server_address.sIP,appparam->server_address.nPort);
        } else if (name=="control_ipaddr") {
	    strcpy(appparam->control_ipaddr,val.c_str());
        } else if (name=="doMulticast") {
            appparam->doMulticast=(val=="TRUE") || (val=="true");
        } else if (name=="multicast_address") {
	    API_ParseColon (val,appparam->multicast_address.sIP,appparam->multicast_address.nPort);
        } else if (name=="doNetSend") {
            appparam->doNetSend=(val=="TRUE") || (val=="true");
        } else if (name=="send_target_list") {
            API_ParseArray(val,appparam->send_target_list,appparam->iSendNum);
        } else if (name=="doFileWrite") {
            appparam->doFileWrite=(val=="TRUE") || (val=="true");
        } else {
        	continue;
        }
    }
    
    if (fclose(file)!=0) printf("Cannot close config file\n");

    return SUCCESS;
}

/** parse a given codec file */
int API_ParseCodecFile(const char* filename, CodecParamStruct *pParam) {
    printf("++API_ParseCodecFile()\n");

    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;
    }

    CodecParamStruct *appparam = NULL;
    appparam = pParam;

    string line, name,val,valTemp1,valTemp2;
    char lineTemp[8*1024];

⌨️ 快捷键说明

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