2 * Copyright 2001-2004 Brandon Long
5 * ClearSilver Templating System
7 * This code is made available under the terms of the ClearSilver License.
8 * http://www.clearsilver.net/license.hdf
12 #include "cs_config.h"
16 #include <sys/types.h>
35 #include "neo_files.h"
37 NEOERR
*ne_load_file_len (const char *path
, char **str
, int *out_len
)
45 if (out_len
) *out_len
= 0;
47 if (stat(path
, &s
) == -1)
50 return nerr_raise (NERR_NOT_FOUND
, "File %s not found", path
);
51 return nerr_raise_errno (NERR_SYSTEM
, "Unable to stat file %s", path
);
54 if (s
.st_size
>= INT_MAX
)
55 return nerr_raise (NERR_ASSERT
, "File %s too large (%ld >= INT_MAX)",
59 return nerr_raise (NERR_ASSERT
, "File %s size error? (%ld < 0)", path
,
62 fd
= open (path
, O_RDONLY
);
65 return nerr_raise_errno (NERR_SYSTEM
, "Unable to open file %s", path
);
68 *str
= (char *) malloc (len
+ 1);
73 return nerr_raise (NERR_NOMEM
,
74 "Unable to allocate memory (%d) to load file %s", len
+ 1, path
);
76 if ((bytes_read
= read (fd
, *str
, len
)) == -1)
80 return nerr_raise_errno (NERR_SYSTEM
, "Unable to read file %s", path
);
83 (*str
)[bytes_read
] = '\0';
85 if (out_len
) *out_len
= bytes_read
;
90 NEOERR
*ne_load_file (const char *path
, char **str
) {
91 return ne_load_file_len (path
, str
, NULL
);