FileSource.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: FileSource.h
  4. * Description: Node for creating File sources
  5. *
  6. * $Date: 30 July 2021
  7. * $Revision: V1.10.0
  8. *
  9. * Target Processor: Cortex-M and Cortex-A cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
  13. *
  14. * SPDX-License-Identifier: Apache-2.0
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the License); you may
  17. * not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  24. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. */
  28. #ifndef _FILESOURCE_H_
  29. #define _FILESOURCE_H_
  30. template<typename OUT,int outputSize> class FileSource;
  31. /*
  32. Real a list of floats from a file and pad with zeros indefinitely when end of
  33. file is reached.
  34. */
  35. template<int outputSize>
  36. class FileSource<float32_t,outputSize>: public GenericSource<float32_t,outputSize>
  37. {
  38. public:
  39. FileSource(FIFOBase<float32_t> &dst,std::string name):GenericSource<float32_t,outputSize>(dst),
  40. input(name)
  41. {
  42. };
  43. int run(){
  44. string str;
  45. int i;
  46. float32_t *b=this->getWriteBuffer();
  47. if (input.eof())
  48. {
  49. for(i=0;i<outputSize;i++)
  50. {
  51. b[i] = 0;
  52. }
  53. }
  54. else
  55. {
  56. for(i=0;i<outputSize;i++)
  57. {
  58. if (!getline(input, str))
  59. {
  60. b[i] = 0;
  61. break;
  62. }
  63. b[i] = (float)atof(str.c_str());
  64. }
  65. for(;i<outputSize;i++)
  66. {
  67. b[i] = 0;
  68. }
  69. }
  70. return(0);
  71. };
  72. ifstream input;
  73. };
  74. #endif