Example #1
0
int
locfile_read (struct localedef_t *result, const struct charmap_t *charmap)
{
    const char *filename = result->name;
    const char *repertoire_name = result->repertoire_name;
    int locale_mask = result->needed & ~result->avail;
    struct linereader *ldfile;
    int not_here = ALL_LOCALES;

    /* If no repertoire name was specified use the global one.  */
    if (repertoire_name == NULL)
        repertoire_name = repertoire_global;

    /* Open the locale definition file.  */
    ldfile = lr_open (filename, locfile_hash);
    if (ldfile == NULL)
    {
        if (filename != NULL && filename[0] != '/')
        {
            char *i18npath = getenv ("I18NPATH");
            if (i18npath != NULL && *i18npath != '\0')
            {
                const size_t pathlen = strlen (i18npath);
                char i18npathbuf[pathlen + 1];
                char path[strlen (filename) + 1 + pathlen
                          + sizeof ("/locales/") - 1];
                char *next;
                i18npath = memcpy (i18npathbuf, i18npath, pathlen + 1);

                while (ldfile == NULL
                        && (next = strsep (&i18npath, ":")) != NULL)
                {
                    stpcpy (stpcpy (stpcpy (path, next), "/locales/"), filename);

                    ldfile = lr_open (path, locfile_hash);

                    if (ldfile == NULL)
                    {
                        stpcpy (stpcpy (path, next), filename);

                        ldfile = lr_open (path, locfile_hash);
                    }
                }
            }

            /* Test in the default directory.  */
            if (ldfile == NULL)
            {
                char path[strlen (filename) + 1 + sizeof (LOCSRCDIR)];

                stpcpy (stpcpy (stpcpy (path, LOCSRCDIR), "/"), filename);
                ldfile = lr_open (path, locfile_hash);
            }
        }

        if (ldfile == NULL)
            return 1;
    }

    /* Parse locale definition file and store result in RESULT.  */
    while (1)
    {
        struct token *now = lr_token (ldfile, charmap, NULL, NULL, verbose);
        enum token_t nowtok = now->tok;
        struct token *arg;

        if (nowtok == tok_eof)
            break;

        if (nowtok == tok_eol)
            /* Ignore empty lines.  */
            continue;

        switch (nowtok)
        {
        case tok_escape_char:
        case tok_comment_char:
            /* We need an argument.  */
            arg = lr_token (ldfile, charmap, NULL, NULL, verbose);

            if (arg->tok != tok_ident)
            {
                SYNTAX_ERROR (_("bad argument"));
                continue;
            }

            if (arg->val.str.lenmb != 1)
            {
                lr_error (ldfile, _("\
argument to `%s' must be a single character"),
                          nowtok == tok_escape_char
                          ? "escape_char" : "comment_char");

                lr_ignore_rest (ldfile, 0);
                continue;
            }

            if (nowtok == tok_escape_char)
                ldfile->escape_char = *arg->val.str.startmb;
            else
                ldfile->comment_char = *arg->val.str.startmb;
            break;

        case tok_repertoiremap:
            /* We need an argument.  */
            arg = lr_token (ldfile, charmap, NULL, NULL, verbose);

            if (arg->tok != tok_ident)
            {
                SYNTAX_ERROR (_("bad argument"));
                continue;
            }

            if (repertoire_name == NULL)
            {
                char *newp = alloca (arg->val.str.lenmb + 1);

                *((char *) mempcpy (newp, arg->val.str.startmb,
                                    arg->val.str.lenmb)) = '\0';
                repertoire_name = newp;
            }
            break;

        case tok_lc_ctype:
            ctype_read (ldfile, result, charmap, repertoire_name,
                        (locale_mask & CTYPE_LOCALE) == 0);
            result->avail |= locale_mask & CTYPE_LOCALE;
            not_here ^= CTYPE_LOCALE;
            continue;

        case tok_lc_collate:
            collate_read (ldfile, result, charmap, repertoire_name,
                          (locale_mask & COLLATE_LOCALE) == 0);
            result->avail |= locale_mask & COLLATE_LOCALE;
            not_here ^= COLLATE_LOCALE;
            continue;

        case tok_lc_monetary:
            monetary_read (ldfile, result, charmap, repertoire_name,
                           (locale_mask & MONETARY_LOCALE) == 0);
            result->avail |= locale_mask & MONETARY_LOCALE;
            not_here ^= MONETARY_LOCALE;
            continue;

        case tok_lc_numeric:
            numeric_read (ldfile, result, charmap, repertoire_name,
                          (locale_mask & NUMERIC_LOCALE) == 0);
            result->avail |= locale_mask & NUMERIC_LOCALE;
            not_here ^= NUMERIC_LOCALE;
            continue;

        case tok_lc_time:
            time_read (ldfile, result, charmap, repertoire_name,
                       (locale_mask & TIME_LOCALE) == 0);
            result->avail |= locale_mask & TIME_LOCALE;
            not_here ^= TIME_LOCALE;
            continue;

        case tok_lc_messages:
            messages_read (ldfile, result, charmap, repertoire_name,
                           (locale_mask & MESSAGES_LOCALE) == 0);
            result->avail |= locale_mask & MESSAGES_LOCALE;
            not_here ^= MESSAGES_LOCALE;
            continue;

        case tok_lc_paper:
            paper_read (ldfile, result, charmap, repertoire_name,
                        (locale_mask & PAPER_LOCALE) == 0);
            result->avail |= locale_mask & PAPER_LOCALE;
            not_here ^= PAPER_LOCALE;
            continue;

        case tok_lc_name:
            name_read (ldfile, result, charmap, repertoire_name,
                       (locale_mask & NAME_LOCALE) == 0);
            result->avail |= locale_mask & NAME_LOCALE;
            not_here ^= NAME_LOCALE;
            continue;

        case tok_lc_address:
            address_read (ldfile, result, charmap, repertoire_name,
                          (locale_mask & ADDRESS_LOCALE) == 0);
            result->avail |= locale_mask & ADDRESS_LOCALE;
            not_here ^= ADDRESS_LOCALE;
            continue;

        case tok_lc_telephone:
            telephone_read (ldfile, result, charmap, repertoire_name,
                            (locale_mask & TELEPHONE_LOCALE) == 0);
            result->avail |= locale_mask & TELEPHONE_LOCALE;
            not_here ^= TELEPHONE_LOCALE;
            continue;

        case tok_lc_measurement:
            measurement_read (ldfile, result, charmap, repertoire_name,
                              (locale_mask & MEASUREMENT_LOCALE) == 0);
            result->avail |= locale_mask & MEASUREMENT_LOCALE;
            not_here ^= MEASUREMENT_LOCALE;
            continue;

        case tok_lc_identification:
            identification_read (ldfile, result, charmap, repertoire_name,
                                 (locale_mask & IDENTIFICATION_LOCALE) == 0);
            result->avail |= locale_mask & IDENTIFICATION_LOCALE;
            not_here ^= IDENTIFICATION_LOCALE;
            continue;

        default:
            SYNTAX_ERROR (_("\
syntax error: not inside a locale definition section"));
            continue;
        }

        /* The rest of the line must be empty.  */
        lr_ignore_rest (ldfile, 1);
    }
Example #2
0
//----------------------------------------
int32_t CMission::LoadRefMission(bool searchByID /* = true */ )
{
    int32_t result = BRATHL_SUCCESS;
  
    const uint32_t MAX_LINE_LEN = 255;
    char line[MAX_LINE_LEN+1];
  
    brathl_mission id = 0;
    char name[CMission::m_maxLenName+1];
    int32_t cycle  = 0;
    int32_t pass   = 0;
    double julian  = 0.0;
    double repeat  = 0.0;
    int32_t nbPass = 0;
    bool bFoundInFile = false;
  
    CMission missionSave = *this;
  
    std::string refFilePathName = CTools::FindDataFile( CMission::m_refFileName );
    if (refFilePathName == "")
    {
        if (m_printWarnings)
        {
            printf( "\nWARNING - CMission::LoadRefMission - Unabled to open file '%s' "
                    "\nCheck directory '%s'\n",
                    CMission::m_refFileName,
                    CTools::GetInternalDataDir().c_str());
        }
        return BRATHL_WARNING_OPEN_FILE_REF_FILE;
    }

    // reads file contains conversion parameters
    CFile fileRef(refFilePathName, CFile::modeRead);

    if (fileRef.IsOpen() == false)
    {
        if (m_printWarnings)
        {
            printf( "\nWARNING - CMission::LoadRefMission - Unabled to open file '%s'\n", refFilePathName.c_str() );
        }
        return BRATHL_WARNING_OPEN_FILE_REF_FILE;
    }
  
    int32_t nbFields = EOF;
    int32_t lineNb = 0;
    int32_t size = fileRef.ReadLineData(line, MAX_LINE_LEN);
  
    while (size > 0)
    {
        ++lineNb;
        nbFields = sscanf ( line, "%d %s %d %d %lf %lf %d",
                            &id, name, &cycle, &pass, &julian, &repeat, &nbPass );
    
        if ( nbFields < 7 )
        {
            if (m_printWarnings)
            {
                printf( "\nWARNING - CMission::LoadRefMission - Invalid reference mission file format - line: %d of file: %s \n",
                        lineNb,
                        refFilePathName.c_str() );
            }
            return BRATHL_WARNING_INVALID_REF_FILE_FIELD;
        }
        /*printf("id %d, name %s, cycle %d, pass %d, julian %lf, repead %lf, nbpass %d \n", id, name, cycle, pass, julian, repeat, nbPass);*/
	
        if ( searchByID )
            bFoundInFile = ( id == GetMission() );
        else
        {
            std::string product_type ( GetName() ); // product type/label
            std::string name_read( name );          // mission name at reference file

            product_type = CTools::StringToLower(product_type).substr(0, name_read.size());
            name_read    = CTools::StringToLower(name_read);

            bFoundInFile = ( product_type == name_read );
        }

        if ( bFoundInFile )
        {
        	missionSave = *this;

            // Assign mission conversion parameters
            m_mission     = id;
            m_missionName = name;
            m_cycleRef    = cycle;
            m_passRef     = pass;
            m_repeat      = repeat;
            m_nbPass      = nbPass;
            result        = m_dateRef.SetDateJulian(julian);

            if (result != BRATHL_SUCCESS)
            {
                if (m_printWarnings)
                {
                    printf( "\nWARNING - CMission::LoadRefMission - Invalid reference date - file name %s \n"
                            "mission %s, cycle %d, pass %d, julian %lf, repeat %lf, nb passes %d\n",
                            refFilePathName.c_str(),
                            name, cycle, pass, julian, repeat, nbPass  );
                }
                *this = missionSave;
                result = BRATHL_WARNING_INVALID_REF_FILE_FIELDDATE;
            }
        }

        // Breaks while loop if mission was found
        if ( bFoundInFile ) {  break; }
        else                { size = fileRef.ReadLineData(line, MAX_LINE_LEN); } // reads next line
    }
  
    if ( bFoundInFile == false )
    {
        if ( m_printWarnings )
        {
            if (searchByID)
                printf("\nWARNING - CMission::LoadRefMission - no mission ID=%d found in file: %s \n", GetMission(), refFilePathName.c_str());
            else
                printf("\nWARNING - CMission::LoadRefMission - no mission name='%s' found in file: %s \n", GetName(), refFilePathName.c_str());
        }
        m_missionName = CMission::m_nameUnknown;
        result = BRATHL_ERROR_INVALID_MISSION;
    }
   
    fileRef.Close();
    return result;
}