~ubuntu-branches/debian/stretch/alpine/stretch

« back to all changes in this revision

Viewing changes to pith/readfile.c

  • Committer: Bazaar Package Importer
  • Author(s): Asheesh Laroia
  • Date: 2007-02-17 13:17:42 UTC
  • Revision ID: james.westby@ubuntu.com-20070217131742-99x5c6cpg1pbkdhw
Tags: upstream-0.82+dfsg
ImportĀ upstreamĀ versionĀ 0.82+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#if !defined(lint) && !defined(DOS)
 
2
static char rcsid[] = "$Id: readfile.c 237 2006-11-16 04:08:15Z mikes@u.washington.edu $";
 
3
#endif
 
4
 
 
5
/*
 
6
 * ========================================================================
 
7
 * Copyright 2006 University of Washington
 
8
 *
 
9
 * Licensed under the Apache License, Version 2.0 (the "License");
 
10
 * you may not use this file except in compliance with the License.
 
11
 * You may obtain a copy of the License at
 
12
 *
 
13
 *     http://www.apache.org/licenses/LICENSE-2.0
 
14
 *
 
15
 * ========================================================================
 
16
 */
 
17
 
 
18
#include "../pith/headers.h"
 
19
 
 
20
#include "../pith/store.h"
 
21
 
 
22
#include "readfile.h"
 
23
 
 
24
 
 
25
/*----------------------------------------------------------------------
 
26
    Read whole file into memory
 
27
 
 
28
  Args: filename -- path name of file to read
 
29
 
 
30
  Result: Returns pointer to malloced memory with the contents of the file
 
31
          or NULL
 
32
 
 
33
This won't work very well if the file has NULLs in it.
 
34
 ----*/
 
35
char *
 
36
read_file(char *filename, int so_get_flags)
 
37
{
 
38
    STORE_S *in_file = NULL, *out_store = NULL;
 
39
    unsigned char c;
 
40
    char *return_text = NULL;
 
41
 
 
42
    if((in_file = so_get(FileStar, filename, so_get_flags | READ_ACCESS))){
 
43
 
 
44
 
 
45
        if(!(out_store = so_get(CharStar, NULL, EDIT_ACCESS))){
 
46
            so_give(&in_file);
 
47
            return NULL;
 
48
        }
 
49
 
 
50
        /*
 
51
         * We're just using the READ_FROM_LOCALE flag to translate
 
52
         * to UTF-8.
 
53
         */
 
54
        while(so_readc(&c, in_file))
 
55
          so_writec(c, out_store);
 
56
 
 
57
        if(in_file)
 
58
          so_give(&in_file);
 
59
 
 
60
        if(out_store){
 
61
            return_text = (char *) so_text(out_store);
 
62
            /* avoid freeing this */
 
63
            if(out_store->txt)
 
64
              out_store->txt = NULL;
 
65
 
 
66
            so_give(&out_store);
 
67
        }
 
68
    }
 
69
 
 
70
    return(return_text);
 
71
}