ByteStream.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (C) 2007-2012 Siemens AG
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published
  6. * by the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. * GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*******************************************************************
  19. *
  20. * @author Daniel.Peintner.EXT@siemens.com
  21. * @version 2012-01-31
  22. * @contact Joerg.Heuer@siemens.com
  23. *
  24. * <p>Code generated by EXIdizer</p>
  25. ********************************************************************/
  26. #define _CRT_SECURE_NO_DEPRECATE 1
  27. #include "EXITypes.h"
  28. #ifndef BYTE_STREAM_C
  29. #define BYTE_STREAM_C
  30. int readBytesFromFile(const char * filename, uint8_t* data, uint32_t size, uint32_t pos) {
  31. FILE* f;
  32. int character;
  33. f = fopen(filename, "rb");
  34. if (f == NULL) {
  35. return EXI_ERROR_INPUT_FILE_HANDLE;
  36. } else {
  37. /* read bytes */
  38. while ((character = getc(f)) != EOF) {
  39. if (pos >= size) {
  40. return EXI_ERROR_OUT_OF_BYTE_BUFFER;
  41. }
  42. data[pos++] = (uint8_t) character;
  43. }
  44. fclose(f);
  45. }
  46. return pos;
  47. }
  48. int writeBytesToFile(uint8_t* data, uint32_t len, const char * filename) {
  49. uint16_t rlen;
  50. FILE* f = fopen(filename, "wb+");
  51. if (f == NULL) {
  52. return -1;
  53. } else {
  54. rlen = fwrite(data, sizeof(uint8_t), len, f);
  55. fflush(f);
  56. fclose(f);
  57. if(rlen == len) {
  58. return 0;
  59. } else {
  60. return EXI_ERROR_OUTPUT_FILE;
  61. }
  62. }
  63. }
  64. #endif