gnuastro-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnuastro-commits] master 2a47b79e 1/2: Make (text-not-contains): new Ma


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 2a47b79e 1/2: Make (text-not-contains): new Make extension function
Date: Sun, 4 Sep 2022 11:39:33 -0400 (EDT)

branch: master
commit 2a47b79e2b8b1179db466921227a2fec874cab2e
Author: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>

    Make (text-not-contains): new Make extension function
    
    Until now, we only had an operator to select the words that contain a
    certain string. Similarly, it also regularly happens that you want words
    that don't contain a certain string.
    
    With this command, this has been done with the newly added
    'ast-text-not-contains' function. Since the internal structure of this
    function was almost identical to the 'ast-text-contains' function, a "base"
    function has been defined and both call that (with an argument to specify
    the differing condition).
    
    Also, while looking over the PDF book, I noticed two places that the
    example code had passed the page margin! So they have also been corrected
    in this commit.
---
 NEWS              | 21 ++++++++++++---------
 doc/gnuastro.texi | 37 ++++++++++++++++++++++++++-----------
 lib/makeplugin.c  | 55 ++++++++++++++++++++++++++++++++++++++++++++-----------
 3 files changed, 82 insertions(+), 31 deletions(-)

diff --git a/NEWS b/NEWS
index 96d181df..f08eaac7 100644
--- a/NEWS
+++ b/NEWS
@@ -91,22 +91,25 @@ See the end of the file for license conditions.
     inner parts of the star, they degrade the stack's outer parts. With
     this option, the fainter stars won't harm the outer parts.
 
-  GNU Make extensions:
-  - It is now possible to use custom Gnuastro functions in GNU Make, using
+  GNU Make extensions (in a Makefile)
+    It is now possible to use custom Gnuastro functions in GNU Make, using
     its extension facilities with Dynamic libraries. GNU Make is a very
     powerful workflow manager that is also used for data analysis (not just
     for compilation). With the Gnaustro Make functions, (astronomical) data
     analysis becomes even more easier and faster. In the following, you can
-    see the first set of such functions (they all begin with 'astgmk' for
-    "Astronomical GNU Make"):
-    - ast-text-contains: Will only return those space-separated words
-      within a larger list that contain a certain string. The to-contain
-      string can be placed anywhere within the words of the larger list.
-    - ast-fits-with-keyvalue: Takes a keyword name, a list of keyword
+    see the first set of such functions (they all begin with 'ast-'). For
+    more, see the newly added chapter in the Gnuastro manual.
+    - ast-text-contains: will return space-separated words within a larger
+      list that contain a certain string. The to-contain string can be
+      anywhere within the words of the larger list.
+    - ast-text-not-contains: will return space-separated words within a
+      larger list that DO NOT contain a certain string. The to-not-contain
+      string can be anywhere within the words of the larger list.
+    - ast-fits-with-keyvalue: takes a keyword name, a list of keyword
       values, a HDU and a list of FITS files. It will return only those
       FITS files that have the requested value(s) in the requested keyword
       of the requested HDU.
-    - ast-fits-unique-keyvalues: Takes a keyword name, a HDU and a list of
+    - ast-fits-unique-keyvalues: takes a keyword name, a HDU and a list of
       FITS files. It will return all the unique values given to that
       keyword within the FITS files.
 
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index 6af9f188..e9aa2475 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -7992,7 +7992,7 @@ ImageMagick is a wonderful and robust program for image 
manipulation on the comm
 Since ImageMagick version 7, it is necessary to edit the policy file 
(@file{/etc/ImageMagick-7/policy.xml}) to have the following line (it maybe 
present, but commented, in this case un-comment it):
 
 @example
-<policy domain="coder" rights="read|write" pattern="@{PS,PDF,XPS@}" />
+<policy domain="coder" rights="read|write" pattern="@{PS,PDF,XPS@}"/>
 @end example
 
 If the following line is present, it is also necessary to comment/remove it.
@@ -27990,7 +27990,7 @@ $ which astfits | sed 
-e's|bin/astfits|lib/libgnuastro_make.so|'
 @node Makefile functions of Gnuastro,  , Loading the Gnuastro Make functions, 
Makefile extensions
 @section Makefile functions of Gnuastro
 
-All Gnuastro Make functions start with the @command{ast-} prefix (similar to 
the programs on the command-line, but with a dash.
+All Gnuastro Make functions start with the @command{ast-} prefix (similar to 
the programs on the command-line, but with a dash).
 After you have loaded Gnuastro's shared library for Makefiles within your 
Makefile, you can call these functions just like any Make function.
 For instructions on how to load Gnuastro's Make functions, see @ref{Loading 
the Gnuastro Make functions}.
 
@@ -28008,12 +28008,14 @@ For more, see the 
@url{https://www.gnu.org/software/make/manual/html_node/Flavor
 @end cartouche
 
 @table @code
-@item $(ast-text-contains PATTERN, TEXT)
-Returns all whitespace-separated words in @code{TEXT} that contain of the 
@code{PATTERN}, removing any words that @emph{do not} match.
-For example, the following minimal Makefile will only print the @code{bAaz} 
word of the list.
+@item $(ast-text-contains STRING, TEXT)
+Returns all whitespace-separated words in @code{TEXT} that contain the 
@code{STRING}, removing any words that @emph{do not} match.
+For example, the following minimal Makefile will only print the @code{bAaz 
Aah} word of the list.
 
 @example
-list = fooo baar bAaz uggh
+load /usr/local/lib/libgnuastro_make.so
+
+list = fooo baar bAaz uggh Aah
 all:
      echo $(ast-text-contains Aa, $(list))
 @end example
@@ -28022,6 +28024,19 @@ This can be thought of as Make's own @code{filter} 
function, but if it would acc
 In fact, the first sentence describing this function is taken from the Make 
manual's first sentence that describes the @code{filter} function!
 However, unfortuantely Make's @code{filter} function only accepts a single 
@code{%}, not two!
 
+@item $(ast-text-not-contains STRING, TEXT)
+Returns all whitespace-separated words in @code{TEXT} that @emph{do not} 
contain the @code{STRING}, removing any words that @emph{do not} match.
+This is the inverse of the @code{ast-text-contains} function.
+For example, the following minimal Makefile will print @code{fooo baar uggh} 
word of the list.
+
+@example
+load /usr/local/lib/libgnuastro_make.so
+
+list = fooo baar bAaz uggh Aah
+all:
+     echo $(ast-text-not-contains Aa, $(list))
+@end example
+
 @item $(ast-fits-with-keyvalue KEYNAME, KEYVALUES, HDU, FITS_FILES)
 Will select only the FITS files (from a list of many in @code{FITS_FILES}, 
non-FITS files are ignored), where the @code{KEYNAME} keyword has the value(s) 
given in @code{KEYVALUES}.
 Only the HDU given in the @code{HDU} argument will be checked.
@@ -31877,9 +31892,9 @@ gal_fits_key_read_from_ptr(fptr, keysll, 0, 0);
 
 /* Use the values as you like... */
 
-/* Free all the allocated spaces. Note that `name' was not allocated
-   in this example, so we should explicitly set it to NULL before
-   calling `gal_data_array_free'. */
+/* Free all the allocated spaces. Note that `name' was not
+   allocated in this example, so we should explicitly set
+   it to NULL before calling `gal_data_array_free'. */
 for(i=0;i<N;++i) keysll[i].name=NULL;
 gal_data_array_free(keysll, N, 1);
 @end example
@@ -33347,8 +33362,8 @@ main(void)
   /* Clean up. Due to the in-place flag (in
    * 'GAL_ARITHMETIC_FLAGS_BASIC'), 'out1' and 'out2' point to the
    * same array in memory and due to the freeing flag, any input
-   * dataset(s) that were not returned have been freed internally by
-   * 'gal_arithmetic'. Therefore it is only necessary to free
+   * dataset(s) that were not returned have been freed internally
+   * by 'gal_arithmetic'. Therefore it is only necessary to free
    * 'out2': all other allocated spaces have been freed internally.
    * before reaching this point. */
   gal_data_free(out2);
diff --git a/lib/makeplugin.c b/lib/makeplugin.c
index 3d7f838a..3a3e7763 100644
--- a/lib/makeplugin.c
+++ b/lib/makeplugin.c
@@ -49,6 +49,7 @@ int plugin_is_GPL_compatible=1;
 /* Names of the separate functions */
 #define MAKEPLUGIN_FUNC_PREFIX "ast"
 static char *text_contains_name=MAKEPLUGIN_FUNC_PREFIX"-text-contains";
+static char *text_not_contains_name=MAKEPLUGIN_FUNC_PREFIX"-text-not-contains";
 static char 
*fits_with_keyvalue_name=MAKEPLUGIN_FUNC_PREFIX"-fits-with-keyvalue";
 static char 
*fits_unique_keyvalues_name=MAKEPLUGIN_FUNC_PREFIX"-fits-unique-keyvalues";
 
@@ -65,13 +66,10 @@ static char 
*fits_unique_keyvalues_name=MAKEPLUGIN_FUNC_PREFIX"-fits-unique-keyv
 /***************             Text utilities             ***************/
 /**********************************************************************/
 
-/* Return any of the input strings that contain the given string. It takes
-   two arguments:
-      0. String to check.
-      1. List of text.*/
+/* Base function that is used for both the contains and not-contains
+   functions. */
 static char *
-makeplugin_text_contains(const char *caller, unsigned int argc,
-                         char **argv)
+makeplugin_text_contains_base(char **argv, int has1_not0)
 {
   char *out=NULL;
   gal_list_str_t *tmp, *outlist=NULL;
@@ -80,10 +78,12 @@ makeplugin_text_contains(const char *caller, unsigned int 
argc,
 
   /* Parse the input strings and find the ones that match. */
   for(tmp=strings; tmp!=NULL; tmp=tmp->next)
-    if( gal_txt_contains_string(tmp->v, match) )
+    if( gal_txt_contains_string(tmp->v, match)==has1_not0 )
       gal_list_str_add(&outlist, tmp->v, 0);
 
-  /* Write the list into one string. */
+  /* Write the list into one string, but first reverse it so it has the
+     same order as the input. */
+  gal_list_str_reverse(&outlist);
   out=gal_list_str_cat(outlist);
 
   /* Clean up and return. */
@@ -96,6 +96,36 @@ makeplugin_text_contains(const char *caller, unsigned int 
argc,
 
 
 
+/* Return any of the input strings that contain the given string. It takes
+   two arguments:
+      0. String to check.
+      1. List of text.*/
+static char *
+makeplugin_text_contains(const char *caller, unsigned int argc,
+                         char **argv)
+{
+  return makeplugin_text_contains_base(argv, 1);
+}
+
+
+
+
+
+/* Return any of the input strings that contain the given string. It takes
+   two arguments:
+      0. String to check.
+      1. List of text.*/
+static char *
+makeplugin_text_not_contains(const char *caller, unsigned int argc,
+                             char **argv)
+{
+  return makeplugin_text_contains_base(argv, 0);
+}
+
+
+
+
+
 
 
 
@@ -223,9 +253,12 @@ makeplugin_fits_unique_keyvalues(const char *caller, 
unsigned int argc,
 int
 libgnuastro_make_gmk_setup()
 {
-  /* Return any of the input strings that contain the given string. */
-  gmk_add_function(text_contains_name,
-                   makeplugin_text_contains,
+  /* Return the input strings that contain the given string. */
+  gmk_add_function(text_contains_name, makeplugin_text_contains,
+                   2, 2, GMK_FUNC_DEFAULT);
+
+  /* Return the input strings that DON'T contain the given string. */
+  gmk_add_function(text_not_contains_name, makeplugin_text_not_contains,
                    2, 2, GMK_FUNC_DEFAULT);
 
   /* Select files, were a certain keyword has a certain value. It takes



reply via email to

[Prev in Thread] Current Thread [Next in Thread]