|
| 1 | +/* |
| 2 | + * Copyright 2020 Comcast Cable Communications Management, LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#include <errno.h> |
| 17 | +#include <string.h> |
| 18 | +#include <stdlib.h> |
| 19 | +#include "webcfg_log.h" |
| 20 | +#include "webcfg_metadata.h" |
| 21 | +#include "webcfg_multipart.h" |
| 22 | + |
| 23 | +/*----------------------------------------------------------------------------*/ |
| 24 | +/* Macros */ |
| 25 | +/*----------------------------------------------------------------------------*/ |
| 26 | +#define MAXCHAR 1024 |
| 27 | +/*----------------------------------------------------------------------------*/ |
| 28 | +/* Data Structures */ |
| 29 | +/*----------------------------------------------------------------------------*/ |
| 30 | +typedef struct SubDocSupportMap |
| 31 | +{ |
| 32 | + char name[256];//portforwarding or wlan |
| 33 | + char support[8];//true or false; |
| 34 | + struct SubDocSupportMap *next; |
| 35 | +}SubDocSupportMap_t; |
| 36 | + |
| 37 | +/*----------------------------------------------------------------------------*/ |
| 38 | +/* File Scoped Variables */ |
| 39 | +/*----------------------------------------------------------------------------*/ |
| 40 | +static char * supported_bits = NULL; |
| 41 | +static char * supported_version = NULL; |
| 42 | +SubDocSupportMap_t *g_sdInfoHead = NULL; |
| 43 | +SubDocSupportMap_t *g_sdInfoTail = NULL; |
| 44 | +/*----------------------------------------------------------------------------*/ |
| 45 | +/* Function Prototypes */ |
| 46 | +/*----------------------------------------------------------------------------*/ |
| 47 | +void displaystruct(); |
| 48 | +SubDocSupportMap_t * get_global_sdInfoHead(void); |
| 49 | +SubDocSupportMap_t * get_global_sdInfoTail(void); |
| 50 | +/*----------------------------------------------------------------------------*/ |
| 51 | +/* External Functions */ |
| 52 | +/*----------------------------------------------------------------------------*/ |
| 53 | + |
| 54 | + |
| 55 | +void initWebcfgProperties(char * filename) |
| 56 | +{ |
| 57 | + FILE *fp = NULL; |
| 58 | + char str[MAXCHAR] = {'\0'}; |
| 59 | + //For WEBCONFIG_SUBDOC_MAP parsing |
| 60 | + char *p; |
| 61 | + char *token; |
| 62 | + |
| 63 | + WebcfgDebug("webcfg properties file path is %s\n", filename); |
| 64 | + fp = fopen(filename,"r"); |
| 65 | + |
| 66 | + if (fp == NULL) |
| 67 | + { |
| 68 | + WebcfgError("Failed to open file %s\n", filename); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + while (fgets(str, MAXCHAR, fp) != NULL) |
| 73 | + { |
| 74 | + char * value = NULL; |
| 75 | + |
| 76 | + if(NULL != (value = strstr(str,"WEBCONFIG_SUPPORTED_DOCS_BIT="))) |
| 77 | + { |
| 78 | + WebcfgDebug("The value stored is %s\n", str); |
| 79 | + value = value + strlen("WEBCONFIG_SUPPORTED_DOCS_BIT="); |
| 80 | + value[strlen(value)-1] = '\0'; |
| 81 | + setsupportedDocs(value); |
| 82 | + value = NULL; |
| 83 | + } |
| 84 | + |
| 85 | + if(NULL != (value =strstr(str,"WEBCONFIG_DOC_SCHEMA_VERSION"))) |
| 86 | + { |
| 87 | + WebcfgDebug("The value stored is %s\n", str); |
| 88 | + value = value + strlen("WEBCONFIG_DOC_SCHEMA_VERSION="); |
| 89 | + value[strlen(value)-1] = '\0'; |
| 90 | + setsupportedVersion(value); |
| 91 | + value = NULL; |
| 92 | + } |
| 93 | + |
| 94 | + if(strncmp(str,"WEBCONFIG_SUBDOC_MAP",strlen("WEBCONFIG_SUBDOC_MAP")) ==0) |
| 95 | + { |
| 96 | + |
| 97 | + p = str; |
| 98 | + char *tok = NULL; |
| 99 | + WebcfgDebug("The value of tok is %s\n", tok); |
| 100 | + tok = strtok_r(p, " =",&p); |
| 101 | + token = strtok_r(p,",",&p); |
| 102 | + while(token!= NULL) |
| 103 | + { |
| 104 | + |
| 105 | + char subdoc[100]; |
| 106 | + char *subtoken; |
| 107 | + SubDocSupportMap_t *sdInfo = NULL; |
| 108 | + strncpy(subdoc,token,(sizeof(subdoc)-1)); |
| 109 | + puts(subdoc); |
| 110 | + sdInfo = (SubDocSupportMap_t *)malloc(sizeof(SubDocSupportMap_t)); |
| 111 | + if( sdInfo==NULL ) |
| 112 | + { |
| 113 | + fclose(fp); |
| 114 | + WebcfgError("Unable to allocate memory"); |
| 115 | + return; |
| 116 | + } |
| 117 | + memset(sdInfo, 0, sizeof(SubDocSupportMap_t)); |
| 118 | + |
| 119 | + subtoken = strtok(subdoc,":");//portforwarding or lan |
| 120 | + |
| 121 | + if(subtoken == NULL) |
| 122 | + { |
| 123 | + fclose(fp); |
| 124 | + WEBCFG_FREE(sdInfo); |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + strncpy(sdInfo->name,subtoken,(sizeof(sdInfo->name)-1)); |
| 129 | + subtoken = strtok(NULL,":");//skip 1st value |
| 130 | + subtoken = strtok(NULL,":");//true or false |
| 131 | + strncpy(sdInfo->support,subtoken,(sizeof(sdInfo->support)-1)); |
| 132 | + token =strtok_r(p,",",&p); |
| 133 | + sdInfo->next = NULL; |
| 134 | + |
| 135 | + if(g_sdInfoTail == NULL) |
| 136 | + { |
| 137 | + g_sdInfoHead = sdInfo; |
| 138 | + g_sdInfoTail = sdInfo; |
| 139 | + } |
| 140 | + else |
| 141 | + { |
| 142 | + SubDocSupportMap_t *temp =NULL; |
| 143 | + temp = get_global_sdInfoTail(); |
| 144 | + temp->next = sdInfo; |
| 145 | + g_sdInfoTail = sdInfo; |
| 146 | + } |
| 147 | + |
| 148 | + } |
| 149 | + |
| 150 | + } |
| 151 | + } |
| 152 | + fclose(fp); |
| 153 | + |
| 154 | + if(g_sdInfoHead != NULL) |
| 155 | + { |
| 156 | + displaystruct(); |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +void setsupportedDocs( char * value) |
| 161 | +{ |
| 162 | + if(value != NULL) |
| 163 | + { |
| 164 | + supported_bits = strdup(value); |
| 165 | + } |
| 166 | + else |
| 167 | + { |
| 168 | + supported_bits = NULL; |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +void setsupportedVersion( char * value) |
| 173 | +{ |
| 174 | + if(value != NULL) |
| 175 | + { |
| 176 | + supported_version = strdup(value); |
| 177 | + } |
| 178 | + else |
| 179 | + { |
| 180 | + supported_version = NULL; |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +char * getsupportedDocs() |
| 185 | +{ |
| 186 | + WebcfgDebug("The value in supportedbits get is %s\n",supported_bits); |
| 187 | + return supported_bits; |
| 188 | +} |
| 189 | + |
| 190 | +char * getsupportedVersion() |
| 191 | +{ |
| 192 | + WebcfgDebug("The value in supportedversion get is %s\n",supported_version); |
| 193 | + return supported_version; |
| 194 | +} |
| 195 | + |
| 196 | +WEBCFG_STATUS isSubDocSupported(char *subDoc) |
| 197 | +{ |
| 198 | + |
| 199 | + SubDocSupportMap_t *sd = NULL; |
| 200 | + |
| 201 | + sd = get_global_sdInfoHead(); |
| 202 | + |
| 203 | + while(sd != NULL) |
| 204 | + { |
| 205 | + if(strncmp(sd->name, subDoc, strlen(subDoc)) == 0) |
| 206 | + { |
| 207 | + WebcfgDebug("The subdoc %s is present\n",sd->name); |
| 208 | + if(strncmp(sd->support, "true", strlen("true")) == 0) |
| 209 | + { |
| 210 | + WebcfgInfo("%s is supported\n",subDoc); |
| 211 | + return WEBCFG_SUCCESS; |
| 212 | + |
| 213 | + } |
| 214 | + else |
| 215 | + { |
| 216 | + WebcfgInfo("%s is not supported\n",subDoc); |
| 217 | + return WEBCFG_FAILURE; |
| 218 | + } |
| 219 | + } |
| 220 | + sd = sd->next; |
| 221 | + |
| 222 | + } |
| 223 | + WebcfgError("Supported doc bit not found for %s\n",subDoc); |
| 224 | + return WEBCFG_FAILURE; |
| 225 | +} |
| 226 | + |
| 227 | +/*----------------------------------------------------------------------------*/ |
| 228 | +/* Internal functions */ |
| 229 | +/*----------------------------------------------------------------------------*/ |
| 230 | +SubDocSupportMap_t * get_global_sdInfoHead(void) |
| 231 | +{ |
| 232 | + SubDocSupportMap_t *tmp = NULL; |
| 233 | + tmp = g_sdInfoHead; |
| 234 | + return tmp; |
| 235 | +} |
| 236 | + |
| 237 | +SubDocSupportMap_t * get_global_sdInfoTail(void) |
| 238 | +{ |
| 239 | + SubDocSupportMap_t *tmp = NULL; |
| 240 | + tmp = g_sdInfoTail; |
| 241 | + return tmp; |
| 242 | +} |
| 243 | + |
| 244 | +void displaystruct() |
| 245 | +{ |
| 246 | + SubDocSupportMap_t *temp =NULL; |
| 247 | + temp = get_global_sdInfoHead(); |
| 248 | + while(temp != NULL) |
| 249 | + { |
| 250 | + WebcfgDebug(" %s %s\n",temp->name,temp->support); |
| 251 | + temp=temp->next; |
| 252 | + } |
| 253 | +} |
0 commit comments