parse.c (2207B)
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20#include "config.h" 21 22#include <guacamole/timestamp.h> 23 24#include <errno.h> 25#include <limits.h> 26#include <string.h> 27#include <stdlib.h> 28 29int guacenc_parse_int(char* arg, int* i) { 30 31 char* end; 32 33 /* Parse string as an integer */ 34 errno = 0; 35 long int value = strtol(arg, &end, 10); 36 37 /* Ignore number if invalid / non-positive */ 38 if (errno != 0 || value <= 0 || value > INT_MAX || *end != '\0') 39 return 1; 40 41 /* Store value */ 42 *i = value; 43 44 /* Parsing successful */ 45 return 0; 46 47} 48 49int guacenc_parse_dimensions(char* arg, int* width, int* height) { 50 51 /* Locate the 'x' within the dimensions string */ 52 char* x = strchr(arg, 'x'); 53 if (x == NULL) 54 return 1; 55 56 /* Replace 'x' with a null terminator */ 57 *x = '\0'; 58 59 /* Parse width and height */ 60 int w, h; 61 if (guacenc_parse_int(arg, &w) || guacenc_parse_int(x+1, &h)) 62 return 1; 63 64 /* Width and height are both valid */ 65 *width = w; 66 *height = h; 67 68 return 0; 69 70} 71 72guac_timestamp guacenc_parse_timestamp(const char* str) { 73 74 int sign = 1; 75 int64_t num = 0; 76 77 for (; *str != '\0'; str++) { 78 79 /* Flip sign for each '-' encountered */ 80 if (*str == '-') 81 sign = -sign; 82 83 /* If not '-', assume the character is a digit */ 84 else 85 num = num * 10 + (*str - '0'); 86 87 } 88 89 return (guac_timestamp) (num * sign); 90 91} 92