From 77ae53cf09f6475a44bb024c6451ff0e76bfc536 Mon Sep 17 00:00:00 2001
From: DUOLabs333 <dvdugo333@gmail.com>
Date: Thu, 15 Sep 2022 14:53:58 -0400
Subject: [PATCH] Add compatibility for MacOS

---
 src/fsearch_database_entry.c |   4 +
 src/fsearch_file_utils.c     |  14 +++-
 src/fsearch_window_actions.c |   8 ++
 src/meson.build              |   4 +
 src/strverscmp.c             | 158 +++++++++++++++++++++++++++++++++++
 5 files changed, 186 insertions(+), 2 deletions(-)
 create mode 100644 src/strverscmp.c

diff --git a/src/fsearch_database_entry.c b/src/fsearch_database_entry.c
index 05a8d4b3a..75e05edaa 100644
--- src/fsearch_database_entry.c
+++ src/fsearch_database_entry.c
@@ -6,6 +6,10 @@
 #include <stdlib.h>
 #include <string.h>
 
+#ifdef __MACH__
+int strverscmp (const char *, const char *);
+#endif
+
 struct FsearchDatabaseEntry {
     FsearchDatabaseEntryFolder *parent;
     char *name;
diff --git a/src/fsearch_file_utils.c b/src/fsearch_file_utils.c
index 26d31ec42..27b1953ae 100644
--- src/fsearch_file_utils.c
+++ src/fsearch_file_utils.c
@@ -22,7 +22,11 @@
 #include "fsearch_limits.h"
 #include "fsearch_string_utils.h"
 #include "fsearch_ui_utils.h"
+
+#ifndef __MACH__
 #include <gio/gdesktopappinfo.h>
+#endif
+
 #include <gio/gio.h>
 #include <glib/gi18n.h>
 #include <stdbool.h>
@@ -237,7 +241,7 @@ create_uris_launch_context(const char *content_type, GPtrArray *files, FsearchFi
             if (!path) {
                 continue;
             }
-            GDesktopAppInfo *desktop_app_info = g_desktop_app_info_new_from_filename(path);
+            GAppInfo *desktop_app_info = g_app_info_create_from_commandline("/usr/bin/open",NULL,G_APP_INFO_CREATE_NONE,NULL);
             if (!desktop_app_info) {
                 add_error_message_with_format(ctx->error_messages,
                                               C_("Will be followed by the file path.",
@@ -296,7 +300,8 @@ handle_queued_uris(FsearchFileUtilsLaunchContext *launch_ctx) {
     }
     else {
         FsearchFileUtilsLaunchUrisContext *uris_ctx = g_queue_pop_head(launch_ctx->launch_uris_ctx_queue);
-#if GLIB_CHECK_VERSION(2, 60, 0)
+
+#if GLIB_CHECK_VERSION(2, 60, 0) && !defined(__MACH__)
         g_app_info_launch_uris_async(uris_ctx->app_info,
                                      uris_ctx->uris,
                                      launch_ctx->app_launch_context,
@@ -563,7 +568,12 @@ fsearch_file_utils_get_file_type(const char *name, gboolean is_dir) {
 
 GIcon *
 fsearch_file_utils_get_desktop_file_icon(const char *path) {
+    #ifdef __MACH__
+    g_autoptr(GAppInfo) info = NULL;
+    #else
     g_autoptr(GAppInfo) info = (GAppInfo *)g_desktop_app_info_new_from_filename(path);
+    #endif
+
     if (!info) {
         goto default_icon;
     }
diff --git a/src/fsearch_window_actions.c b/src/fsearch_window_actions.c
index 6aeb08b7b..9a9cee7dc 100644
--- src/fsearch_window_actions.c
+++ src/fsearch_window_actions.c
@@ -20,7 +20,10 @@
 #include <config.h>
 #endif
 
+#ifndef __MACH__
 #include <gio/gdesktopappinfo.h>
+#endif
+
 #include <glib/gi18n.h>
 #include <stdint.h>
 
@@ -455,7 +458,12 @@ fsearch_window_action_open_with(GSimpleAction *action, GVariant *variant, gpoint
     if (!app_id) {
         return;
     }
+    #ifdef __MACH__
+    g_autoptr(GAppInfo) app_info=NULL;
+    #else
     g_autoptr(GDesktopAppInfo) app_info = g_desktop_app_info_new(app_id);
+    #endif
+    
     if (!app_info) {
         return;
     }
diff --git a/src/meson.build b/src/meson.build
index b2527a565..f6b0a890e 100644
--- src/meson.build
+++ src/meson.build
@@ -53,6 +53,10 @@ libfsearch_sources = [
     'fsearch_window_actions.c',
 ]
 
+if build_machine.system()=='darwin'
+    libfsearch_sources+=['strverscmp.c']
+endif
+
 fsearch_deps = [
     cc.find_library('m', required: true),
     dependency('gio-unix-2.0', version: '>= 2.50'),
diff --git a/src/strverscmp.c b/src/strverscmp.c
new file mode 100644
index 000000000..2d1b4066d
--- /dev/null
+++ src/strverscmp.c
@@ -0,0 +1,158 @@
+/* Compare strings while treating digits characters numerically.
+   Copyright (C) 1997, 2002, 2005 Free Software Foundation, Inc.
+   This file is part of the libiberty library.
+   Contributed by Jean-François Bignolles <bignolle@ecoledoc.ibp.fr>, 1997.
+
+   Libiberty is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   Libiberty is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, write to the Free
+   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301 USA.  */
+
+/* #include "libiberty.h" */
+/* #include "safe-ctype.h" */
+#include <ctype.h>
+
+/* 
+@deftypefun int strverscmp (const char *@var{s1}, const char *@var{s2})
+The @code{strverscmp} function compares the string @var{s1} against
+@var{s2}, considering them as holding indices/version numbers.  Return
+value follows the same conventions as found in the @code{strverscmp}
+function.  In fact, if @var{s1} and @var{s2} contain no digits,
+@code{strverscmp} behaves like @code{strcmp}.
+
+Basically, we compare strings normally (character by character), until
+we find a digit in each string - then we enter a special comparison
+mode, where each sequence of digits is taken as a whole.  If we reach the
+end of these two parts without noticing a difference, we return to the
+standard comparison mode.  There are two types of numeric parts:
+"integral" and "fractional" (those  begin with a '0'). The types
+of the numeric parts affect the way we sort them:
+
+@itemize @bullet
+@item
+integral/integral: we compare values as you would expect.
+
+@item
+fractional/integral: the fractional part is less than the integral one.
+Again, no surprise.
+
+@item
+fractional/fractional: the things become a bit more complex.
+If the common prefix contains only leading zeroes, the longest part is less
+than the other one; else the comparison behaves normally.
+@end itemize
+
+@smallexample
+strverscmp ("no digit", "no digit")
+    @result{} 0    // @r{same behavior as strcmp.}
+strverscmp ("item#99", "item#100")
+    @result{} <0   // @r{same prefix, but 99 < 100.}
+strverscmp ("alpha1", "alpha001")
+    @result{} >0   // @r{fractional part inferior to integral one.}
+strverscmp ("part1_f012", "part1_f01")
+    @result{} >0   // @r{two fractional parts.}
+strverscmp ("foo.009", "foo.0")
+    @result{} <0   // @r{idem, but with leading zeroes only.}
+@end smallexample
+
+This function is especially useful when dealing with filename sorting,
+because filenames frequently hold indices/version numbers.
+@end deftypefun
+
+*/
+
+/* states: S_N: normal, S_I: comparing integral part, S_F: comparing
+           fractional parts, S_Z: idem but with leading Zeroes only */
+#define  S_N    0x0
+#define  S_I    0x4
+#define  S_F    0x8
+#define  S_Z    0xC
+
+/* result_type: CMP: return diff; LEN: compare using len_diff/diff */
+#define  CMP    2
+#define  LEN    3
+
+
+/* Compare S1 and S2 as strings holding indices/version numbers,
+   returning less than, equal to or greater than zero if S1 is less than,
+   equal to or greater than S2 (for more info, see the Glibc texinfo doc).  */
+
+int
+strverscmp (const char *s1, const char *s2)
+{
+  const unsigned char *p1 = (const unsigned char *) s1;
+  const unsigned char *p2 = (const unsigned char *) s2;
+  unsigned char c1, c2;
+  int state;
+  int diff;
+
+  /* Symbol(s)    0       [1-9]   others  (padding)
+     Transition   (10) 0  (01) d  (00) x  (11) -   */
+  static const unsigned int next_state[] =
+    {
+      /* state    x    d    0    - */
+      /* S_N */  S_N, S_I, S_Z, S_N,
+      /* S_I */  S_N, S_I, S_I, S_I,
+      /* S_F */  S_N, S_F, S_F, S_F,
+      /* S_Z */  S_N, S_F, S_Z, S_Z
+    };
+
+  static const int result_type[] =
+    {
+      /* state   x/x  x/d  x/0  x/-  d/x  d/d  d/0  d/-
+                 0/x  0/d  0/0  0/-  -/x  -/d  -/0  -/- */
+
+      /* S_N */  CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
+                 CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
+      /* S_I */  CMP, -1,  -1,  CMP, +1,  LEN, LEN, CMP,
+                 +1,  LEN, LEN, CMP, CMP, CMP, CMP, CMP,
+      /* S_F */  CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
+                 CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
+      /* S_Z */  CMP, +1,  +1,  CMP, -1,  CMP, CMP, CMP,
+                 -1,  CMP, CMP, CMP
+    };
+
+  if (p1 == p2)
+    return 0;
+
+  c1 = *p1++;
+  c2 = *p2++;
+  /* Hint: '0' is a digit too.  */
+  state = S_N | ((c1 == '0') + (isdigit (c1) != 0));
+
+  while ((diff = c1 - c2) == 0 && c1 != '\0')
+    {
+      state = next_state[state];
+      c1 = *p1++;
+      c2 = *p2++;
+      state |= (c1 == '0') + (isdigit (c1) != 0);
+    }
+
+  state = result_type[state << 2 | (((c2 == '0') + (isdigit (c2) != 0)))];
+
+  switch (state)
+    {
+    case CMP:
+      return diff;
+      
+    case LEN:
+      while (isdigit (*p1++))
+	if (!isdigit (*p2++))
+	  return 1;
+      
+      return isdigit (*p2) ? -1 : diff;
+      
+    default:
+      return state;
+    }
+}
