FileSource.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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(){
  53. string str;
  54. int i;
  55. float32_t *b=this->getWriteBuffer();
  56. if (input.eof())
  57. {
  58. for(i=0;i<outputSize;i++)
  59. {
  60. b[i] = 0;
  61. }
  62. }
  63. else
  64. {
  65. for(i=0;i<outputSize;i++)
  66. {
  67. if (!getline(input, str))
  68. {
  69. b[i] = 0;
  70. break;
  71. }
  72. b[i] = (float)atof(str.c_str());
  73. }
  74. for(;i<outputSize;i++)
  75. {
  76. b[i] = 0;
  77. }
  78. }
  79. return(0);
  80. };
  81. ifstream input;
  82. };
  83. #endif