From 0a680007293883549c515cedeb5e683caaebf7b5 Mon Sep 17 00:00:00 2001
From: Jedidiah Barber <contact@jedbarber.id.au>
Date: Tue, 25 Jul 2023 22:25:14 +1200
Subject: Small tweaks to virtual example

---
 example/virtual_io_example.adb | 36 ++++++++++++++----------------------
 1 file changed, 14 insertions(+), 22 deletions(-)

diff --git a/example/virtual_io_example.adb b/example/virtual_io_example.adb
index add9b56..0561655 100644
--- a/example/virtual_io_example.adb
+++ b/example/virtual_io_example.adb
@@ -33,7 +33,7 @@ procedure Virtual_IO_Example is
 
 
     Virtual_File   : DIO.File_Type;
-    Current_Offset : Libsndfile.Count_Type := 1;
+    Current_Offset : Libsndfile.Count_Type := 0;
 
 
     function My_Length
@@ -49,20 +49,14 @@ procedure Virtual_IO_Example is
     begin
         case Whence is
             when Libsndfile.From_Start =>
-                Current_Offset := 1 + Offset;
+                Current_Offset := Offset;
             when Libsndfile.From_Current =>
                 Current_Offset := Current_Offset + Offset;
             when Libsndfile.From_End =>
-                Current_Offset := Libsndfile.Count_Type (DIO.Size (Virtual_File)) + Offset;
+                Current_Offset := My_Length - 1 + Offset;
         end case;
-        if Current_Offset < 1 or
-           Current_Offset > Libsndfile.Count_Type (DIO.Size (Virtual_File))
-        then
-            Current_Offset := 1;
-            return -1;
-        else
-            return Current_Offset;
-        end if;
+        Current_Offset := Current_Offset mod My_Length;
+        return Current_Offset;
     end My_Seek;
 
     function My_Read
@@ -73,13 +67,12 @@ procedure Virtual_IO_Example is
         Item : Interfaces.Unsigned_8;
         Data_Position : Integer := 1;
     begin
-        while Current_Offset <= Libsndfile.Count_Type (DIO.Size (Virtual_File)) and
-            Data_Position - 1 < Integer (Bytes)
-        loop
-            DIO.Read (Virtual_File, Item, DIO.Count (Current_Offset));
+        while Data_Position <= Integer (Bytes) loop
+            DIO.Read (Virtual_File, Item, DIO.Count (1 + Current_Offset));
             Data (Data_Position) := Item;
-            Current_Offset := Current_Offset + 1;
             Data_Position := Data_Position + 1;
+            exit when Current_Offset + 1 > My_Length;
+            Current_Offset := Current_Offset + 1;
         end loop;
         return Libsndfile.Count_Type (Data_Position - 1);
     end My_Read;
@@ -92,13 +85,12 @@ procedure Virtual_IO_Example is
         Item : Interfaces.Unsigned_8;
         Data_Position : Integer := 1;
     begin
-        while Current_Offset <= Libsndfile.Count_Type (DIO.Size (Virtual_File)) and
-            Data_Position - 1 < Integer (Bytes)
-        loop
+        while Data_Position <= Integer (Bytes) loop
             Item := Data (Data_Position);
-            DIO.Write (Virtual_File, Item, DIO.Count (Current_Offset));
-            Current_Offset := Current_Offset + 1;
+            DIO.Write (Virtual_File, Item, DIO.Count (1 + Current_Offset));
             Data_Position := Data_Position + 1;
+            exit when Current_Offset + 1 > My_Length;
+            Current_Offset := Current_Offset + 1;
         end loop;
         return Libsndfile.Count_Type (Data_Position - 1);
     end My_Write;
@@ -106,7 +98,7 @@ procedure Virtual_IO_Example is
     function My_Tell
         return Libsndfile.Count_Type is
     begin
-        return Current_Offset;
+        return Current_Offset + 1;
     end My_Tell;
 
 
-- 
cgit