2015-10-18 10:43:40 +02:00
|
|
|
/*
|
|
|
|
This file is part of libcapwap.
|
|
|
|
|
|
|
|
libcapwap is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
libcapwap is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with libcapwap. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-04-20 07:55:49 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2015-10-18 10:43:40 +02:00
|
|
|
#include "file.h"
|
|
|
|
|
2015-05-04 07:28:21 +02:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a file from disk to memory
|
|
|
|
* @param filename name of file to load
|
|
|
|
* @param size variable which receives the size of the file in bytes
|
|
|
|
* @return a pointer to the memory where the file was loaded in \n
|
2015-10-18 10:32:01 +02:00
|
|
|
* The memory allocated returned in data must be freed using free.
|
2015-05-04 07:28:21 +02:00
|
|
|
*
|
|
|
|
* Eexample:
|
2018-03-06 18:28:31 +01:00
|
|
|
* \code{.c}
|
2015-05-04 07:28:21 +02:00
|
|
|
#include "capwap/file.h"
|
|
|
|
size_t bytes;
|
|
|
|
char * data = cw_load_file("file.txt",&bytes);
|
2015-10-18 10:32:01 +02:00
|
|
|
if (data){
|
2018-03-06 18:28:31 +01:00
|
|
|
@startcomment Do something with data ... @endcomment
|
2015-05-04 07:28:21 +02:00
|
|
|
free (data);
|
2015-10-18 10:32:01 +02:00
|
|
|
}
|
2015-05-04 07:28:21 +02:00
|
|
|
\endcode
|
2015-10-18 10:32:01 +02:00
|
|
|
*/
|
|
|
|
char *cw_load_file(const char *filename, size_t * size)
|
2015-04-20 07:55:49 +02:00
|
|
|
{
|
2018-03-06 18:28:31 +01:00
|
|
|
char *buf;
|
|
|
|
FILE *infile;
|
|
|
|
infile = fopen(filename, "rb");
|
2015-10-18 10:32:01 +02:00
|
|
|
if (!infile)
|
2015-04-20 07:55:49 +02:00
|
|
|
return NULL;
|
2015-10-18 10:32:01 +02:00
|
|
|
fseek(infile, 0, SEEK_END);
|
2015-04-20 07:55:49 +02:00
|
|
|
*size = ftell(infile);
|
2018-03-06 18:28:31 +01:00
|
|
|
buf = malloc(*size);
|
2015-10-18 10:32:01 +02:00
|
|
|
if (!buf)
|
2015-04-20 07:55:49 +02:00
|
|
|
goto errX;
|
|
|
|
|
2015-10-18 10:32:01 +02:00
|
|
|
fseek(infile, 0, SEEK_SET);
|
|
|
|
*size = fread(buf, 1, *size, infile);
|
|
|
|
errX:
|
2015-04-20 07:55:49 +02:00
|
|
|
fclose(infile);
|
|
|
|
return buf;
|
|
|
|
}
|