javacommons/utils/src/javainifileutils.cpp
changeset 21 2a9601315dfc
child 23 98ccebc37403
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  JavaCommonUtils
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <iostream>
       
    20 #include <sstream>
       
    21 #include <vector>
       
    22 
       
    23 #include <string.h>
       
    24 
       
    25 #include "logger.h"
       
    26 #include "javainifileutils.h"
       
    27 #include "exceptionbase.h"
       
    28 
       
    29 using namespace java::util;
       
    30 
       
    31 
       
    32 OS_EXPORT int JavaIniFileUtils::getProperty(const std::string& property)
       
    33 {
       
    34 #ifdef RD_JAVA_INI_FILE_ACCESS_IN_USE
       
    35     FileContent fileContent = readFileContent();
       
    36     int result = -1;
       
    37 
       
    38     const char* position = getPosition(fileContent, property);
       
    39     if (position)
       
    40     {
       
    41         result = *position - 48;
       
    42 //        ELOG1(EUtils, "Res: %d", result);
       
    43     }
       
    44     return result;
       
    45 #else // RD_JAVA_INI_FILE_ACCESS_IN_USE
       
    46     int res = 0;
       
    47     if (property == PREWARM)
       
    48     {
       
    49         res = 0;
       
    50     }
       
    51     else if (property == UI_PREWARM)
       
    52     {
       
    53         res = 0;
       
    54     }
       
    55 
       
    56     else if (property == PREWARM_CAPTAIN_CONTROL)
       
    57     {
       
    58         res = 0;
       
    59     }
       
    60 
       
    61     else if (property == START_SCREEN_ENABLED)
       
    62     {
       
    63         res = 1;
       
    64     }
       
    65 
       
    66     else if (property == SIMPLE_START_SCREEN)
       
    67     {
       
    68         res = 0;
       
    69     }
       
    70 
       
    71     else if (property == START_SCREEN_SKIN_ENABLED)
       
    72     {
       
    73         res = 1;
       
    74     }
       
    75 
       
    76     else if (property == START_SCREEN_UPDATE_INTERVAL)
       
    77     {
       
    78         res = 8;
       
    79     }
       
    80     return res;
       
    81 
       
    82 #endif // RD_JAVA_INI_FILE_ACCESS_IN_USE
       
    83 
       
    84 }
       
    85 
       
    86 #ifdef RD_JAVA_INI_FILE_ACCESS_IN_USE
       
    87 OS_EXPORT void JavaIniFileUtils::setProperty(const std::string& property,
       
    88         int value)
       
    89 {
       
    90     FileContent fileContent = readFileContent();
       
    91     char* position = getPosition(fileContent, property);
       
    92     if (position)
       
    93     {
       
    94         *position = (char)(value+48);
       
    95 //        ELOG1(EUtils, "Res: %s", fileContent.getContent());
       
    96         writeFileContent(fileContent.getContent());
       
    97     }
       
    98     else
       
    99     {
       
   100         const char* origContent =  fileContent.getContent();
       
   101         std::string newContent;
       
   102         if (origContent && strlen(origContent) > 0)
       
   103         {
       
   104             newContent += origContent;
       
   105             newContent += (char)10;
       
   106         }
       
   107         newContent += property;
       
   108         newContent += ": ";
       
   109         newContent += (char)(value+48);
       
   110         writeFileContent(newContent.c_str());
       
   111     }
       
   112 }
       
   113 #else // RD_JAVA_INI_FILE_ACCESS_IN_USE
       
   114 OS_EXPORT void JavaIniFileUtils::setProperty(const std::string& /*property*/,
       
   115         int /*value*/)
       
   116 {
       
   117 }
       
   118 #endif // RD_JAVA_INI_FILE_ACCESS_IN_USE
       
   119 
       
   120 
       
   121 char* JavaIniFileUtils::getPosition(FileContent& content, const std::string& property)
       
   122 {
       
   123     const char* data = content.getContent();
       
   124     if (data)
       
   125     {
       
   126         char* position = strstr(data, property.c_str());
       
   127         if (position)
       
   128         {
       
   129             position += property.length()+2;
       
   130 
       
   131             return position;
       
   132         }
       
   133     }
       
   134     return 0;
       
   135 }
       
   136 
       
   137 FileContent JavaIniFileUtils::readFileContent(bool create)
       
   138 {
       
   139     const char* fileName = "c:\\java\\javaini.txt";
       
   140     char*   data = 0;
       
   141     FILE*   iniFile;
       
   142     long    len = 0;
       
   143     iniFile = fopen(fileName, "r");
       
   144     if (iniFile == 0 && create)
       
   145     {
       
   146         iniFile = fopen(fileName, "a");
       
   147     }
       
   148     if (iniFile != 0)
       
   149     {
       
   150         fseek(iniFile, 0L, SEEK_END);
       
   151         len = ftell(iniFile);
       
   152         if (len < 0)
       
   153         {
       
   154             // error, return NULL content
       
   155             fclose(iniFile);
       
   156             return FileContent(data);
       
   157         }
       
   158         rewind(iniFile);
       
   159         data = new char[len+1];
       
   160         data[len] = 0;
       
   161         if (data != 0)
       
   162         {
       
   163             fread(data, sizeof(char), len, iniFile);
       
   164         }
       
   165         fclose(iniFile);
       
   166     }
       
   167     return FileContent(data);
       
   168 }
       
   169 
       
   170 void JavaIniFileUtils::writeFileContent(const char* content)
       
   171 {
       
   172     FILE*   iniFile;
       
   173     iniFile = fopen("c:\\java\\javaini.txt", "w");
       
   174     if (iniFile != 0)
       
   175     {
       
   176         fwrite(content, 1, strlen(content), iniFile);
       
   177         fclose(iniFile);
       
   178     }
       
   179 }