ByteStream.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (C) 2007-2018 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. *
  14. * You should have received a copy of the GNU Lesser General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*******************************************************************
  18. *
  19. * @author Daniel.Peintner.EXT@siemens.com
  20. * @version 2017-03-02
  21. * @contact Richard.Kuntschke@siemens.com
  22. *
  23. * <p>Code generated by EXIdizer</p>
  24. * <p>Schema: V2G_CI_MsgDef.xsd</p>
  25. *
  26. *
  27. ********************************************************************/
  28. /* Avoid VS warning, put before your included header files */
  29. /* warning C4996: ‘fopen’: This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. */
  30. #define _CRT_SECURE_NO_DEPRECATE
  31. #include <stdio.h>
  32. #include "EXITypes.h"
  33. #include "ErrorCodes.h"
  34. #ifndef BYTE_STREAM_C
  35. #define BYTE_STREAM_C
  36. int readBytesFromFile(const char * filename, uint8_t* data, size_t size, size_t* pos) {
  37. FILE* f;
  38. int character;
  39. int errn = 0;
  40. f = fopen(filename, "rb");
  41. if (f == NULL) {
  42. errn = EXI_ERROR_INPUT_FILE_HANDLE;
  43. } else {
  44. /* read bytes */
  45. while (errn == 0 && (character = getc(f)) != EOF) {
  46. if (*pos >= size) {
  47. errn = EXI_ERROR_OUT_OF_BYTE_BUFFER;
  48. } else {
  49. data[(*pos)++] = (uint8_t) character;
  50. }
  51. }
  52. fclose(f);
  53. }
  54. return errn;
  55. }
  56. int writeBytesToFile(uint8_t* data, size_t len, const char * filename) {
  57. size_t rlen;
  58. FILE* f = fopen(filename, "wb+");
  59. if (f == NULL) {
  60. return -1;
  61. } else {
  62. rlen = fwrite(data, sizeof(uint8_t), len, f);
  63. fflush(f);
  64. fclose(f);
  65. if(rlen == len) {
  66. return 0;
  67. } else {
  68. return EXI_ERROR_OUTPUT_FILE;
  69. }
  70. }
  71. }
  72. #endif /* BYTE_STREAM_C */