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

📄 cms_cfg.cc

📁 Source code for an Numeric Cmputer
💻 CC
📖 第 1 页 / 共 2 页
字号:
/********************************************************************* Description: cms_cfg.cc*   C++ file for the  Communication Management System (CMS).*   Provides cms_config -- CMS function which reads configuration file.**   Derived from a work by Fred Proctor & Will Shackleford** Author:* License: LGPL Version 2* System: Linux*    * Copyright (c) 2004 All rights reserved.** Last change: * $Revision: 1.9 $* $Author: paul_c $* $Date: 2005/06/13 14:38:49 $********************************************************************/extern int verbose_nml_error_messages;#ifdef __cplusplusextern "C" {#endif#include <stdio.h>		/* sscanf(), NULL FILE, fopen(), fgets() */#include <unistd.h>		/* gethostname() */#include <string.h>		/* strcpy(), strlen(),memcpy()				   strcmp(),strchr() */#include <errno.h>		/* errno */#include <ctype.h>		/* toupper(), tolower() */#include <netdb.h>#include <arpa/inet.h>		/* inet_ntoa */#ifdef __cplusplus}#endif#include "cms.hh"		/* class CMS */#include "cms_cfg.hh" /* TCP stands for "Transmission Control Protocol". This is the recommended    method for remote connection, for most applications. It is more reliable    and can handle larger messages than UDP and is more widely available than     RPC. */#include "tcpmem.hh"		/* class TCPMEM */ /* If the buffer type or process type specified in the configuration file is     "PHANTOM" then every NML call of that type will result in calling your    phantom function. */#include "phantom.hh"		/* class PHANTOMMEM */ /* LOCMEM is useful when many modules are linked together in one thread of    execution but you want to write them such that each module uses the NML    API just as it would if it needed to communicate with the other modules    running separately. There is no need for any mutual exclusion mechanism    and memory is obtained with a simple malloc so the operating system will    not exceed its limits for semaphores or shared memory segments. */#include "locmem.hh"		/* class LOCMEM */ /* SHMEM is intended for communications between tasks managed by the same    operating system. The operating system allocates the memory to be shared    so users do not need to specify one. Users have a choice of mutual    exclusion techniques, using an operating system semaphore or mutex, or    disabling and enabling context switching or interrupts during the    appropriate critical sections. */#include "shmem.hh"		/* class SHMEM */#include "rcs_print.hh"		/* rcs_print_error() */#include "linklist.hh"		/* LinkedList */struct CONFIG_FILE_INFO {    CONFIG_FILE_INFO() {	lines_list = NULL;    };    ~CONFIG_FILE_INFO() {	if (NULL != lines_list) {	    delete lines_list;	    lines_list = NULL;	}    };    LinkedList *lines_list;    char file_name[80];};static LinkedList *config_file_list = NULL;static int loading_config_file = 0;int load_nml_config_file(const char *file){    unload_nml_config_file(file);    if (loading_config_file) {	return -1;    }    loading_config_file = 1;    if (NULL == file) {	loading_config_file = 0;	return -1;    }    if (NULL == config_file_list) {	config_file_list = new LinkedList();    }    if (NULL == config_file_list) {	loading_config_file = 0;	return -1;    }    char line[CMS_CONFIG_LINELEN];	/* Temporary buffer for line from					   file. */    CONFIG_FILE_INFO *info = new CONFIG_FILE_INFO();    info->lines_list = new LinkedList();    strncpy(info->file_name, file, 80);    FILE *fp;    fp = fopen(file, "r");    if (fp == NULL) {	rcs_print_error("cms_config: can't open '%s'. Error = %d -- %s\n",	    file, errno, strerror(errno));	if (NULL != info) {	    delete info;	}	loading_config_file = 0;	return -1;    }    while (!feof(fp)) {	if ((fgets(line, CMS_CONFIG_LINELEN, fp)) == NULL) {	    break;	}	int linelen = strlen(line);	if (linelen < 3) {	    continue;	}	while (line[linelen - 1] == '\\') {	    int pos = linelen - 2;	    if ((fgets(line + pos, CMS_CONFIG_LINELEN - pos, fp)) == NULL) {		break;	    }	    linelen = strlen(line);	    if (linelen > CMS_CONFIG_LINELEN - 2) {		break;	    }	}	if (line[0] == '#') {	    continue;	}	info->lines_list->store_at_tail(line, linelen + 1, 1);    }    if (NULL != fp) {	fclose(fp);	fp = NULL;    }    config_file_list->store_at_tail(info, sizeof(info), 0);    loading_config_file = 0;    return 0;}int unload_nml_config_file(const char *file){    if (loading_config_file) {	return -1;    }    if (NULL == file) {	return -1;    }    if (NULL == config_file_list) {	return -1;    }    CONFIG_FILE_INFO *info =	(CONFIG_FILE_INFO *) config_file_list->get_head();    while (NULL != info) {	if (!strncmp(info->file_name, file, 80)) {	    config_file_list->delete_current_node();	    delete info;	    return 0;	}	info = (CONFIG_FILE_INFO *) config_file_list->get_next();    }    return -1;}CONFIG_FILE_INFO *get_loaded_nml_config_file(const char *file){    if (NULL == file) {	return NULL;    }    if (NULL == config_file_list) {	return NULL;    }    CONFIG_FILE_INFO *info =	(CONFIG_FILE_INFO *) config_file_list->get_head();    while (NULL != info) {	if (!strncmp(info->file_name, file, 80)) {	    return info;	}	info = (CONFIG_FILE_INFO *) config_file_list->get_next();    }    return NULL;}/*! \todo Another #if 0 */#if 0int print_loaded_nml_config_file(const char *file){    CONFIG_FILE_INFO *info = get_loaded_nml_config_file(file);    if (NULL == info) {	rcs_print("Config file %s not loaded.\n");	return -1;    }    if (NULL == info->lines_list) {	return -1;    }    char *line = (char *) info->lines_list->get_head();    while (NULL != line) {	rcs_print("%s", line);	int linelen = strlen(line);	if (linelen > 1) {	    char last_char = line[linelen - 1];	    if (last_char != '\n' && last_char != '\r') {		rcs_print("\n");	    }	}	line = (char *) info->lines_list->get_next();    }    rcs_print("\n");    return 0;}int print_loaded_nml_config_file_list(){    if (loading_config_file) {	rcs_print	    ("In the process of loading a config file, please try again later.\n");	return -1;    }    if (NULL == config_file_list) {	rcs_print("No Configuration files loaded.\n");	return 0;    }    CONFIG_FILE_INFO *info =	(CONFIG_FILE_INFO *) config_file_list->get_head();    while (NULL != info) {	if (NULL != info->lines_list) {	    rcs_print("%s \t- - \t%d lines\n", info->file_name,		info->lines_list->list_size);	} else {	    rcs_print("%s \t-1 lines", info->file_name);	}	info = (CONFIG_FILE_INFO *) config_file_list->get_next();    }    return 0;}#endifint unload_all_nml_config_file(){    if (loading_config_file) {	return -1;    }    if (NULL == config_file_list) {	return -1;    }    CONFIG_FILE_INFO *info =	(CONFIG_FILE_INFO *) config_file_list->get_head();    while (NULL != info) {	config_file_list->delete_current_node();	delete info;	info = (CONFIG_FILE_INFO *) config_file_list->get_next();    }    if (config_file_list->list_size <= 0) {	delete config_file_list;	config_file_list = NULL;    }    return 0;}static int convert2upper(char *dest, char *src, int len){    int i;    if (src == NULL || dest == NULL) {	rcs_print_error("convert2upper passed NULL argument.\n");	return -1;    }    for (i = 0; i < len; i++) {	if (src[i] == 0) {	    dest[i] = 0;	    return i;	}	dest[i] = toupper(src[i]);    }    return i;}int cms_copy(CMS ** dest, CMS * src, int set_to_server, int set_to_master){    if (NULL == dest || NULL == src) {	return -1;    }    return cms_create_from_lines(dest, src->BufferLine, src->ProcessLine,	set_to_server, set_to_master);}extern char *get_buffer_line(const char *bufname, const char *filename){    int line_len, line_number;    char linebuf[CMS_CONFIG_LINELEN];	/* Temporary buffer for line from					   file. */    char *line = linebuf;    FILE *fp = NULL;		/* FILE ptr to config file.  */    char *word[4];		/* array of pointers to words from line */    /* Open the configuration file. */    LinkedList *lines_list = NULL;    CONFIG_FILE_INFO *info = get_loaded_nml_config_file(filename);    if (NULL != info) {	lines_list = info->lines_list;	line = (char *) lines_list->get_head();    }    if (NULL == lines_list) {	fp = fopen(filename, "r");	if (fp == NULL) {	    rcs_print_error("cms_config: can't open '%s'. Error = %d -- %s\n",		filename, errno, strerror(errno));	    loading_config_file = 0;	    return NULL;	}    }    /* Read the configuration file line by line until the lines matching */    /* bufname and procname are found.  */    line_number = 0;    int first_line = 1;    while (1) {	if (NULL != lines_list) {	    if (!first_line) {		line = (char *) lines_list->get_next();	    }	    first_line = 0;	    if (NULL == line) {		break;	    }	} else {	    if (feof(fp)) {		break;	    }	    if ((fgets(line, CMS_CONFIG_LINELEN, fp)) == NULL) {		break;	    }	}	line_number++;	line_len = strlen(line);	while (line[line_len - 1] == '\\') {	    int pos = line_len - 2;	    if ((fgets(line + pos, CMS_CONFIG_LINELEN - pos, fp)) == NULL) {		break;	    }	    line_len = strlen(line);	    if (line_len > CMS_CONFIG_LINELEN - 2) {		break;	    }	    line_number++;	}	if (line_len > CMS_CONFIG_LINELEN) {	    rcs_print_error		("cms_cfg: Line length of line number %d in %s exceeds max length of %d",		line_number, filename, CMS_CONFIG_LINELEN);	}	/* Skip comment lines and lines starting with white space. */	if (line[0] == CMS_CONFIG_COMMENTCHAR ||	    strchr(" \t\n\r\0", line[0]) != NULL) {	    continue;	}	/* Separate out the first four strings in the line. */	if (separate_words(word, 4, line) != 4) {	    continue;	}	if (!strcmp(word[1], bufname) && line[0] == 'B') {	    /* Buffer line found, store the line and type. */	    return line;	}    }    return NULL;}enum CONFIG_SEARCH_ERROR_TYPE {    CONFIG_SEARCH_ERROR_NOT_SET,    CONFIG_SEARCH_OK,    BAD_CONFIG_FILE,    NO_PROCESS_LINE,    NO_BUFFER_LINE,    MISC_CONFIG_SEARCH_ERROR};struct CONFIG_SEARCH_STRUCT {    enum CONFIG_SEARCH_ERROR_TYPE error_type;    int bufline_found;    int bufline_number;    int procline_found;    int procline_number;    const char *bufname;    const char *bufname_for_procline;    const char *procname;    const char *filename;    char buffer_line[CMS_CONFIG_LINELEN];	/* Line matching bufname. */    char proc_line[CMS_CONFIG_LINELEN];	/* Line matching procname & bufname. */    char buffer_type[CMS_CONFIG_LINELEN];	/* "SHMEM" or "GLOBMEM" */    char proc_type[CMS_CONFIG_LINELEN];	/* "REMOTE" or "LOCAL" */};void find_proc_and_buffer_lines(CONFIG_SEARCH_STRUCT * s);/* Function for initializing a CMS from a configuration file. */ /* Returns 0 for success or -1 for error. */int cms_config(CMS ** cms, char *bufname, char *procname, char *filename,    int set_to_server, int set_to_master){    CONFIG_SEARCH_STRUCT search;    char buf[CMS_CONFIG_LINELEN];    char buf2[CMS_CONFIG_LINELEN];    char *default_ptr = 0;    if (0 == bufname || 0 == procname || 0 == filename) {	return -1;    }    rcs_print_debug(PRINT_CMS_CONFIG_INFO, "cms_config arguments:\n");    rcs_print_debug(PRINT_CMS_CONFIG_INFO, "bufname = %s\n", bufname);    rcs_print_debug(PRINT_CMS_CONFIG_INFO, "procname = %s\n", procname);    rcs_print_debug(PRINT_CMS_CONFIG_INFO, "filename = %s\n", filename);    search.error_type = CONFIG_SEARCH_ERROR_NOT_SET;    search.bufline_found = 0;    search.bufline_number = -1;    search.procline_found = 0;    search.procline_number = -1;    search.bufname = bufname;    search.bufname_for_procline = bufname;    search.procname = procname;    search.filename = filename;    find_proc_and_buffer_lines(&search);    if (NO_PROCESS_LINE == search.error_type) {	search.bufname_for_procline = "default";	find_proc_and_buffer_lines(&search);	if (search.error_type == CONFIG_SEARCH_OK) {	    default_ptr = 0;	    strncpy(buf, search.proc_line, CMS_CONFIG_LINELEN);	    default_ptr = strstr(buf, "default");	    if (default_ptr) {		strcpy(buf2, default_ptr + 7);		strcpy(default_ptr, bufname);		default_ptr += strlen(bufname);		strcpy(default_ptr, buf2);		strncpy(search.proc_line, buf, CMS_CONFIG_LINELEN);	    }	    strcat(search.proc_line, " defaultbuf");	}    }    if (NO_PROCESS_LINE == search.error_type) {	search.bufname_for_procline = "default";	search.procname = "default";	find_proc_and_buffer_lines(&search);	if (search.error_type == CONFIG_SEARCH_OK) {	    strncpy(buf, search.proc_line, CMS_CONFIG_LINELEN);	    default_ptr = strstr(buf, "default");	    if (default_ptr) {		strcpy(buf2, default_ptr + 7);		strcpy(default_ptr, procname);		default_ptr += strlen(procname);		strcpy(default_ptr, buf2);		default_ptr = strstr(buf, "default");	    }	    if (default_ptr) {		strcpy(buf2, default_ptr + 7);		strcpy(default_ptr, bufname);

⌨️ 快捷键说明

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