Formula/Config/INI.pm untested

filter: Statementstmt 0.0% 0 / 74 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 148 CC 11 SCAR 32.7
CC
11
Cov
0%
_serialize
72.0
_unpack
20.0
save
2.0
CRAP 26.3
1 # This code is part of Perl distribution Math-Formula version 0.18.
2 # The POD got stripped from this file by OODoc version 3.03.
3 # For contributors see file ChangeLog.
4  
5 # This software is copyright (c) 2023-2025 by Mark Overmeer.
6  
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10  
11 #oodist: *** DO NOT USE THIS VERSION FOR PRODUCTION ***
12 #oodist: This file contains OODoc-style documentation which will get stripped
13 #oodist: during its release in the distribution. You can use this file for
14 #oodist: testing, however the code of this development version may be broken!
15  
16 package Math::Formula::Config::INI;{
17 our $VERSION = '0.18';
18 }
19  
20 use parent 'Math::Formula::Config';
21  
22 use warnings;
23 use strict;
24  
25 use Log::Report 'math-formula';
26 use Scalar::Util 'blessed';
27  
28 use Config::INI::Writer ();
29 use Config::INI::Reader ();
30  
31 use Math::Formula::Context ();
32 use Math::Formula ();
33  
34 #--------------------
35  
36 #--------------------
37  
38 sub save($%)
39 { my ($self, $context, %args) = @_;
40 my $name = $context->name;
41  
42 my $index = $context->_index;
43 my %tree = (
44 _ => $self->_set_encode($index->{attributes}),
45 formulas => $self->_set_encode($index->{formulas}),
46 );
47  
48 my $fn = $self->path_for($args{filename} || "$name.ini");
49 Config::INI::Writer->write_file(\%tree, $fn);
50 }
51  
52 sub _set_encode($)
53 { my ($self, $set) = @_;
54 my %data;
55 $data{$_ =~ s/^ctx_//r} = $self->_serialize($_, $set->{$_}) for keys %$set;
56 \%data;
57 }
58  
59 sub _double_quoted($) { '"' . ($_[0] =~ s/"/\\"/gr) . '"' }
60  
61 sub _serialize($$)
62 { my ($self, $name, $what) = @_;
63 my %attrs;
64  
65 if(blessed $what && $what->isa('Math::Formula'))
66 { if(my $r = $what->returns) { $attrs{returns} = $r };
67 $what = $what->expression;
68 }
69  
70 my $v = '';
71 if(blessed $what && $what->isa('Math::Formula::Type'))
72 { # No attributes possible for simple types
73 return $what->value
74 if $what->isa('MF::STRING') || $what->isa('MF::FLOAT') || $what->isa('MF::INTEGER');
75  
76 $v = _double_quoted($what->token);
77 }
78 elsif(ref $what eq 'CODE')
79 { warning __x"cannot (yet) save CODE, skipped '{name}'", name => $name;
80 return undef;
81 }
82 elsif(length $what)
83 { $v = _double_quoted $what;
84 }
85  
86 if(keys %attrs)
87 { $v .= '; ' . (join ', ', map "$_='$attrs{$_}'", sort keys %attrs);
88 }
89  
90 return $v;
91 }
92  
93  
94 sub load($%)
95 { my ($self, $name, %args) = @_;
96 my $fn = $self->path_for($args{filename} || "$name.ini");
97  
98 my $read = Config::INI::Reader->read_file($fn);
99 my $attrs = $self->_set_decode($read->{_});
100 Math::Formula::Context->new(name => $name, %$attrs,
101 formulas => $self->_set_decode($read->{formulas}),
102 );
103 }
104  
105 sub _set_decode($)
106 { my ($self, $set) = @_;
107 $set or return {};
108  
109 my %forms;
110 $forms{$_} = $self->_unpack($_, $set->{$_}) for keys %$set;
111 \%forms;
112 }
113  
114 sub _unpack($$)
115 { my ($self, $name, $encoded) = @_;
116 my $dummy = Math::Formula->new('dummy', '7');
117  
118 if($encoded =~ m/^"(.*?)"(?:;\s*(.*))?$/)
119 { my ($expr, $attrs) = ($1, $2 // '');
120 my %attrs = $attrs =~ m/(\w+)\='([^']+)'/g;
121 return Math::Formula->new($name, $expr =~ s/\\"/"/gr, %attrs);
122 }
123  
124 $encoded =~ qr/^[0-9]+$/ ? MF::INTEGER->new($encoded)
125 : $encoded =~ qr/^[0-9][0-9.e+\-]+$/ ? MF::FLOAT->new($encoded)
126 : MF::STRING->new(undef, $encoded);
127 }
128  
129 #--------------------
130  
131 1;