TimJTi commented on code in PR #2178:
URL: https://github.com/apache/nuttx-apps/pull/2178#discussion_r1383787724


##########
system/settings/storage_text.c:
##########
@@ -0,0 +1,379 @@
+/****************************************************************************
+ * apps/system/settings/storage_text.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include "system/settings.h"
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <ctype.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <nuttx/config.h>
+#include <sys/types.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define BUFFER_SIZE 256
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+FAR static setting_t *getsetting(char *key);
+extern state_t g_settings;
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: load_text
+ *
+ * Description:
+ *    Loads text from a storage file.
+ *
+ * Input Parameters:
+ *    file             - the filename of the storage to use
+ *
+ * Returned Value:
+ *   Success or negated failure code
+ *
+ ****************************************************************************/
+
+int load_text(FAR char *file)
+{
+  int ret = OK;
+  FAR FILE *f;
+
+  /* Check that the file exists */
+
+  if (access(file, F_OK) != 0)
+    {
+      /* If not, try the backup file */
+
+      FAR char *backup_file = malloc(strlen(file) + 2);
+      if (backup_file == NULL)
+        {
+          return -ENODEV;
+        }
+
+      strcpy(backup_file, file);
+      strcat(backup_file, "~");
+
+      if (access(backup_file, F_OK) == 0)
+        {
+          /* There is a backup file
+           * Restore this as the new settings file
+           */
+
+          rename(backup_file, file);
+        }
+
+      free(backup_file);
+    }
+
+  f = fopen(file, "r");
+  if (f == NULL)
+    {
+      return -ENOENT;
+    }
+
+  FAR char *buffer = malloc(BUFFER_SIZE);
+  if (buffer == NULL)
+    {
+      ret = -ENOMEM;
+      goto abort;
+    }
+
+  while (fgets(buffer, BUFFER_SIZE, f))
+    {
+      /* Remove any line terminators */
+
+      for (int i = ((int)strlen(buffer) - 1); i > 0; i--)
+        {
+          if (buffer[i] == ';' || buffer[i] == '\n' || buffer[i] == '\r')
+            {
+              buffer[i] = '\0';
+            }
+          else
+            {
+              break;
+            }
+        }
+
+      /* Separate the key / value pair */
+
+      FAR char *eq = strchr(buffer, '=');
+      if (eq == NULL)
+        {
+          continue;
+        }
+
+      FAR char *key = buffer;
+      FAR char *val = eq + 1;

Review Comment:
   As above



##########
system/settings/storage_text.c:
##########
@@ -0,0 +1,379 @@
+/****************************************************************************
+ * apps/system/settings/storage_text.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include "system/settings.h"
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <ctype.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <nuttx/config.h>
+#include <sys/types.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define BUFFER_SIZE 256
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+FAR static setting_t *getsetting(char *key);
+extern state_t g_settings;
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: load_text
+ *
+ * Description:
+ *    Loads text from a storage file.
+ *
+ * Input Parameters:
+ *    file             - the filename of the storage to use
+ *
+ * Returned Value:
+ *   Success or negated failure code
+ *
+ ****************************************************************************/
+
+int load_text(FAR char *file)
+{
+  int ret = OK;
+  FAR FILE *f;
+
+  /* Check that the file exists */
+
+  if (access(file, F_OK) != 0)
+    {
+      /* If not, try the backup file */
+
+      FAR char *backup_file = malloc(strlen(file) + 2);
+      if (backup_file == NULL)
+        {
+          return -ENODEV;
+        }
+
+      strcpy(backup_file, file);
+      strcat(backup_file, "~");
+
+      if (access(backup_file, F_OK) == 0)
+        {
+          /* There is a backup file
+           * Restore this as the new settings file
+           */
+
+          rename(backup_file, file);
+        }
+
+      free(backup_file);
+    }
+
+  f = fopen(file, "r");
+  if (f == NULL)
+    {
+      return -ENOENT;
+    }
+
+  FAR char *buffer = malloc(BUFFER_SIZE);
+  if (buffer == NULL)
+    {
+      ret = -ENOMEM;
+      goto abort;
+    }
+
+  while (fgets(buffer, BUFFER_SIZE, f))
+    {
+      /* Remove any line terminators */
+
+      for (int i = ((int)strlen(buffer) - 1); i > 0; i--)
+        {
+          if (buffer[i] == ';' || buffer[i] == '\n' || buffer[i] == '\r')
+            {
+              buffer[i] = '\0';
+            }
+          else
+            {
+              break;
+            }
+        }
+
+      /* Separate the key / value pair */
+
+      FAR char *eq = strchr(buffer, '=');
+      if (eq == NULL)
+        {
+          continue;
+        }
+
+      FAR char *key = buffer;
+      FAR char *val = eq + 1;
+      *eq = '\0';
+
+      /* Check if the key is valid */
+
+      if (!isalpha(key[0]))
+        {
+          continue;
+        }
+
+      /* Get the setting slot */
+
+      FAR setting_t *setting = getsetting(key);

Review Comment:
   As above



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to