libdspl-2.0
Библиотека алгоритмов цифровой обработки сигналов
readbin.c
1 /*
2 * Copyright (c) 2015-2024 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
30 
168 #endif
169 #ifdef DOXYGEN_RUSSIAN
170 
317 #endif
318 int DSPL_API readbin(char* fn, void** x, int* pn, int* pm, int* dtype)
319 {
320  FILE* pFile = NULL;
321  int n, m, t, err;
322 
323  if(!x || !pn || !pm)
324  return ERROR_PTR;
325  if(!fn)
326  return ERROR_FNAME;
327 
328  pFile = fopen(fn, "rb");
329  if(pFile == NULL)
330  return ERROR_FOPEN;
331 
332  if(fread(&t, sizeof(int), 1, pFile) != 1)
333  {
334  err = ERROR_FREAD_SIZE;
335  goto exit_label;
336  }
337 
338  if(dtype)
339  *dtype = t;
340 
341  if(fread(&n, sizeof(int), 1, pFile) != 1)
342  {
343  err = ERROR_FREAD_SIZE;
344  goto exit_label;
345  }
346  *pn = n;
347 
348  if(fread(&m, sizeof(int), 1, pFile) != 1)
349  {
350  err = ERROR_FREAD_SIZE;
351  goto exit_label;
352  }
353  *pm = m;
354 
355  switch(t)
356  {
357  case DAT_DOUBLE:
358  (*x) = (*x) ? realloc(*x, n*m*sizeof(double)) :
359  malloc(n*m*sizeof(double));
360  if(fread(*x, sizeof(double), n*m, pFile) != n*m)
361  {
362  err = ERROR_FREAD_SIZE;
363  goto exit_label;
364  }
365 
366  break;
367  case DAT_COMPLEX:
368  (*x) = (*x) ? realloc(*x, n*m*sizeof(complex_t)) :
369  malloc(n*m*sizeof(complex_t));
370  if(fread(*x, sizeof(complex_t), n*m, pFile) != n*m)
371  {
372  err = ERROR_FREAD_SIZE;
373  goto exit_label;
374  }
375  break;
376  default:
377  err = ERROR_DAT_TYPE;
378  goto exit_label;
379  }
380  err = RES_OK;
381 exit_label:
382  if(pFile)
383  fclose(pFile);
384  return err;
385 }
386 
#define ERROR_FOPEN
Ошибка открытия файла. Файл с заданным именем не может быть открыт для записи и (или) чтения.
Definition: dspl.h:583
#define ERROR_PTR
Ошибка указателя. Данная ошибка означает, что один из обязательных указателей (память под который дол...
Definition: dspl.h:610
#define ERROR_FNAME
Ошибка в имени файла. Необходимо задать корректное имя файла.
Definition: dspl.h:582
#define ERROR_FREAD_SIZE
Ошибка чтения блока данных из бинарного файла. Ошибка возникает, если при чтении блока данных из файл...
Definition: dspl.h:584
double complex_t[2]
Описание комплексного типа данных.
Definition: dspl.h:86
#define RES_OK
Функция завершилась корректно. Ошибки отсутствуют.
Definition: dspl.h:558
int readbin(char *fn, void **x, int *pn, int *pm, int *dtype)
Считать данные из бинарного файла
Definition: readbin.c:318
#define ERROR_DAT_TYPE
Неверный тип данных. Данная ошибка возникает при сохранении данных в бинарный файл,...
Definition: dspl.h:566