xref: /trunk/main/solenv/bin/modules/par2script/files.pm (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1#*************************************************************************
2#
3# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4#
5# Copyright 2000, 2010 Oracle and/or its affiliates.
6#
7# OpenOffice.org - a multi-platform office productivity suite
8#
9# This file is part of OpenOffice.org.
10#
11# OpenOffice.org is free software: you can redistribute it and/or modify
12# it under the terms of the GNU Lesser General Public License version 3
13# only, as published by the Free Software Foundation.
14#
15# OpenOffice.org is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU Lesser General Public License version 3 for more details
19# (a copy is included in the LICENSE file that accompanied this code).
20#
21# You should have received a copy of the GNU Lesser General Public License
22# version 3 along with OpenOffice.org.  If not, see
23# <http://www.openoffice.org/license.html>
24# for a copy of the LGPLv3 License.
25#
26#*************************************************************************
27
28
29package par2script::files;
30
31use par2script::exiter;
32
33############################################
34# File Operations
35############################################
36
37sub check_file
38{
39    my ($arg) = @_;
40
41    if(!( -f $arg ))
42    {
43        par2script::exiter::exit_program("ERROR: Cannot find file $arg", "check_file");
44    }
45}
46
47sub read_file
48{
49    my ($localfile) = @_;
50
51    my @localfile = ();
52
53    open( IN, "<$localfile" ) || par2script::exiter::exit_program("ERROR: Cannot open file: $localfile", "read_file");
54    while ( <IN> ) { push(@localfile, $_); }
55    close( IN );
56
57    return \@localfile;
58}
59
60###########################################
61# Saving files, arrays and hashes
62###########################################
63
64sub save_file
65{
66    my ($savefile, $savecontent) = @_;
67    open( OUT, ">$savefile" );
68    print OUT @{$savecontent};
69    close( OUT);
70    if (! -f $savefile) { pre2par::exiter::exit_program("ERROR: Cannot write file: $savefile", "save_file"); }
71}
72
73sub save_hash
74{
75    my ($savefile, $hashref) = @_;
76
77    my @printcontent = ();
78
79    my ($itemkey, $itemvalue, $line);
80
81    foreach $itemkey ( keys %{$hashref} )
82    {
83        $line = "";
84        $itemvalue = $hashref->{$itemkey};
85        $line = $itemkey . "=" . $itemvalue . "\n";
86        push(@printcontent, $line);
87    }
88
89    open( OUT, ">$savefile" );
90    print OUT @printcontent;
91    close( OUT);
92}
93
94sub save_array_of_hashes
95{
96    my ($savefile, $arrayref) = @_;
97
98    my @printcontent = ();
99
100    my ($itemkey, $itemvalue, $line, $hashref);
101
102    for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
103    {
104        $line = "";
105        $hashref = ${$arrayref}[$i];
106
107        foreach $itemkey ( keys %{$hashref} )
108        {
109            $itemvalue = $hashref->{$itemkey};
110
111            $line = $line . $itemkey . "=" . $itemvalue . "\t";
112        }
113
114        $line = $line . "\n";
115
116        push(@printcontent, $line);
117    }
118
119    open( OUT, ">$savefile" );
120    print OUT @printcontent;
121    close( OUT);
122}
123
1241;
125