Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reduce cognitive complexity (sonar).
[simgrid.git] / src / xbt / xbt_os_file.c
index 46dd79a..692b7c3 100644 (file)
@@ -1,6 +1,6 @@
 /* xbt_os_file.c -- portable interface to file-related functions            */
 
-/* Copyright (c) 2007-2010, 2012-2015. The SimGrid Team.
+/* Copyright (c) 2007-2010, 2012-2017. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
  */
 ssize_t xbt_getline(char **buf, size_t *n, FILE *stream)
 {
-  ssize_t i;
-  int ch;
-
-  ch = getc(stream);
+  int ch = getc(stream);
   if (ferror(stream) || feof(stream))
     return -1;
 
@@ -48,15 +45,22 @@ ssize_t xbt_getline(char **buf, size_t *n, FILE *stream)
     *buf = xbt_malloc(*n);
   }
 
-  i = 0;
+  ssize_t i = 0;
   do {
-    if (i == *n)
-      *buf = xbt_realloc(*buf, *n += 512);
-    (*buf)[i++] = ch;
-  } while (ch != '\n' && (ch = getc(stream)) != EOF);
+    if (i == *n) {
+      *n += 512;
+      *buf = xbt_realloc(*buf, *n);
+    }
+    (*buf)[i] = ch;
+    i++;
+    if (ch == '\n')
+      break;
+  } while ((ch = getc(stream)) != EOF);
 
-  if (i == *n)
-    *buf = xbt_realloc(*buf, *n += 1);
+  if (i == *n) {
+    *n += 1;
+    *buf = xbt_realloc(*buf, *n);
+  }
   (*buf)[i] = '\0';
 
   return i;