Home | History | Annotate | Download | only in src
      1 /* ------------------------------------------------------------------
      2  * Copyright (C) 1998-2009 PacketVideo
      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
     13  * express or implied.
     14  * See the License for the specific language governing permissions
     15  * and limitations under the License.
     16  * -------------------------------------------------------------------
     17  */
     18 #include "oscl_configfile_list.h"
     19 #include "oscl_string.h"
     20 #include "oscl_file_io.h"
     21 #include "oscl_file_find.h"
     22 #include "oscl_file_types.h"
     23 #include "pvlogger.h"
     24 #include "oscl_uuid.h"
     25 
     26 #define CONFIG_FILE_EXTENSION "*.cfg"
     27 #define OSCL_NUMBER_OF_CONFIG_FILES 16
     28 
     29 OSCL_EXPORT_REF OsclLibStatus OsclLibraryUtil::SetupLibraryEnv(const char* aEnvName, const char* aEnvValue)
     30 {
     31     // currently only MSWIN is supported;
     32     // Linux is not implemented since its library search path needs to be set at compilation time
     33     OsclLibStatus aStatus = OsclLibSuccess;
     34 
     35     OSCL_UNUSED_ARG(aEnvName);
     36     OSCL_UNUSED_ARG(aEnvValue);
     37     aStatus = OsclLibNotSupported;
     38     return aStatus;
     39 }
     40 
     41 
     42 OSCL_EXPORT_REF OsclConfigFileList::OsclConfigFileList()
     43 {
     44     ipLogger = PVLogger::GetLoggerObject("oscllib");
     45 #if OSCL_LIBRARY_PERF_LOGGING
     46     iDiagnosticsLogger = PVLogger::GetLoggerObject("pvplayerdiagnostics.oscllib.osclconfigfilelist");
     47     iCfgFileNum = 0;
     48 #endif
     49     int32 err = 0;
     50     OSCL_TRY(err,
     51              iCfgList.reserve(OSCL_NUMBER_OF_CONFIG_FILES);
     52             );
     53     if (err)
     54     {
     55         iCfgList.clear();
     56         OSCL_LEAVE(err);
     57     }
     58 }
     59 
     60 OSCL_EXPORT_REF OsclConfigFileList::~OsclConfigFileList()
     61 {
     62     ipLogger = NULL;
     63 #if OSCL_LIBRARY_PERF_LOGGING
     64     iDiagnosticsLogger = NULL;
     65 #endif
     66     iCfgList.clear();
     67 }
     68 
     69 // This method finds all files with extention .cfg in given directory
     70 // using oscl_file_find and stores in a vector in iCfgList
     71 // each cfg in iCfgList stored with whole pathname
     72 // @Return: OsclLibSuccess if any config file found, OsclLibNotFound if not any, OsclLibFail otherwise
     73 
     74 OSCL_EXPORT_REF OsclLibStatus OsclConfigFileList::Populate(const OSCL_String& aConfigFileDir, OsclConfigFileList::SortType aSortType)
     75 {
     76     PVLOGGER_LOGMSG(PVLOGMSG_INST_LLDBG, ipLogger, PVLOGMSG_DEBUG,
     77                     (0, "OsclConfigFileList::Populate '%s' IN", aConfigFileDir.get_cstr()));
     78 #if OSCL_LIBRARY_PERF_LOGGING
     79     TICK starttime;
     80     SET_TICK(starttime);
     81     iCfgFileNum = 0;
     82 #endif
     83     Oscl_FileFind aCfgFind;
     84     OsclLibStatus status = OsclLibSuccess;
     85     char* strbuf = (char*)OSCL_MALLOC(OSCL_IO_FILENAME_MAXLEN * sizeof(char));
     86     if (NULL == strbuf)
     87         return OsclLibOutOfMemory;
     88     if (NULL == aCfgFind.FindFirst(aConfigFileDir.get_str(), CONFIG_FILE_EXTENSION, strbuf, OSCL_IO_FILENAME_MAXLEN))
     89     {
     90         OSCL_FREE(strbuf);
     91         strbuf = NULL;
     92         if (Oscl_FileFind::E_NO_MATCH == aCfgFind.GetLastError())
     93         {
     94             status = OsclLibNotFound;
     95         }
     96         else
     97         {
     98             iCfgList.clear();
     99             return OsclLibFail;
    100         }
    101     }
    102     else
    103     {
    104         //oscl_file_find return file with pathname.
    105         iCfgList.push_front(strbuf);
    106 #if OSCL_LIBRARY_PERF_LOGGING
    107         iCfgFileNum++;
    108 #endif
    109         // found first config file, continue until Oscl_FileFind::E_NO_MATCH
    110         while (aCfgFind.FindNext(strbuf, OSCL_IO_FILENAME_MAXLEN) && Oscl_FileFind::E_NO_MATCH != aCfgFind.GetLastError())
    111         {
    112             iCfgList.push_back(strbuf);
    113 #if OSCL_LIBRARY_PERF_LOGGING
    114             iCfgFileNum++;
    115 #endif
    116         }
    117 #if OSCL_LIBRARY_PERF_LOGGING
    118         uint32 difftime;
    119         DIFF_TICK(starttime, difftime);
    120         PVLOGGER_LOGMSG(PVLOGMSG_INST_PROF, iDiagnosticsLogger, PVLOGMSG_INFO,
    121                         (0, "OsclConfigFileList::Populate - Searching path %s ...", aConfigFileDir.get_cstr()));
    122         PVLOGGER_LOGMSG(PVLOGMSG_INST_PROF, iDiagnosticsLogger, PVLOGMSG_INFO,
    123                         (0, "                                   Time taken = %d ticks", difftime));
    124         PVLOGGER_LOGMSG(PVLOGMSG_INST_PROF, iDiagnosticsLogger, PVLOGMSG_INFO,
    125                         (0, "                                   Number of config files found = %d", iCfgFileNum));
    126 #endif
    127     }
    128     OSCL_FREE(strbuf);
    129     // error if there's none config file found and loaded in the vector,
    130     if (iCfgList.size() <= 0)
    131     {
    132         PVLOGGER_LOGMSG(PVLOGMSG_INST_REL, ipLogger, PVLOGMSG_WARNING,
    133                         (0, "OsclConfigFileList::Populate, Didn't find any config file in %s", aConfigFileDir.get_str()));
    134         status = OsclLibNotFound;
    135     }
    136     // Sort the list if requested
    137     else if (aSortType != OsclConfigFileList::ENoSort)
    138     {
    139         Sort();
    140     }
    141 
    142     return status;
    143 }
    144 
    145 OSCL_EXPORT_REF uint32 OsclConfigFileList::Size()
    146 {
    147     return iCfgList.size();
    148 }
    149 
    150 OSCL_EXPORT_REF const OSCL_String& OsclConfigFileList::GetConfigfileAt(uint32 n)
    151 {
    152     return iCfgList[n];
    153 }
    154 
    155 void OsclConfigFileList::Sort()
    156 {
    157     uint32 numConfigs = Size();
    158     uint32 ii = 0;
    159     bool swapped = false;
    160 
    161     // Bubblesort iCfgList - As long as there aren't very many config files
    162     // in the list, efficiency shouldn't be an issue. If the list gets to be
    163     // too large (100+) then a new sort algorithm should be used.
    164     do
    165     {
    166         swapped = false;
    167         for (ii = 0; ii < numConfigs - 1; ii++)
    168         {
    169             if (oscl_strncmp(iCfgList[ii].get_cstr(),
    170                              iCfgList[ii + 1].get_cstr(),
    171                              oscl_strlen(iCfgList[ii].get_cstr())) > 0)
    172             {
    173                 OSCL_StackString<OSCL_IO_FILENAME_MAXLEN> temp = iCfgList[ii];
    174                 iCfgList[ii] = iCfgList[ii + 1];
    175                 iCfgList[ii + 1] = temp;
    176                 swapped = true;
    177             }
    178         }
    179     }
    180     while (swapped);
    181 }
    182 
    183