📄 argstr.c
字号:
/* -*- Mode: C; c-basic-offset:4 ; -*- * * Copyright (C) 1997 University of Chicago. * See COPYRIGHT notice in top-level directory. */#include "mpiimpl.h"#ifdef HAVE_STDIO_H#include <stdio.h>#endif#ifdef HAVE_STDLIB_H#include <stdlib.h>#endif#ifdef HAVE_STRING_H#include <string.h>#endif#ifdef HAVE_MATH_H#include <math.h>#endif/* ctype is needed for isspace and isascii (isspace is only defined for values on which isascii returns true). */#include <ctype.h>#define MPIU_STR_TRUNCATED MPIU_STR_NOMEMstatic int encode_buffer(char *dest, int dest_length, const char *src, int src_length, int *num_encoded){ int num_used; int n = 0; char ch; if (src_length == 0) { if (dest_length > 2) { *dest = MPIU_STR_QUOTE_CHAR; dest++; *dest = MPIU_STR_QUOTE_CHAR; dest++; *dest = '\0'; *num_encoded = 0; return MPIU_STR_SUCCESS; } else { return MPIU_STR_TRUNCATED; } } while (src_length && dest_length) { ch = *src; num_used = MPIU_Snprintf(dest, dest_length, "%02X", (unsigned char)*src); if (num_used < 0) { *num_encoded = n; return MPIU_STR_TRUNCATED; } /*MPIU_DBG_MSG_FMT(OTHER,VERBOSE,(MPIU_DBG_FDEST," %c = %c%c", ch, dest[0], dest[1]));*/ dest += num_used; dest_length -= num_used; src++; n++; src_length--; } *num_encoded = n; return src_length ? MPIU_STR_TRUNCATED : MPIU_STR_SUCCESS;}static int decode_buffer(const char *str, char *dest, int length, int *num_decoded){ char hex[3]; int value; int n = 0; if (str == NULL || dest == NULL || num_decoded == NULL) return MPIU_STR_FAIL; if (length < 1) { *num_decoded = 0; if (*str == '\0') return MPIU_STR_SUCCESS; return MPIU_STR_TRUNCATED; } if (*str == MPIU_STR_QUOTE_CHAR) str++; hex[2] = '\0'; while (*str != '\0' && *str != MPIU_STR_SEPAR_CHAR && *str != MPIU_STR_QUOTE_CHAR && length) { hex[0] = *str; str++; hex[1] = *str; str++; sscanf(hex, "%X", &value); *dest = (char)value; /*MPIU_DBG_MSG_FMT(OTHER,VERBOSE,(MPIU_DBG_FDEST," %s = %c\n", hex, *dest));*/ dest++; n++; length--; } *num_decoded = n; if (length == 0) { if (*str != '\0' && *str != MPIU_STR_SEPAR_CHAR && *str != MPIU_STR_QUOTE_CHAR) return MPIU_STR_TRUNCATED; } return MPIU_STR_SUCCESS;}static const char * first_token(const char *str){ if (str == NULL) return NULL; /* isspace is defined only if isascii is true */ while (/*isascii(*str) && isspace(*str)*/ *str == MPIU_STR_SEPAR_CHAR) str++; if (*str == '\0') return NULL; return str;}static const char * next_token(const char *str){ if (str == NULL) return NULL; str = first_token(str); if (str == NULL) return NULL; if (*str == MPIU_STR_QUOTE_CHAR) { /* move over string */ str++; /* move over the first quote */ if (*str == '\0') return NULL; while (*str != MPIU_STR_QUOTE_CHAR) { /* move until the last quote, ignoring escaped quotes */ if (*str == MPIU_STR_ESCAPE_CHAR) { str++; if (*str == MPIU_STR_QUOTE_CHAR) str++; } else { str++; } if (*str == '\0') return NULL; } str++; /* move over the last quote */ } else { if (*str == MPIU_STR_DELIM_CHAR) { /* move over the DELIM token */ str++; } else { /* move over literal */ while (/*(isascii(*str) && !isspace(*str)) &&*/ *str != MPIU_STR_SEPAR_CHAR && *str != MPIU_STR_DELIM_CHAR && *str != '\0') str++; } } return first_token(str);}static int compare_token(const char *token, const char *str){ if (token == NULL || str == NULL) return -1; if (*token == MPIU_STR_QUOTE_CHAR) { /* compare quoted strings */ token++; /* move over the first quote */ /* compare characters until reaching the end of the string or the end quote character */ for(;;) { if (*token == MPIU_STR_ESCAPE_CHAR) { if (*(token+1) == MPIU_STR_QUOTE_CHAR) { /* move over the escape character if the next character is a quote character */ token++; } if (*token != *str) break; } else { if (*token != *str || *token == MPIU_STR_QUOTE_CHAR) break; } if (*str == '\0') break; token++; str++; } if (*str == '\0' && *token == MPIU_STR_QUOTE_CHAR) return 0; if (*token == MPIU_STR_QUOTE_CHAR) return 1; if (*str < *token) return -1; return 1; } /* compare DELIM token */ if (*token == MPIU_STR_DELIM_CHAR) { if (*str == MPIU_STR_DELIM_CHAR) { str++; if (*str == '\0') return 0; return 1; } if (*token < *str) return -1; return 1; } /* compare literals */ while (*token == *str && *str != '\0' && *token != MPIU_STR_DELIM_CHAR && (*token != MPIU_STR_SEPAR_CHAR) ) { token++; str++; } if ( (*str == '\0') && (*token == MPIU_STR_DELIM_CHAR || (*token == MPIU_STR_SEPAR_CHAR) || *token == '\0') ) return 0; if (*token == MPIU_STR_DELIM_CHAR || (*token == MPIU_STR_SEPAR_CHAR) || *token < *str) return -1; return 1;}static int token_copy(const char *token, char *str, int maxlen){ /* check parameters */ if (token == NULL || str == NULL) return MPIU_STR_FAIL; /* check special buffer lengths */ if (maxlen < 1) return MPIU_STR_FAIL; if (maxlen == 1) { *str = '\0'; return (str[0] == '\0') ? MPIU_STR_SUCCESS : MPIU_STR_TRUNCATED; } /* cosy up to the token */ token = first_token(token); if (token == NULL) { *str = '\0'; return MPIU_STR_SUCCESS; } if (*token == MPIU_STR_DELIM_CHAR) { /* copy the special deliminator token */ str[0] = MPIU_STR_DELIM_CHAR; str[1] = '\0'; return MPIU_STR_SUCCESS; } if (*token == MPIU_STR_QUOTE_CHAR) { /* quoted copy */ token++; /* move over the first quote */ do { if (*token == MPIU_STR_ESCAPE_CHAR) { if (*(token+1) == MPIU_STR_QUOTE_CHAR) token++; *str = *token; } else { if (*token == MPIU_STR_QUOTE_CHAR) { *str = '\0'; return MPIU_STR_SUCCESS; } *str = *token; } str++; token++; maxlen--; } while (maxlen); /* we've run out of destination characters so back up and null terminate the string */ str--; *str = '\0'; return MPIU_STR_TRUNCATED; } /* literal copy */ while (*token != MPIU_STR_DELIM_CHAR && (*token != MPIU_STR_SEPAR_CHAR) && *token != '\0' && maxlen) { *str = *token; str++; token++; maxlen--; } if (maxlen) { *str = '\0'; return MPIU_STR_SUCCESS; } str--; *str = '\0'; return MPIU_STR_TRUNCATED;}static void token_hide(char *token){ /* check parameters */ if (token == NULL) return; /* cosy up to the token */ token = (char*)first_token(token); if (token == NULL) return; if (*token == MPIU_STR_DELIM_CHAR) { *token = MPIU_STR_HIDE_CHAR; return; } /* quoted */ if (*token == MPIU_STR_QUOTE_CHAR) { *token = MPIU_STR_HIDE_CHAR; token++; /* move over the first quote */ while (*token != '\0') { if (*token == MPIU_STR_ESCAPE_CHAR) { if (*(token+1) == MPIU_STR_QUOTE_CHAR) { *token = MPIU_STR_HIDE_CHAR; token++; } *token = MPIU_STR_HIDE_CHAR; } else { if (*token == MPIU_STR_QUOTE_CHAR) { *token = MPIU_STR_HIDE_CHAR; return; } *token = MPIU_STR_HIDE_CHAR; } token++; } return; } /* literal */ while (*token != MPIU_STR_DELIM_CHAR && (*token != MPIU_STR_SEPAR_CHAR) && *token != '\0') { *token = MPIU_STR_HIDE_CHAR; token++; }}/*@ MPIU_Str_get_string_arg - Extract an option from a string with a maximum length Input Parameters:+ str - Source string. key - key- maxlen - Maximum total length of 'val' Output Parameter:. val - output string Return value: MPIU_STR_SUCCESS, MPIU_STR_NOMEM, MPIU_STR_FAIL Notes: This routine searches for a "key = value" entry in a string Module: Utility @*/int MPIU_Str_get_string_arg(const char *str, const char *flag, char *val, int maxlen){ if (maxlen < 1) return MPIU_STR_FAIL; /* line up with the first token */ str = first_token(str); if (str == NULL) return MPIU_STR_FAIL; /* This loop will match the first instance of "flag = value" in the string. */ do { if (compare_token(str, flag) == 0) { str = next_token(str); if (compare_token(str, MPIU_STR_DELIM_STR) == 0) { str = next_token(str); if (str == NULL) return MPIU_STR_FAIL; return token_copy(str, val, maxlen); } } else { str = next_token(str); } } while (str); return MPIU_STR_FAIL;}/*@ MPIU_Str_get_binary_arg - Extract an option from a string with a maximum length Input Parameters:+ str - Source string. key - key- maxlen - Maximum total length of 'buffer' Output Parameter:+ buffer - output buffer- out_length - output length Return value: MPIU_STR_SUCCESS, MPIU_STR_NOMEM, MPIU_STR_FAIL Notes: This routine searches for a "key = value" entry in a string and decodes the value back to binary data. The data must have been encoded with MPIU_Str_add_binary_arg. Module: Utility @*/int MPIU_Str_get_binary_arg(const char *str, const char *flag, char *buffer, int maxlen, int *out_length){ if (maxlen < 1) return MPIU_STR_FAIL; /* line up with the first token */ str = first_token(str); if (str == NULL) return MPIU_STR_FAIL; /* This loop will match the first instance of "flag = value" in the string. */ do { if (compare_token(str, flag) == 0) { str = next_token(str); if (compare_token(str, MPIU_STR_DELIM_STR) == 0) { str = next_token(str); if (str == NULL) return MPIU_STR_FAIL; return decode_buffer(str, buffer, maxlen, out_length); } } else { str = next_token(str); } } while (str); return MPIU_STR_FAIL;}/*@ MPIU_Str_hide_string_arg - Over-write the value of an string option with * characters Input Parameters:+ str - input string- key - key Output Parameter:. str - The string data is modified if the key is found in the string Return value: MPIU_STR_SUCCESS, MPIU_STR_FAIL Notes: This routine covers an option in a string converting "key = value" to "key = *****" Module: Utility @*/MPIU_BOOL MPIU_Str_hide_string_arg(char *str, const char *flag){ /* line up with the first token */ str = (char*)first_token(str); if (str == NULL) return MPIU_TRUE; do { if (compare_token(str, flag) == 0) { str = (char*)next_token(str); if (compare_token(str, MPIU_STR_DELIM_STR) == 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -