libdspl-2.0
Библиотека алгоритмов цифровой обработки сигналов
readbin.c
1/*
2* Copyright (c) 2015-2022 Sergey Bakhurin
3* Digital Signal Processing Library [http://dsplib.org]
4*
5* This file is part of DSPL.
6*
7* is free software: you can redistribute it and/or modify
8* it under the terms of the GNU General Public License as published by
9* the Free Software Foundation, either version 3 of the License, or
10* (at your option) any later version.
11*
12* DSPL is distributed in the hope that it will be useful,
13* but WITHOUT ANY WARRANTY; without even the implied warranty of
14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15* GNU General Public License for more details.
16*
17* You should have received a copy of the GNU General Public License
18* along with Foobar. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include "dspl.h"
26
27
28
29#ifdef DOXYGEN_ENGLISH
165#endif
166#ifdef DOXYGEN_RUSSIAN
313#endif
314int DSPL_API readbin(char* fn, void** x, int* pn, int* pm, int* dtype)
315{
316 FILE* pFile = NULL;
317 int n, m, t, err;
318
319 if(!x || !pn || !pm)
320 return ERROR_PTR;
321 if(!fn)
322 return ERROR_FNAME;
323
324 pFile = fopen(fn, "rb");
325 if(pFile == NULL)
326 return ERROR_FOPEN;
327
328 if(fread(&t, sizeof(int), 1, pFile) != 1)
329 {
330 err = ERROR_FREAD_SIZE;
331 goto exit_label;
332 }
333
334 if(dtype)
335 *dtype = t;
336
337 if(fread(&n, sizeof(int), 1, pFile) != 1)
338 {
339 err = ERROR_FREAD_SIZE;
340 goto exit_label;
341 }
342 *pn = n;
343
344 if(fread(&m, sizeof(int), 1, pFile) != 1)
345 {
346 err = ERROR_FREAD_SIZE;
347 goto exit_label;
348 }
349 *pm = m;
350
351 switch(t)
352 {
353 case DAT_DOUBLE:
354 (*x) = (*x) ? realloc(*x, n*m*sizeof(double)) :
355 malloc(n*m*sizeof(double));
356 if(fread(*x, sizeof(double), n*m, pFile) != n*m)
357 {
358 err = ERROR_FREAD_SIZE;
359 goto exit_label;
360 }
361
362 break;
363 case DAT_COMPLEX:
364 (*x) = (*x) ? realloc(*x, n*m*sizeof(complex_t)) :
365 malloc(n*m*sizeof(complex_t));
366 if(fread(*x, sizeof(complex_t), n*m, pFile) != n*m)
367 {
368 err = ERROR_FREAD_SIZE;
369 goto exit_label;
370 }
371 break;
372 default:
373 err = ERROR_DAT_TYPE;
374 goto exit_label;
375 }
376 err = RES_OK;
377exit_label:
378 if(pFile)
379 fclose(pFile);
380 return err;
381}
382
#define ERROR_DAT_TYPE
Неверный тип данных. Данная ошибка возникает при сохранении данных в бинарный файл,...
Definition: dspl.h:566
#define RES_OK
Функция завершилась корректно. Ошибки отсутствуют.
Definition: dspl.h:558
#define ERROR_FNAME
Ошибка в имени файла. Необходимо задать корректное имя файла.
Definition: dspl.h:582
#define ERROR_PTR
Ошибка указателя. Данная ошибка означает, что один из обязательных указателей (память под который дол...
Definition: dspl.h:610
#define ERROR_FREAD_SIZE
Ошибка чтения блока данных из бинарного файла. Ошибка возникает, если при чтении блока данных из файл...
Definition: dspl.h:584
#define ERROR_FOPEN
Ошибка открытия файла. Файл с заданным именем не может быть открыт для записи и (или) чтения.
Definition: dspl.h:583
int readbin(char *fn, void **x, int *pn, int *pm, int *dtype)
Считать данные из бинарного файла
Definition: readbin.c:314
double complex_t[2]
Описание комплексного типа данных.
Definition: dspl.h:86