FileSource.h 2.3 KB

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