www.pudn.com > modifyencini.rar > CODEC_WriteDevParam.h


// 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 
#include 
#include 
#include 
#include 

using namespace std;

#define MAX_NET_ADDR_LEN 30
#define MAX_LINE_VAL_LEN 200
#define MAX_FILE_LINE 100

int API_WriteConfigFile(const char* filename, ConfigParamStruct *pParam) {
    cout<<"++API_WriteConfigFile()"<
        // 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; iiSendNum; 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
        // 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);
            } else if (name=="aud_mode") {
                sprintf(lineVal, "%d", pParam->AudChnMode);
            } else {
                continue;
            }
        } else {
            if (name=="video_horizontal_size") {
                sprintf(lineVal, "%d", pParam->HResolu);
            } else if (name=="video_vertical_size") {
                sprintf(lineVal, "%d", pParam->VResolu);
            } else if (name=="video_bit_rate") {
                sprintf(lineVal, "%d", pParam->VidBitRate);
            } else if (name=="video_vbv_buffer_size") {
                sprintf(lineVal, "%d", pParam->VBVSize);
            } else if (name=="vid_video_format") {
                sprintf(lineVal, "%d", pParam->VidFmt);
            } else if (name=="mux_mpeg_coding_standard") {
                sprintf(lineVal, "%d", pParam->VidMpeg);
            } else if (name=="audio_sampling_rate") {
                sprintf(lineVal, "%d", pParam->AudSamRate);
            } else if (name=="audio_bit_rate") {
                sprintf(lineVal, "%d", pParam->AudBitRate);
            } else if (name=="audio_channel") {
                sprintf(lineVal, "%d", pParam->AudChnMode);
            } 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(int i=0;i
        // 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=="com1_baud_rate") {
            sprintf(lineVal, "%d", pParam->COM1.BaudRate);
        } else if (name=="com1_parity_type") {
            sprintf(lineVal, "%c", pParam->COM1.ParityType);
        } else if (name=="com1_data_bit") {
            sprintf(lineVal, "%d", pParam->COM1.DataBitLen);
        } else if (name=="com1_stop_bit") {
            sprintf(lineVal, "%d", pParam->COM1.StopBitLen);
        } else if (name=="com2_baud_rate") {
            sprintf(lineVal, "%d", pParam->COM2.BaudRate);
        } else if (name=="com2_parity_type") {
            sprintf(lineVal, "%c", pParam->COM2.ParityType);
        } else if (name=="com2_data_bit") {
            sprintf(lineVal, "%d", pParam->COM2.DataBitLen);
        } else if (name=="com2_stop_bit") {
            sprintf(lineVal, "%d", pParam->COM2.StopBitLen);
        } else if (name=="local_alarm_com") {
            sprintf(lineVal, "%d", pParam->LocalAlarmCom);
        } else if (name=="com_remote_address") {
            strcpy(netAddr, pParam->ComRemoteAddr.sIP);
            sprintf(sPort, "%d", pParam->ComRemoteAddr.nPort);
            strcat(netAddr, ":");
            strcat(netAddr, sPort);
            strcpy(lineVal, netAddr);
        } 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(int i=0;iDevType;
    cfg_param.DevID    = dev_param->DevID;
    cfg_param.MediaType = dev_param->MediaType;
    cfg_param.TransProto= dev_param->TransProto;
    strcpy(cfg_param.local_network_ipaddr, dev_param->local_network_ipaddr);
    strcpy(cfg_param.local_network_mask,   dev_param->local_network_mask);
    strcpy(cfg_param.local_network_gateway,dev_param->local_network_gateway);
    cfg_param.my_port =  dev_param->my_port;
    memcpy(&(cfg_param.server_address), (char *)&(dev_param->server_address), sizeof(Net_Addr));
    strcpy(cfg_param.control_ipaddr, dev_param->control_ipaddr);
    cfg_param.doMulticast = dev_param->doMulticast;
    memcpy(&(cfg_param.multicast_address), (char *)&(dev_param->multicast_address), sizeof(Net_Addr));
    cfg_param.doNetSend = dev_param->doNetSend;
    cfg_param.iSendNum = dev_param->iSendNum;          
    memcpy(&(cfg_param.send_target_list), (char *)&(dev_param->send_target_list), sizeof(cfg_param.send_target_list));
    cfg_param.doFileWrite = dev_param->doFileWrite;
    // write config param to file
    char *cfgfilename = "./conf1/config.ini";
    retval = API_WriteConfigFile(cfgfilename, &cfg_param);
    if(retval == FAILURE) {
        printf("write config file error, quiting...\n");
        return FAILURE;
    }

    // get codec param
    _MEDIA_TYPE_ media_type;
    char codecfilename[MAX_FILE_NAME_LEN];
    media_type = dev_param->MediaType;
    if (MEDIA_VES == media_type) {
        strcpy(codecfilename,"./conf1/codec_es.ini");
    } else if (MEDIA_PS == media_type) {
        strcpy(codecfilename,"./conf1/codec_ps.ini");
    } else if (MEDIA_TS == media_type) {
        strcpy(codecfilename,"./conf1/codec_ts.ini");
    } else {
        printf("CODEC_WriteDevParam(): check media type unmatch\n");
        return FAILURE;
    }

    codec_param.HResolu    = dev_param->HResolu;
    codec_param.VResolu    =  dev_param->VResolu;   
    codec_param.VidBitRate =  dev_param->VidBitRate;
    codec_param.VBVSize    = dev_param->VBVSize;
    codec_param.VidFmt     = dev_param->VidFmt;
    codec_param.VidMpeg    = dev_param->VidMpeg;
    codec_param.AudSamRate = dev_param->AudSamRate;
    codec_param.AudBitRate =  dev_param->AudBitRate;
    codec_param.AudChnMode = dev_param->AudChnMode;
    // write codec param to file 
    retval = API_WriteCodecFile(codecfilename, &codec_param);
    if(retval == FAILURE) {
        printf("write codec file error, quiting...\n");
        return FAILURE;
    }

    // get com config param
    com_cfg.COM1.BaudRate = dev_param->com1_baud_rate;
    com_cfg.COM1.ParityType = dev_param->com1_parity_type;
    com_cfg.COM1.DataBitLen = dev_param->com1_data_bit;
    com_cfg.COM1.StopBitLen =  dev_param->com1_stop_bit;
    com_cfg.COM2.BaudRate =  dev_param->com2_baud_rate;
    com_cfg.COM2.ParityType =  dev_param->com2_parity_type;
    com_cfg.COM2.DataBitLen =  dev_param->com2_data_bit;
    com_cfg.COM2.StopBitLen = dev_param->com2_stop_bit;
    com_cfg.LocalAlarmCom = dev_param->local_alarm_com;
    memcpy(&(com_cfg.ComRemoteAddr),(char *)&(dev_param->com_remote_address), sizeof(Net_Addr));
    // write com config param to file
    char *comcfgfilename="./conf1/config.ini";
    retval = API_WriteComConfigFile(comcfgfilename, &com_cfg);
    if(retval == FAILURE) {
        printf("write com config file error, quiting...\n");
        return FAILURE;
    }

    return SUCCESS;
}

#endif