changeset 21123:85b0935625c1

graalRuntime: make service file parsing more robust: - support various line endings (\r, \n and \r\n) - support # comments - support empty lines
author Gilles Duboscq <gilles.m.duboscq@oracle.com>
date Mon, 27 Apr 2015 18:17:18 +0200
parents 7f78f999512a
children 647f571f54da
files src/share/vm/graal/graalRuntime.cpp
diffstat 1 files changed, 34 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/vm/graal/graalRuntime.cpp	Mon Apr 27 16:11:46 2015 +0200
+++ b/src/share/vm/graal/graalRuntime.cpp	Mon Apr 27 18:17:18 2015 +0200
@@ -1099,19 +1099,43 @@
         GrowableArray<char*>* implNames = new GrowableArray<char*>();
         char* line = buffer;
         while (line - buffer < num_read) {
-          char* nl = strchr(line, '\n');
-          if (nl != NULL) {
-            *nl = '\0';
+          // find line end (\r, \n or \r\n)
+          char* nextline = NULL;
+          char* cr = strchr(line, '\r');
+          char* lf = strchr(line, '\n');
+          if (cr != NULL && lf != NULL) {
+            char* min = MIN2(cr, lf);
+            *min = '\0';
+            if (lf == cr + 1) {
+              nextline = lf + 1;
+            } else {
+              nextline = min + 1;
+            }
+          } else if (cr != NULL) {
+            *cr = '\0';
+            nextline = cr + 1;
+          } else if (lf != NULL) {
+            *lf = '\0';
+            nextline = lf + 1;
           }
-          // Turn all '.'s into '/'s
-          for (size_t index = 0; line[index] != '\0'; index++) {
-            if (line[index] == '.') {
-              line[index] = '/';
+          // trim left
+          while (*line == ' ' || *line == '\t') line++;
+          char* end = line + strlen(line);
+          // trim right
+          while (end > line && (*(end -1) == ' ' || *(end -1) == '\t')) end--;
+          *end = '\0';
+          // skip comments and empty lines
+          if (*line != '#' && strlen(line) > 0) {
+            // Turn all '.'s into '/'s
+            for (size_t index = 0; line[index] != '\0'; index++) {
+              if (line[index] == '.') {
+                line[index] = '/';
+              }
             }
+            implNames->append(line);
           }
-          implNames->append(line);
-          if (nl != NULL) {
-            line = nl + 1;
+          if (nextline != NULL) {
+            line = nextline;
           } else {
             // File without newline at the end
             break;