51Degrees Device Detection C/C++  4.1

A device detection library that is used natively or by 51Degrees products


1 /* *********************************************************************
2  * This Original Work is copyright of 51 Degrees Mobile Experts Limited.
3  * Copyright 2019 51 Degrees Mobile Experts Limited, 5 Charlotte Close,
4  * Caversham, Reading, Berkshire, United Kingdom RG4 7BY.
5  *
6  * This Original Work is licensed under the European Union Public Licence (EUPL)
7  * v.1.2 and is subject to its terms as set out below.
8  *
9  * If a copy of the EUPL was not distributed with this file, You can obtain
10  * one at https://opensource.org/licenses/EUPL-1.2.
11  *
12  * The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
13  * amended by the European Commission) shall be deemed incompatible for
14  * the purposes of the Work and the provisions of the compatibility
15  * clause in Article 5 of the EUPL shall not apply.
16  *
17  * If using the Work as, or as part of, a network application, by
18  * including the attribution notice(s) required under Article 5 of the EUPL
19  * in the end user terms of the application under an appropriate heading,
20  * such notice(s) shall fulfill the requirements of that article.
21  * ********************************************************************* */
147 /* Define NDEBUG if needed, to ensure asserts are disabled in release builds */
148 #if !defined(DEBUG) && !defined(_DEBUG) && !defined(NDEBUG)
149 #define NDEBUG
150 #endif
152 #include <limits.h>
153 #include <stdio.h>
154 #include <errno.h>
155 #include <stdint.h>
156 #include <stdbool.h>
157 #ifdef _MSC_VER
158 #include <windows.h>
159 #include <direct.h>
160 #include <tchar.h>
161 #else
162 #include <dirent.h>
163 #include <sys/stat.h>
164 #endif
165 #include <time.h>
166 #include <assert.h>
167 #include <limits.h>
168 #include <time.h>
169 #include "data.h"
170 #include "exceptions.h"
171 #include "status.h"
172 #include "memory.h"
173 #include "pool.h"
174 #include "threading.h"
179 #ifdef _MSC_FULL_VER
180 #define GetCurrentDir _getcwd
181 #include <windows.h>
182 #include <direct.h>
183 #include <tchar.h>
184 #else
185 #define GetCurrentDir getcwd
186 #include <unistd.h>
187 #include <dirent.h>
188 #include <sys/stat.h>
189 #endif
199 #ifdef _MSC_FULL_VER
201 #else
203 #endif
205 #ifdef __cplusplus
206 #define EXTERNAL extern "C"
207 #else
208 #define EXTERNAL
209 #endif
214 typedef union fiftyone_degrees_file_handle_t {
215  FILE *file;
222  typedef struct fiftyone_degrees_file_pool_t {
224  long length;
242  const char* fileName,
243  FILE** handle);
253  const char* fileName,
254  const void *data,
255  const size_t length);
267  const char *source,
268  const char *destination);
277  const char *fileName);
286  const char *pathName);
304  const char *dataFolderName,
305  const char *fileName,
306  char *destination,
307  size_t size);
326  const char *masterFile,
327  const char **paths,
328  int count,
329  long bytesToCompare,
330  const char *destination);
350  const char *masterFileName,
351  const char **paths,
352  int count,
353  long bytesToCompare);
368  const char *masterFile,
369  const char **paths,
370  int count,
371  const char *destination);
385  fiftyoneDegreesFilePool *filePool,
386  const char *fileName,
387  uint16_t concurrency,
388  fiftyoneDegreesException *exception);
401  fiftyoneDegreesFilePool *filePool,
402  fiftyoneDegreesException *exception);
410  fiftyoneDegreesFileHandle* handle);
418 EXTERNAL long fiftyoneDegreesFileGetSize(const char *fileName);
429  const char *fileName,
443 EXTERNAL const char* fiftyoneDegreesFileGetFileName(const char *filePath);
448 #endif
fiftyoneDegreesStatusCode fiftyoneDegreesFileCreateTempFile(const char *masterFile, const char **paths, int count, const char *destination)
Create a temporary file containing a copy of the master file using the first writable path in the lis...
const char * fiftyoneDegreesFileGetFileName(const char *filePath)
Gets the last, file name, segment of the full file path.
bool fiftyoneDegreesFileGetExistingTempFile(const char *masterFile, const char **paths, int count, long bytesToCompare, const char *destination)
Gets the path to a temporary file which is an exact copy of the master file if one exists.
fiftyoneDegreesStatusCode fiftyoneDegreesFilePoolInit(fiftyoneDegreesFilePool *filePool, const char *fileName, uint16_t concurrency, fiftyoneDegreesException *exception)
Initialises the pool with a stack of open read only file handles all associated with the file name.
Status returned from the initialisation of a resource.
Definition: status.h:82
Pool item node in the stack of items.
Definition: pool.h:179
fiftyoneDegreesStatusCode fiftyoneDegreesFileGetPath(const char *dataFolderName, const char *fileName, char *destination, size_t size)
Iterates up the folders from the current working directory until a file in the sub folder dataFolderN...
long fiftyoneDegreesFileGetSize(const char *fileName)
Returns the size of a file in bytes, or -1 if the file does not exist or cannot be accessed.
void fiftyoneDegreesFileHandleRelease(fiftyoneDegreesFileHandle *handle)
Returns a handle previously retrieved via fiftyoneDegreesFileHandleGet back to the pool.
Used to read data from memory in a similar manner to a file handle.
Definition: memory.h:58
FILE * file
Open read handle to the source data file.
Definition: file.h:215
fiftyoneDegreesPoolItem item
The pool item with the resource.
Definition: file.h:216
void fiftyoneDegreesFilePoolReset(fiftyoneDegreesFilePool *filePool)
Resets the pool without releasing any resources.
fiftyoneDegreesStatusCode fiftyoneDegreesFileOpen(const char *fileName, FILE **handle)
Opens the file path provided placing the file handle in the handle parameter.
fiftyoneDegreesPool pool
The pool of file handles.
Definition: file.h:223
fiftyoneDegreesFileHandle * fiftyoneDegreesFileHandleGet(fiftyoneDegreesFilePool *filePool, fiftyoneDegreesException *exception)
Retrieves a read only open file handle from the pool.
Pool of resources stored as items in a stack.
Definition: pool.h:199
fiftyoneDegreesStatusCode fiftyoneDegreesFileWrite(const char *fileName, const void *data, const size_t length)
Writes binary data to the file path provided, closing the file once finished.
void fiftyoneDegreesFilePoolRelease(fiftyoneDegreesFilePool *pool)
Releases the file handles contained in the pool and frees any internal memory used by the pool.
File handle node in the stack of handles.
Definition: file.h:214
fiftyoneDegreesStatusCode fiftyoneDegreesFileReadToByteArray(const char *fileName, fiftyoneDegreesMemoryReader *reader)
Reads the contents of a file into memory.
fiftyoneDegreesStatusCode fiftyoneDegreesFileCreateDirectory(const char *pathName)
Creates a directory with the specified path, and returns the result of the operation.
long length
Length of the file in bytes.
Definition: file.h:224
Structure used to represent a 51Degrees exception and passed into methods that might generate excepti...
Definition: exceptions.h:116
fiftyoneDegreesStatusCode fiftyoneDegreesFileDelete(const char *fileName)
Delete a file from the file system.
Stack of handles used to read data from a single source file.
Definition: file.h:222
fiftyoneDegreesStatusCode fiftyoneDegreesFileCopy(const char *source, const char *destination)
Copy a file from one location to another.
int fiftyoneDegreesFileDeleteUnusedTempFiles(const char *masterFileName, const char **paths, int count, long bytesToCompare)
Finds all the temporary files which is an exact copy of the master file if any exist.