[segyio] 340/376: Move common functionality for apps into own lib

Jørgen Kvalsvik jokva-guest at moszumanska.debian.org
Wed Sep 20 08:04:55 UTC 2017


This is an automated email from the git hooks/post-receive script.

jokva-guest pushed a commit to branch debian
in repository segyio.

commit ea99621a2e2d979ac99088b75dc123a8e4400a6d
Author: Jørgen Kvalsvik <jokva at statoil.com>
Date:   Tue Aug 8 14:25:23 2017 +0200

    Move common functionality for apps into own lib
    
    Some functionality is necessary for most applications (error messages,
    number parsing etc.). Organise this in the apputils library to reduce
    duplication across binaries.
---
 applications/CMakeLists.txt |  5 ++++-
 applications/apputils.c     | 48 +++++++++++++++++++++++++++++++++++++++++++++
 applications/apputils.h     | 10 ++++++++++
 applications/segyio-cath.c  | 25 +++++++++--------------
 4 files changed, 71 insertions(+), 17 deletions(-)

diff --git a/applications/CMakeLists.txt b/applications/CMakeLists.txt
index fb72190..1b146da 100644
--- a/applications/CMakeLists.txt
+++ b/applications/CMakeLists.txt
@@ -9,6 +9,9 @@ if (NOT MSVC)
     set(CMAKE_C_FLAGS "-std=c99 ${CMAKE_C_FLAGS}")
 endif()
 
+add_library(apputils STATIC apputils.c)
+target_link_libraries(apputils segyio)
+
 add_executable(segyinfo segyinfo.c)
 target_link_libraries(segyinfo segyio)
 
@@ -16,7 +19,7 @@ add_executable(segyinspect segyinspect.c)
 target_link_libraries(segyinspect segyio)
 
 add_executable(segyio-cath segyio-cath.c)
-target_link_libraries(segyio-cath segyio)
+target_link_libraries(segyio-cath segyio apputils)
 target_compile_definitions(segyio-cath PRIVATE
     -Dsegyio_MAJOR=${segyio_MAJOR}
     -Dsegyio_MINOR=${segyio_MINOR}
diff --git a/applications/apputils.c b/applications/apputils.c
new file mode 100644
index 0000000..8fd03af
--- /dev/null
+++ b/applications/apputils.c
@@ -0,0 +1,48 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "apputils.h"
+#include <segyio/segy.h>
+
+int errmsg( int errcode, const char* msg ) {
+    if( !msg ) return errcode;
+
+    fputs( msg,  stderr );
+    fputc( '\n', stderr );
+    return errcode;
+}
+
+int errmsg2( int errcode, const char* prelude, const char* msg ) {
+    if( !prelude ) return errmsg( errcode, msg );
+
+    fputs( prelude, stderr );
+    fputc( ':', stderr );
+    fputc( ' ', stderr );
+    return errmsg( errcode, msg );
+}
+
+int parseint( const char* str, int* x ) {
+    char* endptr;
+    *x = strtol( str, &endptr, 10 );
+
+    if( *endptr != '\0' ) return 1;
+    if( *x < 0 ) return 2;
+    return 0;
+}
+
+int bfield( const char* header, int field ) {
+    int32_t f;
+    int err = segy_get_bfield( header, field, &f );
+
+    if( err ) return -1;
+    return f;
+}
+
+int trfield( const char* header, int field ) {
+    int32_t f;
+    int err = segy_get_field( header, field, &f );
+
+    if( err ) return -1;
+    return f;
+}
diff --git a/applications/apputils.h b/applications/apputils.h
new file mode 100644
index 0000000..b2dc550
--- /dev/null
+++ b/applications/apputils.h
@@ -0,0 +1,10 @@
+#ifndef SEGYIO_APPUTILS_H
+#define SEGYIO_APPUTILS_H
+
+int errmsg( int errcode, const char* msg );
+int errmsg2( int errcode, const char* prelude, const char* msg );
+int parseint( const char* str, int* x );
+int bfield( const char* header, int field );
+int trfield( const char* header, int field );
+
+#endif //SEGYIO_APPUTILS_H
diff --git a/applications/segyio-cath.c b/applications/segyio-cath.c
index 8e1493c..b3120b4 100644
--- a/applications/segyio-cath.c
+++ b/applications/segyio-cath.c
@@ -6,6 +6,7 @@
 #include <getopt.h>
 #include <unistd.h>
 
+#include "apputils.c"
 #include <segyio/segy.h>
 
 static int help() {
@@ -44,14 +45,6 @@ static int ext_headers( segy_file* fp ) {
     return ext;
 }
 
-static int exit_error( int errcode, const char* msg ) {
-    if( !msg ) return errcode;
-
-    fputs( msg,  stderr );
-    fputc( '\n', stderr );
-    return errcode;
-}
-
 static void print_header( const char* header ) {
     for( int line = 0, ch = 0; line < 40; ++line ) {
         for( int c = 0; c < 80; ++c, ++ch ) putchar( header[ ch ] );
@@ -98,15 +91,15 @@ int main( int argc, char** argv ) {
                 if( num_sz == num_alloc_sz - 1 ) {
                     num_alloc_sz *= 2;
                     int* re = realloc( num, num_alloc_sz * sizeof( int ) );
-                    if( !re ) return exit_error( errno, "Unable to alloc" );
+                    if( !re ) return errmsg( errno, "Unable to alloc" );
                     num = re;
                 }
 
                 num[ num_sz ] = strtol( optarg, &endptr, 10 );
                 if( *endptr != '\0' )
-                    return exit_error( EINVAL, "num must be an integer" );
+                    return errmsg( EINVAL, "num must be an integer" );
                 if( num[ num_sz ] < 0 )
-                    return exit_error( EINVAL, "num must be non-negative" );
+                    return errmsg( EINVAL, "num must be non-negative" );
                 break;
 
             default: return help();
@@ -123,21 +116,21 @@ int main( int argc, char** argv ) {
         if( !fp ) fprintf( stderr, "segyio-cath: %s: No such file or directory\n",
                            argv[ i ] );
 
-        if( !fp && strict ) return exit_error( errno, NULL );
+        if( !fp && strict ) return errmsg( errno, NULL );
         if( !fp ) continue;
 
         if( num_sz == 0 ) num_sz = 1;
 
         const int exts = ext_headers( fp );
         if( exts < 0 )
-            return exit_error( errno, "Unable to read binary header" );
+            return errmsg( errno, "Unable to read binary header" );
 
         if( all ) {
             /* just create the list 0,1,2... as if it was passed explicitly */
             if( exts >= num_alloc_sz ) {
                 num_alloc_sz = exts * 2;
                 int* re = realloc( num, num_alloc_sz * sizeof( int ) );
-                if( !re ) return exit_error( errno, "Unable to alloc" );
+                if( !re ) return errmsg( errno, "Unable to alloc" );
                 num = re;
             }
 
@@ -149,14 +142,14 @@ int main( int argc, char** argv ) {
 
         for( int j = 0; j < num_sz; ++j ) {
             if( strict && ( num[ j ] < 0 || num[ j ] > exts ) )
-                return exit_error( EINVAL, "Header index out of range" );
+                return errmsg( EINVAL, "Header index out of range" );
             if( num[ j ] < 0 || num[ j ] > exts ) continue;
 
             int err = num[ j ] == 0
                 ? segy_read_textheader( fp, header )
                 : segy_read_ext_textheader( fp, num[ j ] - 1, header );
 
-            if( err != 0 ) return exit_error( errno, "Unable to read header" );
+            if( err != 0 ) return errmsg( errno, "Unable to read header" );
             print_header( header );
         }
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/segyio.git



More information about the debian-science-commits mailing list