[Pcsclite-cvs-commit] r5156 - in /trunk/PCSC/src: parser.h tokenparser.l

rousseau at users.alioth.debian.org rousseau at users.alioth.debian.org
Tue Aug 17 14:41:14 UTC 2010


Author: rousseau
Date: Tue Aug 17 14:41:12 2010
New Revision: 5156

URL: http://svn.debian.org/wsvn/pcsclite/?sc=1&rev=5156
Log:
parse the Info.plist file only once and generate a list of key/values

Modified:
    trunk/PCSC/src/parser.h
    trunk/PCSC/src/tokenparser.l

Modified: trunk/PCSC/src/parser.h
URL: http://svn.debian.org/wsvn/pcsclite/trunk/PCSC/src/parser.h?rev=5156&op=diff
==============================================================================
--- trunk/PCSC/src/parser.h (original)
+++ trunk/PCSC/src/parser.h Tue Aug 17 14:41:12 2010
@@ -22,17 +22,17 @@
 {
 #endif
 
-#define TOKEN_MAX_KEY_SIZE   200
-#define TOKEN_MAX_VALUE_SIZE 200
+#include "simclist.h"
 
-#define TOKEN_TYPE_KEY         1
-#define TOKEN_TYPE_STRING      2
+struct bundleElt
+{
+	char *key;
+	list_t values;
+};
 
-int LTPBundleFindValueWithKey(const char *fileName, const char *tokenKey,
-                              /*@out@*/ char *tokenValue, int tokenIndice);
-
-int LTPBundleFindOptionalValueWithKey(const char *fileName,
-	const char *tokenKey, /*@out@*/ char *tokenValue, int tokenIndice);
+int LTPBundleFindValueWithKey(list_t *l, const char *key, list_t **values);
+int bundleParse(const char *fileName, list_t *l);
+void bundleRelease(list_t *l);
 
 #ifdef __cplusplus
 }

Modified: trunk/PCSC/src/tokenparser.l
URL: http://svn.debian.org/wsvn/pcsclite/trunk/PCSC/src/tokenparser.l?rev=5156&op=diff
==============================================================================
--- trunk/PCSC/src/tokenparser.l (original)
+++ trunk/PCSC/src/tokenparser.l Tue Aug 17 14:41:12 2010
@@ -13,7 +13,7 @@
 
 /**
  * @file
- * @brief provides LTPBundleFindValueWithKey() function on non-MacOS X
+ * @brief provides parsing functions for Info.plist files
  * platforms
  */
 
@@ -23,22 +23,20 @@
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
-
-#include "misc.h"
+#define NDEBUG
+#include <assert.h>
+
+#include "simclist.h"
 #include "debuglog.h"
 #include "parser.h"
 #include "strlcpycat.h"
 
-void tpevalToken(char *pcToken, int tokType);
-
-static const char *pcDesiredKey = NULL;
-static char pcKey[TOKEN_MAX_KEY_SIZE];
-static char pcValue[TOKEN_MAX_VALUE_SIZE];
-static char pcFinValue[TOKEN_MAX_VALUE_SIZE];
-static int valueIndex = 0;
-static int desiredIndex = 0;
-
+static void eval_key(char *pcToken, list_t *list_key);
+static void eval_value(char *pcToken, list_t *list_values);
 void tperrorCheck (char *pcToken_error);
+
+static list_t *ListKeys;
+static list_t *ListValues;
 
 %}
 
@@ -50,53 +48,65 @@
 
 #.*                                             {}
 "\n"                                            {}
-\<key\>([A-Z]|[a-z]|[0-9]|[ \t])+\<\/key\>      { valueIndex = 0; tpevalToken(yytext, TOKEN_TYPE_KEY); }
+\<key\>([A-Z]|[a-z]|[0-9]|[ \t])+\<\/key\>      { eval_key(yytext, ListKeys); }
 [ \t]                                           {}
-\<string\>([A-Z]|[a-z]|[0-9]|[ \t]|[!@#$%^&*()\-+/_\:?.,=~'"])+\<\/string\> {tpevalToken(yytext, TOKEN_TYPE_STRING); valueIndex += 1;}
+\<string\>([A-Z]|[a-z]|[0-9]|[ \t]|[!@#$%^&*()\-+/_\:?.,=~'"])+\<\/string\> { eval_value(yytext, ListValues); }
 .                                               { tperrorCheck(yytext); }
 %%
 
 
-void tpevalToken(char *pcToken, int tokType)
-{
-	unsigned int len;
-	len = 0;
-
-	if (tokType == TOKEN_TYPE_KEY)
-	{
-		/* <key>foobar</key>
-		 * 012345 : 5 is the first key character index */
-
-		/* calculate the argument length */
-		for (len=0; pcToken[len+5] != '<'; len++)
-			;
-		len++;	/* final NULL byte */
-
-		if (len > sizeof(pcKey))
-			(void)strlcpy(pcKey, &pcToken[5], sizeof(pcKey));
-		else
-			(void)strlcpy(pcKey, &pcToken[5], len);
-	}
-
-	if (tokType == TOKEN_TYPE_STRING)
-	{
-		/* <string>foobar</string>
-		 * 012345678 : 8 is the first string character index */
-
-		/* calculate the argument length */
-		for (len=0; pcToken[len+8] != '<'; len++)
-			;
-		len++;	/* final NULL byte */
-
-		if (len > sizeof(pcValue))
-			(void)strlcpy(pcValue, &pcToken[8], sizeof(pcValue));
-		else
-			(void)strlcpy(pcValue, &pcToken[8], len);
-
-		if (strcmp(pcKey, pcDesiredKey) == 0)
-			if (desiredIndex == valueIndex)
-				(void)strlcpy(pcFinValue, pcValue, sizeof(pcFinValue));
-	}
+static void eval_key(char *pcToken, list_t *list_key)
+{
+	struct bundleElt *elt;
+	int r;
+	size_t len;
+
+	/* create a new list element */
+	elt = malloc(sizeof(*elt));
+	assert(elt);
+
+	/* <key>foobar</key>
+	 * 012345 : 5 is the first key character index */
+
+	/* calculate the argument length */
+	for (len=0; pcToken[len+5] != '<'; len++)
+		;
+	len++;	/* final NULL byte */
+
+	elt->key = malloc(len);
+	(void)strlcpy(elt->key, &pcToken[5], len);
+
+	r = list_init(&elt->values);
+	assert(r >= 0);
+
+	/* add the key/values */
+	list_append(list_key, elt);
+
+	/* set the list to store the values */
+	ListValues = &elt->values;
+}
+
+static void eval_value(char *pcToken, list_t *list_values)
+{
+	int r;
+	size_t len;
+	char *value;
+
+	/* <string>foobar</string>
+	 * 012345678 : 8 is the first string character index */
+
+	/* calculate the argument length */
+	for (len=0; pcToken[len+8] != '<'; len++)
+		;
+	len++;	/* final NULL byte */
+
+	value = malloc(len);
+	assert(value);
+
+	(void)strlcpy(value, &pcToken[8], len);
+
+	r = list_append(list_values, value);
+	assert(r >= 0);
 }
 
 void tperrorCheck (char *token_error)
@@ -108,29 +118,60 @@
  * Find an optional key in a configuration file
  * No error is logged if the key is not found
  *
+ * @param l
+ * @param list generated by bundleParse()
+ * @param[out] values list of token value (if key found)
+ * @retval 0 OK
+ * @retval 1 key not found
+ */
+int LTPBundleFindValueWithKey(list_t *l, const char *key, list_t **values)
+{
+	unsigned int i;
+	int ret = 1;
+
+	for (i=0; i < list_size(l); i++)
+	{
+		struct bundleElt *elt;
+
+		elt = list_get_at(l, i);
+		assert(elt);
+
+		if (0 == strcmp(elt->key, key))
+		{
+			*values = &elt->values;
+			ret = 0;
+		}
+	}
+
+	return ret;
+}
+
+
+/**
+ * Parse a Info.plist file and file a list
+ *
  * @param fileName file name
- * @param tokenKey key value
- * @param[out] tokenValue token value (if key found)
- * @param tokenIndice indice of the desired key
+ * @param l list containing the results
  * @retval -1 configuration file not found
  * @retval 0 OK
- * @retval 1 key not found
- */
-int LTPBundleFindOptionalValueWithKey(const char *fileName,
-	const char *tokenKey, char *tokenValue, int tokenIndice)
+ */
+int bundleParse(const char *fileName, list_t *l)
 {
 	FILE *file = NULL;
-	int ret = 0;
-
-	desiredIndex  = tokenIndice;
-	pcDesiredKey  = tokenKey;
-	pcFinValue[0] = '\0';
+	int r;
 
 	file = fopen(fileName, "r");
-
 	if (!file)
+	{
+		Log3(PCSC_LOG_CRITICAL, "Could not open bundle file %s: %s",
+			fileName, strerror(errno));
 		return 1;
-
+	}
+
+	r = list_init(l);
+	assert(r >= 0);
+
+	ListKeys = l;
 	yyin = file;
 
 	do
@@ -138,44 +179,55 @@
 		(void)yylex();
 	} while (!feof(file));
 
-	if ('\0' == pcFinValue[0])
-		ret = -1;
-	else
-		(void)strlcpy(tokenValue, pcFinValue, TOKEN_MAX_VALUE_SIZE);
-
 	(void)fclose(file);
-	return ret;
-}
-
-
-/**
- * Find a key in a configuration file
- *
- * @param fileName file name
- * @param tokenKey key value
- * @param[out] tokenValue token value (if key found)
- * @param tokenIndice indice of the desired key
- * @retval -1 configuration file not found
- * @retval 0 OK
- * @retval 1 key not found
- */
-int LTPBundleFindValueWithKey(const char *fileName, const char *tokenKey,
-                              char *tokenValue, int tokenIndice)
-{
-	int ret = 0;
-
-	ret = LTPBundleFindOptionalValueWithKey(fileName, tokenKey, tokenValue,
-		tokenIndice);
-
-	if (1 == ret)
-		Log3(PCSC_LOG_CRITICAL, "Could not open bundle file %s: %s",
-			fileName, strerror(errno));
-
-	if ((-1 == ret) && (0 == tokenIndice))
-		/* Not defined at all */
-		Log3(PCSC_LOG_CRITICAL, "Value/Key not defined for: %s in %s",
-			tokenKey, fileName);
-
-	return ret;
-}
-
+
+#ifndef NDEBUG
+	printf("size: %d\n", list_size(l));
+	for (i=0; i < list_size(l); i++)
+	{
+		struct bundleElt *elt;
+		unsigned int j;
+
+		elt = list_get_at(l, i);
+		assert(elt);
+		printf("Key: %s\n", elt->key);
+
+		for (j=0; j<list_size(&elt->values); j++)
+		{
+			char *v = list_get_at(&elt->values, j);
+			printf(" value: %s\n", v);
+		}
+	}
+#endif
+
+	return 0;
+}
+
+/**
+ * Free the list created by bundleParse()
+ *
+ * @param l list containing the results
+ */
+void bundleRelease(list_t *l)
+{
+	unsigned int i;
+
+	for (i=0; i < list_size(l); i++)
+	{
+		struct bundleElt *elt;
+		unsigned int j;
+
+		elt = list_get_at(l, i);
+		assert(elt);
+
+		/* free all the values */
+		for (j=0; j<list_size(&elt->values); j++)
+			free(list_get_at(&elt->values, j));
+		list_destroy(&elt->values);
+
+		/* free the key */
+		free(elt);
+	}
+
+	list_destroy(l);
+}




More information about the Pcsclite-cvs-commit mailing list