From 5333e92d7881b4dcb2d70a15b71c55d42c80c5e6 Mon Sep 17 00:00:00 2001
From: biopsichas <svajunas.plunge@gmail.com>
Date: Tue, 16 Jul 2024 14:24:18 +0300
Subject: [PATCH] get_file_version function added

---
 DESCRIPTION             |  4 ++--
 NAMESPACE               |  1 +
 R/helper.R              | 27 +++++++++++++++++++++++++++
 man/get_file_version.Rd | 29 +++++++++++++++++++++++++++++
 4 files changed, 59 insertions(+), 2 deletions(-)
 create mode 100644 man/get_file_version.Rd

diff --git a/DESCRIPTION b/DESCRIPTION
index 4fc1887..b2c44a5 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -1,7 +1,7 @@
 Package: SWATdoctR
 Type: Package
 Title: Finding the right diagnoses and treatments for SWAT+ models
-Version: 0.1.24
+Version: 0.1.25
 Author: c(person("Christoph", "Schürz",
              email = "christoph.schuerz@ufz.de",
              role = c("aut", "cre")),
@@ -34,4 +34,4 @@ Suggests:
 License: GPL-3
 Encoding: UTF-8
 LazyData: true
-RoxygenNote: 7.3.1
+RoxygenNote: 7.3.2
diff --git a/NAMESPACE b/NAMESPACE
index a91bc53..24f8692 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -3,6 +3,7 @@
 export(add_kill_op)
 export(check_hru_waterbalance)
 export(filter_hru_at_harvkill)
+export(get_file_version)
 export(get_hru_id_by_attribute)
 export(plot_basin_var)
 export(plot_climate_annual)
diff --git a/R/helper.R b/R/helper.R
index bdfb9f1..b33f0ff 100644
--- a/R/helper.R
+++ b/R/helper.R
@@ -114,6 +114,33 @@ remove_tail <- function(f, pattern){
   return(f)
 }
 
+#' Get the Highest Version Number of a File
+#'
+#' This function searches for files in a specified directory that match a given
+#' pattern and extracts the highest version number from those files.
+#'
+#' @param parent_dir A string representing the path to the directory where the
+#' files are located.
+#' @param file_name A string representing the setup name.
+#' @param add A string to be added to the file name pattern to match.
+#' @return The highest version number found among the matching files as a
+#' numeric value. Returns 0 if no matching files are found.
+#' @export
+#' @examples
+#' \dontrun{
+#' file_version <- get_file_version(parent_dir, save_name, '_nostr_') + 1
+#' }
+
+get_file_version <- function(parent_dir, file_name, add) {
+  files <- list.files(path = parent_dir,
+                      pattern = paste0(file_name, add, '\\d{1,2}', '\\.rds'))
+  vers_val <- as.numeric(gsub(paste(file_name, add, '\\.rds', sep = '|'),
+                              '', files))
+  return(max(vers_val, 0))
+}
+
+#' @keywords internal
+
 lwr <- function(df){
   names(df) <- tolower(names(df))
   return(df)
diff --git a/man/get_file_version.Rd b/man/get_file_version.Rd
new file mode 100644
index 0000000..85e693b
--- /dev/null
+++ b/man/get_file_version.Rd
@@ -0,0 +1,29 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/helper.R
+\name{get_file_version}
+\alias{get_file_version}
+\title{Get the Highest Version Number of a File}
+\usage{
+get_file_version(parent_dir, file_name, add)
+}
+\arguments{
+\item{parent_dir}{A string representing the path to the directory where the
+files are located.}
+
+\item{file_name}{A string representing the setup name.}
+
+\item{add}{A string to be added to the file name pattern to match.}
+}
+\value{
+The highest version number found among the matching files as a
+numeric value. Returns 0 if no matching files are found.
+}
+\description{
+This function searches for files in a specified directory that match a given
+pattern and extracts the highest version number from those files.
+}
+\examples{
+\dontrun{
+file_version <- get_file_version(parent_dir, save_name, '_nostr_') + 1
+}
+}
-- 
GitLab