FileSource.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 prepareForRunning() override
  44. {
  45. if (this->willOverflow()
  46. )
  47. {
  48. return(CG_SKIP_EXECUTION_ID_CODE); // Skip execution
  49. }
  50. return(0);
  51. };
  52. int run() override
  53. {
  54. string str;
  55. int i;
  56. float32_t *b=this->getWriteBuffer();
  57. if (input.eof())
  58. {
  59. for(i=0;i<outputSize;i++)
  60. {
  61. b[i] = 0;
  62. }
  63. }
  64. else
  65. {
  66. for(i=0;i<outputSize;i++)
  67. {
  68. if (!getline(input, str))
  69. {
  70. b[i] = 0;
  71. break;
  72. }
  73. b[i] = (float)atof(str.c_str());
  74. }
  75. for(;i<outputSize;i++)
  76. {
  77. b[i] = 0;
  78. }
  79. }
  80. return(0);
  81. };
  82. ifstream input;
  83. };
  84. #endif