| 1 | # Copyrights 2023 by [Mark Overmeer <markov@cpan.org>]. | ||
| 2 | # For other contributors see ChangeLog. | ||
| 3 | # See the manual pages for details on the licensing terms. | ||
| 4 | # Pod stripped from pm file by OODoc 2.03. | ||
| 5 | package Math::Formula::Config::INI; | ||
| 6 | use vars '$VERSION'; | ||
| 7 | $VERSION = '0.16'; | ||
| 8 | |||
| 9 | use base 'Math::Formula::Config'; | ||
| 10 | |||
| 11 | use warnings; | ||
| 12 | use strict; | ||
| 13 | |||
| 14 | use Log::Report 'math-formula'; | ||
| 15 | use Scalar::Util 'blessed'; | ||
| 16 | |||
| 17 | use Config::INI::Writer (); | ||
| 18 | use Config::INI::Reader (); | ||
| 19 | |||
| 20 | use Math::Formula::Context (); | ||
| 21 | use Math::Formula (); | ||
| 22 | |||
| 23 | |||
| 24 | #---------------------- | ||
| 25 | |||
| 26 | sub save($%) | ||
| 27 | { my ($self, $context, %args) = @_; | ||
| 28 | my $name = $context->name; | ||
| 29 | |||
| 30 | my $index = $context->_index; | ||
| 31 | my %tree = ( | ||
| 32 | _ => $self->_set_encode($index->{attributes}), | ||
| 33 | formulas => $self->_set_encode($index->{formulas}), | ||
| 34 | ); | ||
| 35 | |||
| 36 | my $fn = $self->path_for($args{filename} || "$name.ini"); | ||
| 37 | Config::INI::Writer->write_file(\%tree, $fn); | ||
| 38 | } | ||
| 39 | |||
| 40 | sub _set_encode($) | ||
| 41 | { my ($self, $set) = @_; | ||
| 42 | my %data; | ||
| 43 | $data{$_ =~ s/^ctx_//r} = $self->_serialize($_, $set->{$_}) for keys %$set; | ||
| 44 | \%data; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub _double_quoted($) { '"' . ($_[0] =~ s/"/\\"/gr) . '"' } | ||
| 48 | |||
| 49 | sub _serialize($$) | ||
| 50 | { my ($self, $name, $what) = @_; | ||
| 51 | my %attrs; | ||
| 52 | |||
| 53 | if(blessed $what && $what->isa('Math::Formula')) | ||
| 54 | { if(my $r = $what->returns) { $attrs{returns} = $r }; | ||
| 55 | $what = $what->expression; | ||
| 56 | } | ||
| 57 | |||
| 58 | my $v = ''; | ||
| 59 | if(blessed $what && $what->isa('Math::Formula::Type')) | ||
| 60 | { # No attributes possible for simple types | ||
| 61 | return $what->value | ||
| 62 | if $what->isa('MF::STRING') || $what->isa('MF::FLOAT') || $what->isa('MF::INTEGER'); | ||
| 63 | |||
| 64 | $v = _double_quoted($what->token); | ||
| 65 | } | ||
| 66 | elsif(ref $what eq 'CODE') | ||
| 67 | { warning __x"cannot (yet) save CODE, skipped '{name}'", name => $name; | ||
| 68 | return undef; | ||
| 69 | } | ||
| 70 | elsif(length $what) | ||
| 71 | { $v = _double_quoted $what; | ||
| 72 | } | ||
| 73 | |||
| 74 | if(keys %attrs) | ||
| 75 | { $v .= '; ' . (join ', ', map "$_='$attrs{$_}'", sort keys %attrs); | ||
| 76 | } | ||
| 77 | |||
| 78 | return $v; | ||
| 79 | } | ||
| 80 | |||
| 81 | |||
| 82 | sub load($%) | ||
| 83 | { my ($self, $name, %args) = @_; | ||
| 84 | my $fn = $self->path_for($args{filename} || "$name.ini"); | ||
| 85 | |||
| 86 | my $read = Config::INI::Reader->read_file($fn); | ||
| 87 | my $attrs = $self->_set_decode($read->{_}); | ||
| 88 | Math::Formula::Context->new(name => $name, | ||
| 89 | %$attrs, | ||
| 90 | formulas => $self->_set_decode($read->{formulas}), | ||
| 91 | ); | ||
| 92 | } | ||
| 93 | |||
| 94 | sub _set_decode($) | ||
| 95 | { my ($self, $set) = @_; | ||
| 96 | $set or return {}; | ||
| 97 | |||
| 98 | my %forms; | ||
| 99 | $forms{$_} = $self->_unpack($_, $set->{$_}) for keys %$set; | ||
| 100 | \%forms; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub _unpack($$) | ||
| 104 | { my ($self, $name, $encoded) = @_; | ||
| 105 | my $dummy = Math::Formula->new('dummy', '7'); | ||
| 106 | |||
| 107 | if($encoded =~ m/^"(.*?)"(?:;\s*(.*))?$/) | ||
| 108 | { my ($expr, $attrs) = ($1, $2 // ''); | ||
| 109 | my %attrs = $attrs =~ m/(\w+)\='([^']+)'/g; | ||
| 110 | return Math::Formula->new($name, $expr =~ s/\\"/"/gr, %attrs); | ||
| 111 | } | ||
| 112 | |||
| 113 | $encoded =~ qr/^[0-9]+$/ ? MF::INTEGER->new($encoded) | ||
| 114 | : $encoded =~ qr/^[0-9][0-9.e+\-]+$/ ? MF::FLOAT->new($encoded) | ||
| 115 | : MF::STRING->new(undef, $encoded); | ||
| 116 | } | ||
| 117 | |||
| 118 | #---------------------- | ||
| 119 | |||
| 120 | 1; |