/**
 * Sets a property key to the specified value.
 *
 * @param props The property set in which to add/modify <tt>key</tt>
 * @param key The key to set
 * @param value The value to set <tt>key</tt> to
 */
static void
setProp(Property** props, const char* key , const char* value) {
    /* Try to find the property in the current pool. */
    Property *p;
    for (p= *props; p; p = p->next) {
        if (strcmp(key, p->key) == 0) {
            midpFree((void*)p->value);
            p->value = midpStrdup(value);
            /*
             * if midpStrdup fails we will just return without setting
             * the value to anything other than NULL
             */
            return;
        }
    }

    /* If the value is not defined, add it now */
    p = (Property*)midpMalloc(sizeof(Property));
    if (NULL != p){
        p->next = *props ;
        p->key = midpStrdup(key);
        p->value = midpStrdup(value);

        if ((p->key == NULL) || (p->value == NULL)) {
            /* do nothing if there is no memory */
            midpFree((void*)p->key);
            midpFree((void*)p->value);
            midpFree(p);
            return;
        }
        *props = p ;
    }
}
Пример #2
0
/**
 * Sets a property key to the specified value. If the key does
 * not exist and <tt>useMalloc<tt> is <tt>KNI_TRUE<tt>, use
 * <tt>midpMalloc()<tt> to make a copy of the contents of
 * <tt>key<tt>. If the key does not exist and <tt>useMalloc<tt>
 * is <tt>KNI_FALSE<tt>, make a copy of the pointer to
 * <tt>key<tt>. If the <tt>key<tt> already exists in the
 * property lists, modify its associated value.
 *
 * @param propertySet The property set in which to add/modify
 *                    <tt>key<tt>
 * @param key The key to set
 * @param value The value to associate with <tt>key<tt>
 * @param useMalloc If <tt>KNI_TRUE<tt>, use <tt>midpMalloc()<tt>
 *                  to make a copy of the contents of <tt>value<tt>,
 *                  otherwise make a copy of the pointer to
 *                  <tt>value<tt>.
 */
static void
setProp(Property** propertySet, const char* key, const char* value,
        jboolean useMalloc) {
    Property *p;
    for (p = *propertySet; p; p = p->next) {
        if (strcmp(key, p->key) == 0) {
            if (IS_NEW_VALUE(p)) {
                midpFree((void *)p->value);
            }
            if (useMalloc) {
                SET_NEW_VALUE(p);
                p->value = midpStrdup(value);
            } else {
                UNSET_NEW_VALUE(p);
                p->value = value;
            }
            /*
             * if midpStrdup fails we will just return without setting
             * the value to anything other than NULL
             */
            return;
        }
    }

    /* If the value is not defined, add it now */
    p = (Property*)midpMalloc(sizeof(Property));
    if (NULL != p){
        p->next = *propertySet;
        CLEAR_FLAGS(p);
        if (useMalloc) {
            SET_NEW_VALUE(p);
            SET_NEW_KEY(p);
            p->key = midpStrdup(key);
            p->value = midpStrdup(value);
        } else {
            UNSET_NEW_VALUE(p);
            UNSET_NEW_KEY(p);
            p->value = value;
            p->key = key;
        }

        if ((p->key == NULL) || (p->value == NULL && value != NULL)) {
            /* do nothing if there is no memory */
            if (useMalloc) {
                midpFree((void *)p->key);
                midpFree((void *)p->value);
            }
            midpFree((void *)p);
            return;
        }
        *propertySet = p ;
    }
}
/**
 * Gets OS path for the specified file system root.
 *
 * Note: the caller is responsible for calling 'midpFree' after use.
 * @param root Root name
 * @return The system-dependent path to access the root
 */ 
char* getNativePathForRoot(const char* root) 
{
	// root has the form "C:/", "D:/" etc.
	// just need to replace the slash with Symbian's separator (double backslash)
	TBuf8<KMaxFileName> path(reinterpret_cast<const TUint8*>(root));
	path.SetLength(2);
	path.Append(_L8("\\"));
	
	return midpStrdup((const char*)path.PtrZ());
}
/**
 * Gets the mounted root file systems.
 *
 * Note: the caller is responsible for calling 'midpFree' after use.
 * @return A string containing currently mounted roots
 *          separated by '\n' character
 */
char* getMountedRoots() 
{
	char* roots = NULL;
	TDriveList list;
	if(gFs.DriveList(list) == KErrNone)
	{
		TBuf8<64> buffer;
		for(int i=0;i<KMaxDrives;i++)
		{
			if(list[i] != 0)
			{
				TChar driveChar;
				gFs.DriveToChar(i,driveChar);
				buffer.Append(driveChar);
				buffer.Append(_L(":/\n"));
			}
		}
		buffer.SetLength(buffer.Length() -1);
		roots = midpStrdup((const char*)buffer.PtrZ());
	}
	return roots;
}
/**
 * Reads in a property file and makes the key/value pairs available
 * to MIDP as a property set.
 *
 * @param fd An open file descriptor of the property file to read.
 * @param props A property set to hold the key/value pairs read from
 *              the property file.
 * @return <tt>0</tt> for success, otherwise <tt>-1</tt>
 */
static int
parseConfig(int fd, Property** props) {
    char *buffer;
    int bufferSize;
    int i;
    int j;
    int key_index;
    int value_index;
    int len;
    char *errStr = NULL;
    int startPos;
    int endPos;

    bufferSize = storageSizeOf(&errStr, fd);
    buffer = (char *)midpMalloc(bufferSize);
    if (buffer == NULL) {
        REPORT_WARN(LC_CORE, 
            "midpMalloc failed to allocate buffer for config file."); 
        return -1;
    }

    /* Read the config file in one pass */
    len = storageRead(&errStr, fd, buffer, bufferSize);
    if ((errStr != NULL) || (len != bufferSize)) {
        REPORT_WARN1(LC_CORE, 
             "Warning: can not read config file: %s", errStr);
    
        storageFreeError(errStr);
        midpFree(buffer);
        return 0;
    }

    startPos = 0;
    for (i = startPos; i < bufferSize; i++) {
        if (buffer[i] == '\n') {
            
            buffer[i] = 0;

            /* Skip comment lines which begin  with '#'*/
            if (buffer[startPos] != '#') {
                
                /* Parse the line */
                key_index = startPos;
                for (j = key_index; buffer[j]; j++){

                    Property *prop;

                    if (buffer[j] == ':') {
                        buffer[j] = 0;
                        value_index = ++j;

                        prop = (Property *) midpMalloc(sizeof(Property));
                        if (NULL != prop) {
                            char *key, *value;
                            key = midpStrdup(buffer + key_index);
                            value = midpStrdup(buffer + value_index);
                            
                            /* trim leading and trailing white spaces */
                            trim_WhiteSpace(key);
                            trim_WhiteSpace(value);

                            prop->next = *props;
                            prop->key = key;
                            prop->value = value;
        
                            if ((prop->key == NULL) || (prop->value == NULL)) {
                                midpFree((void*)prop->key);
                                midpFree((void*)prop->value);
                                midpFree(prop);

                                /*
                                 * since we are freeing memory, we're not
                                 * exactly out of memory at this point
                                 */
                                break;
                            }

                            *props = prop;
                        }

                        break;
                    }
                }
            }
            endPos = i;
            startPos = endPos + 1;
        }
    }
    midpFree(buffer);
    return 0;
}