intl/icu/source/tools/toolutil/flagparser.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/tools/toolutil/flagparser.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,171 @@
     1.4 +/******************************************************************************
     1.5 + *   Copyright (C) 2009-2012, International Business Machines
     1.6 + *   Corporation and others.  All Rights Reserved.
     1.7 + *******************************************************************************
     1.8 + */
     1.9 +
    1.10 +#include "flagparser.h"
    1.11 +#include "filestrm.h"
    1.12 +#include "cstring.h"
    1.13 +#include "cmemory.h"
    1.14 +
    1.15 +#define DEFAULT_BUFFER_SIZE 512
    1.16 +
    1.17 +static int32_t currentBufferSize = DEFAULT_BUFFER_SIZE;
    1.18 +
    1.19 +static int32_t extractFlag(char* buffer, int32_t bufferSize, char* flag, int32_t flagSize, const char ** flagNames, int32_t numOfFlags, UErrorCode *status);
    1.20 +static int32_t getFlagOffset(const char *buffer, int32_t bufferSize);
    1.21 +
    1.22 +/*
    1.23 + * Opens the given fileName and reads in the information storing the data in flagBuffer.
    1.24 + */
    1.25 +U_CAPI int32_t U_EXPORT2
    1.26 +parseFlagsFile(const char *fileName, char **flagBuffer, int32_t flagBufferSize, const char ** flagNames, int32_t numOfFlags, UErrorCode *status) {
    1.27 +    char* buffer = uprv_malloc(sizeof(char) * currentBufferSize);
    1.28 +    char* tmpFlagBuffer = uprv_malloc(sizeof(char) * flagBufferSize);
    1.29 +    UBool allocateMoreSpace = FALSE;
    1.30 +    int32_t idx, i;
    1.31 +    int32_t result = 0;
    1.32 +
    1.33 +    FileStream *f = T_FileStream_open(fileName, "r");
    1.34 +    if (f == NULL) {
    1.35 +        *status = U_FILE_ACCESS_ERROR;
    1.36 +        return -1;
    1.37 +    }
    1.38 +
    1.39 +    if (buffer == NULL) {
    1.40 +        *status = U_MEMORY_ALLOCATION_ERROR;
    1.41 +        return -1;
    1.42 +    }
    1.43 +
    1.44 +    do {
    1.45 +        if (allocateMoreSpace) {
    1.46 +            allocateMoreSpace = FALSE;
    1.47 +            currentBufferSize *= 2;
    1.48 +            uprv_free(buffer);
    1.49 +            buffer = uprv_malloc(sizeof(char) * currentBufferSize);
    1.50 +            if (buffer == NULL) {
    1.51 +                uprv_free(tmpFlagBuffer);
    1.52 +                *status = U_MEMORY_ALLOCATION_ERROR;
    1.53 +                return -1;
    1.54 +            }
    1.55 +        }
    1.56 +        for (i = 0; i < numOfFlags;) {
    1.57 +            if (T_FileStream_readLine(f, buffer, currentBufferSize) == NULL) {
    1.58 +                /* End of file reached. */
    1.59 +                break;
    1.60 +            }
    1.61 +            if (buffer[0] == '#') {
    1.62 +                continue;
    1.63 +            }
    1.64 +
    1.65 +            if (uprv_strlen(buffer) == (currentBufferSize - 1) && buffer[currentBufferSize-2] != '\n') {
    1.66 +                /* Allocate more space for buffer if it didnot read the entrire line */
    1.67 +                allocateMoreSpace = TRUE;
    1.68 +                T_FileStream_rewind(f);
    1.69 +                break;
    1.70 +            } else {
    1.71 +                idx = extractFlag(buffer, currentBufferSize, tmpFlagBuffer, flagBufferSize, flagNames, numOfFlags, status);
    1.72 +                if (U_FAILURE(*status)) {
    1.73 +                    if (*status == U_BUFFER_OVERFLOW_ERROR) {
    1.74 +                        result = currentBufferSize;
    1.75 +                    } else {
    1.76 +                        result = -1;
    1.77 +                    }
    1.78 +                    break;
    1.79 +                } else {
    1.80 +                    if (flagNames != NULL) {
    1.81 +                        if (idx >= 0) {
    1.82 +                            uprv_strcpy(flagBuffer[idx], tmpFlagBuffer);
    1.83 +                        } else {
    1.84 +                            /* No match found.  Skip it. */
    1.85 +                            continue;
    1.86 +                        }
    1.87 +                    } else {
    1.88 +                        uprv_strcpy(flagBuffer[i++], tmpFlagBuffer);
    1.89 +                    }
    1.90 +                }
    1.91 +            }
    1.92 +        }
    1.93 +    } while (allocateMoreSpace && U_SUCCESS(*status));
    1.94 +
    1.95 +    uprv_free(tmpFlagBuffer);
    1.96 +    uprv_free(buffer);
    1.97 +
    1.98 +    T_FileStream_close(f);
    1.99 +
   1.100 +    if (U_SUCCESS(*status) && result == 0) {
   1.101 +        currentBufferSize = DEFAULT_BUFFER_SIZE;
   1.102 +    }
   1.103 +
   1.104 +    return result;
   1.105 +}
   1.106 +
   1.107 +
   1.108 +/*
   1.109 + * Extract the setting after the '=' and store it in flag excluding the newline character.
   1.110 + */
   1.111 +static int32_t extractFlag(char* buffer, int32_t bufferSize, char* flag, int32_t flagSize, const char **flagNames, int32_t numOfFlags, UErrorCode *status) {
   1.112 +    int32_t i, idx = -1;
   1.113 +    char *pBuffer;
   1.114 +    int32_t offset=0;
   1.115 +    UBool bufferWritten = FALSE;
   1.116 +
   1.117 +    if (buffer[0] != 0) {
   1.118 +        /* Get the offset (i.e. position after the '=') */
   1.119 +        offset = getFlagOffset(buffer, bufferSize);
   1.120 +        pBuffer = buffer+offset;
   1.121 +        for(i = 0;;i++) {
   1.122 +            if (i >= flagSize) {
   1.123 +                *status = U_BUFFER_OVERFLOW_ERROR;
   1.124 +                return -1;
   1.125 +            }
   1.126 +            if (pBuffer[i+1] == 0) {
   1.127 +                /* Indicates a new line character. End here. */
   1.128 +                flag[i] = 0;
   1.129 +                break;
   1.130 +            }
   1.131 +
   1.132 +            flag[i] = pBuffer[i];
   1.133 +            if (i == 0) {
   1.134 +                bufferWritten = TRUE;
   1.135 +            }
   1.136 +        }
   1.137 +    }
   1.138 +
   1.139 +    if (!bufferWritten) {
   1.140 +        flag[0] = 0;
   1.141 +    }
   1.142 +
   1.143 +    if (flagNames != NULL && offset>0) {
   1.144 +        offset--;  /* Move offset back 1 because of '='*/
   1.145 +        for (i = 0; i < numOfFlags; i++) {
   1.146 +            if (uprv_strncmp(buffer, flagNames[i], offset) == 0) {
   1.147 +                idx = i;
   1.148 +                break;
   1.149 +            }
   1.150 +        }
   1.151 +    }
   1.152 +
   1.153 +    return idx;
   1.154 +}
   1.155 +
   1.156 +/*
   1.157 + * Get the position after the '=' character.
   1.158 + */
   1.159 +static int32_t getFlagOffset(const char *buffer, int32_t bufferSize) {
   1.160 +    int32_t offset = 0;
   1.161 +
   1.162 +    for (offset = 0; offset < bufferSize;offset++) {
   1.163 +        if (buffer[offset] == '=') {
   1.164 +            offset++;
   1.165 +            break;
   1.166 +        }
   1.167 +    }
   1.168 +
   1.169 +    if (offset == bufferSize || (offset - 1) == bufferSize) {
   1.170 +        offset = 0;
   1.171 +    }
   1.172 +
   1.173 +    return offset;
   1.174 +}

mercurial