commit 3cc74915ffcf5f6bdb9cf56931fb6f040158cf47
parent 2bfb5407854d613eb01e7a7b7ec5f2de7a9cbcfc
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Tue, 17 Nov 2015 11:21:52 +0100
Add the schiff_streamline internal data structure
Factorise the streaming of lines.
Diffstat:
5 files changed, 112 insertions(+), 30 deletions(-)
diff --git a/README.md b/README.md
@@ -1,8 +1,9 @@
# Schiff
-The purpose of this program is to estimate the radiative properties of micro
-organism with an "Approximation Method for Short Wavelength or High-Energy
-Scattering" (L. Schiff, 1956).
+The purpose of this program is to estimate the radiative properties of soft
+particles with an "Approximation Method for Short Wavelength or High-Energy
+Scattering" (L. Schiff, 1956). Assume a small contrast on the relative
+refractive index of the particles.
## How to build
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -72,7 +72,8 @@ set(SCHIFF_FILES_INC
schiff_args.h
schiff_geometry.h
schiff_mesh.h
- schiff_optical_properties.h)
+ schiff_optical_properties.h
+ schiff_streamline.h)
set(SCHIFF_FILES_DOC COPYING.fr COPYING.en README.md)
# Prepend each file in the `SCHIFF_FILES_<SRC|INC>' list by the absolute
diff --git a/src/schiff_args.c b/src/schiff_args.c
@@ -49,8 +49,9 @@ print_help(const char* binary)
{
printf(
"Usage: %s [OPTIONS] [FILE]\n"
-"Estimate the radiative properties of micro organisms with an \"Approximation\n"
-"Method for Short Wavelength or High Energy Scattering\" (L. Schiff, 1956).\n\n",
+"Estimate the radiative properties of soft particles with an \"Approximation\n"
+"Method for Short Wavelength or High Energy Scattering\" (L. Schiff, 1956).\n"
+"Assume a small contrast on the relative refractive index of the particles.\n\n",
binary);
printf(
"FILE lists the per wavelength optical properties of the micro organisms. Each\n"
diff --git a/src/schiff_optical_properties.c b/src/schiff_optical_properties.c
@@ -27,6 +27,7 @@
* knowledge of the CeCILL license and that you accept its terms. */
#include "schiff_optical_properties.h"
+#include "schiff_streamline.h"
#include <rsys/cstr.h>
#include <rsys/stretchy_array.h>
@@ -102,7 +103,7 @@ res_T
schiff_optical_properties_load
(struct schiff_optical_properties** props, const char* filename)
{
- FILE* fp;
+ FILE* fp = NULL;
res_T res = RES_OK;
ASSERT(filename && props);
@@ -131,42 +132,28 @@ schiff_optical_properties_load_stream
const char* stream_name)
{
struct schiff_optical_properties* props = NULL;
- char* buf = NULL;
- const size_t buf_chunk = 128;
+ struct schiff_streamline streamline;
size_t iline;
+ char* line;
res_T res = RES_OK;
ASSERT(out_props && stream && stream_name);
- buf = sa_add(buf, buf_chunk);
+ schiff_streamline_init(&streamline);
- for(iline = 1; fgets(buf, (int)sa_size(buf), stream); ++iline) {
- char* line;
- size_t last_char;
+ iline = 1;
+ while((res = schiff_streamline_read(&streamline, stream, &line)) != RES_EOF) {
struct schiff_optical_properties tmp_props;
- while(!strrchr(buf, '\n')) { /* Ensure that the whole line is read */
- if(!fgets(sa_add(buf, buf_chunk), (int)buf_chunk, stream)) /* EOF */
- break;
- }
-
- /* Remove leading spaces */
- line = buf;
- while((*line == ' ' || *line == '\t') && *line != '\0') ++line;
-
- /* Remove newline character(s) */
- last_char = strlen(line);
- while(last_char-- && (line[last_char]=='\n' || line[last_char]=='\r'));
- line[last_char + 1] = '\0';
-
if(*line == '\0' /*Empty line*/ || *line == '#' /* Comment */)
continue;
/* Read optical properties */
- res = parse_optical_properties
- (&tmp_props, stream_name, iline, strtok(line, "#"));
+ line = strtok(line, "#");
+ res = parse_optical_properties(&tmp_props, stream_name, iline, line);
if(res == RES_OK) { /* *NO* error */
sa_push(props, tmp_props);
}
+ ++iline;
}
/* Sort the optical properties in ascending order with respect to the
@@ -175,7 +162,7 @@ schiff_optical_properties_load_stream
sizeof(struct schiff_optical_properties), cmp_properties);
*out_props = props;
- sa_release(buf);
+ schiff_streamline_release(&streamline);
return RES_OK;
}
diff --git a/src/schiff_streamline.h b/src/schiff_streamline.h
@@ -0,0 +1,92 @@
+/* Copyright (C) |Meso|Star> 2015 (contact@meso-star.com)
+ *
+ * This software is governed by the CeCILL license under French law and
+ * abiding by the rules of distribution of free software. You can use,
+ * modify and/or redistribute the software under the terms of the CeCILL
+ * license as circulated by CEA, CNRS and INRIA at the following URL
+ * "http://www.cecill.info".
+ *
+ * As a counterpart to the access to the source code and rights to copy,
+ * modify and redistribute granted by the license, users are provided only
+ * with a limited warranty and the software's author, the holder of the
+ * economic rights, and the successive licensors have only limited
+ * liability.
+ *
+ * In this respect, the user's attention is drawn to the risks associated
+ * with loading, using, modifying and/or developing or reproducing the
+ * software by the user in light of its specific status of free software,
+ * that may mean that it is complicated to manipulate, and that also
+ * therefore means that it is reserved for developers and experienced
+ * professionals having in-depth computer knowledge. Users are therefore
+ * encouraged to load and test the software's suitability as regards their
+ * requirements in conditions enabling the security of their systems and/or
+ * data to be ensured and, more generally, to use and operate it in the
+ * same conditions as regards security.
+ *
+ * The fact that you are presently reading this means that you have had
+ * knowledge of the CeCILL license and that you accept its terms. */
+
+#ifndef SCHIFF_STREAMLINE_H
+#define SHCIFF_STREAMLINE_H
+
+#include <rsys/stretchy_array.h>
+#include <string.h>
+
+struct schiff_streamline { char* buf; };
+
+static INLINE void
+schiff_streamline_init(struct schiff_streamline* sline)
+{
+ ASSERT(sline);
+ memset(sline, 0, sizeof(struct schiff_streamline));
+ sline->buf = sa_add(sline->buf, 128);
+}
+
+static INLINE void
+schiff_streamline_release(struct schiff_streamline* sline)
+{
+ ASSERT(sline);
+ sa_release(sline->buf);
+}
+
+static INLINE res_T
+schiff_streamline_read
+ (struct schiff_streamline* sline,
+ FILE* stream,
+ char** out_line)
+{
+ char* line = NULL;
+ size_t last_char;
+ const size_t chunk = 128;
+ res_T res = RES_OK;
+ ASSERT(sline && stream && out_line);
+
+ if(!fgets(sline->buf, (int)sa_size(sline->buf), stream)) {
+ res = RES_EOF;
+ goto exit;
+ }
+
+ while(!strrchr(sline->buf, '\n')) { /* Ensure that the whole line is read */
+ if(!fgets(sa_add(sline->buf, chunk), (int)chunk, stream)) /* EOF */
+ break;
+ }
+
+ /* Remove leading spaces */
+ line = sline->buf;
+ while((*line == ' ' || *line == '\t') && *line != '\0') ++line;
+
+ /* Remove newline character(s) */
+ last_char = strlen(line);
+ while(last_char-- && (line[last_char]=='\n' || line[last_char]=='\r'));
+ line[last_char + 1] = '\0';
+
+exit:
+ *out_line = line;
+ return res;
+error:
+ line = NULL;
+ goto error;
+}
+
+#endif /* SCHIFF_STREAMLINE_H */
+