Skip to content

Commit b8441cd

Browse files
committed
CHUTIL: replace exception by Result
1 parent 6a2ab91 commit b8441cd

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

CodeHawk/CH/chutil/cHFileIO.ml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ open CHPretty
3333

3434
(* chutil *)
3535
open CHPrettyUtil
36+
open CHTraceResult
37+
3638

3739
exception CHIOFailure of string
3840

@@ -242,7 +244,7 @@ let absolute_name (s: string): string =
242244
s
243245

244246

245-
let rec normalize_path (s: string): string =
247+
let rec normalize_path (s: string): string traceresult =
246248
let has_directory_dot s =
247249
(String.contains s '.')
248250
&& (let dotindex = String.index s '.' in
@@ -252,7 +254,7 @@ let rec normalize_path (s: string): string =
252254
|| ((slashindex - dotindex = 2) && String.get s (dotindex+1) = '.'))) in
253255
let len = String.length s in
254256
if len <= 1 then
255-
s
257+
Ok s
256258
else
257259
if has_directory_dot s then
258260
let dotindex = String.index s '.' in
@@ -268,16 +270,16 @@ let rec normalize_path (s: string): string =
268270
normalize_path (String.concat "" [s1; s2])
269271
else if slsindex > 0 then
270272
normalize_path (String.sub s (dotindex + 3) (len - (dotindex + 3)))
271-
else (* no second slash found *)
272-
raise (CHFailure (LBLOCK [STR "Error in normalize_path: "; STR s]))
273+
else (* no second slash found *)
274+
Error ["normalize_path: " ^ s ^ "; expected second slash"]
273275
else (* no first slash found *)
274-
raise (CHFailure (LBLOCK [STR "Error in normalize_path: "; STR s]))
276+
Error ["normalize_path: " ^ s ^ "; expected a slash"]
275277
else (* just one dot *)
276278
if dotindex + 1 < len && String.get s (dotindex+1) = '/' then
277279
let s1 = String.sub s 0 dotindex in
278280
let s2 = String.sub s (dotindex + 2) (len - (dotindex + 2)) in
279281
normalize_path (String.concat "" [s1; s2])
280282
else
281-
raise (CHFailure (LBLOCK [STR "Error in normalize_path: "; STR s]))
283+
Error ["normalize_path: " ^ s ^ "; multiple dots"]
282284
else (* no dots *)
283-
s
285+
Ok s

CodeHawk/CH/chutil/cHFileIO.mli

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Author: Henny Sipma
44
------------------------------------------------------------------------------
55
The MIT License (MIT)
6-
6+
77
Copyright (c) 2005-2019 Kestrel Technology LLC
88
Copyright (c) 2020-2021 Henny B. Sipma
99
Copyright (c) 2022-2024 Aarno Labs LLC
@@ -14,10 +14,10 @@
1414
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1515
copies of the Software, and to permit persons to whom the Software is
1616
furnished to do so, subject to the following conditions:
17-
17+
1818
The above copyright notice and this permission notice shall be included in all
1919
copies or substantial portions of the Software.
20-
20+
2121
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2222
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2323
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -30,6 +30,10 @@
3030
(* chlib *)
3131
open CHPretty
3232

33+
(* chutil *)
34+
open CHTraceResult
35+
36+
3337
exception CHIOFailure of string
3438

3539
class type file_output_int =
@@ -48,12 +52,13 @@ val relativize_filename: string -> string -> string
4852
(* returns the filename relative to the given directory;
4953
if the filename is absolute, but not in a subdirectory
5054
of the given directory the function fails *)
51-
55+
56+
5257
val absolute_name: string -> string
5358
(* add the current working directory to the filename *)
5459

55-
val normalize_path: string -> string
60+
61+
val normalize_path: string -> string traceresult
5662
(* remove . and .. from path names; if the filename
5763
is relative the function fails if removing .. would result
5864
in a parent directory of the path *)
59-

0 commit comments

Comments
 (0)