/***********************************************************************
*                    SEGGER Microcontroller GmbH                       *
*                        The Embedded Experts                          *
************************************************************************
*                                                                      *
*                  (c) SEGGER Microcontroller GmbH                     *
*                        All rights reserved                           *
*                          www.segger.com                              *
*                                                                      *
************************************************************************
*                                                                      *
************************************************************************
*                                                                      *
*                                                                      *
*  Licensing terms                                                     *
*                                                                      *
* This software may be distributed to your customers free of charge.   *
* This grant of redistribution does not entitle YOU or enduser to      *
* receive from SEGGER hard-copy documentation, technical support,      *
* phone assistance, or enhancements or updates to the Software unless  *
* a specific agreement clearly states otherwise.                       *
*                                                                      *
* THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDER "AS IS" AND ANY        *
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE    *
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR   *
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER BE        *
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,     *
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,             *
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR   *
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY  *
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT         *
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE    *
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH     *
* DAMAGE.                                                              *
*                                                                      *
************************************************************************

-------------------------- END-OF-HEADER -----------------------------

File    : Template_FileOperations.JLinkScript
Purpose : This script shows how to use the SYS_FILE_XXXX() functions to use advanced file I/O.

Notes:
  (1) The script is supposed to be used with the Device Provisioner (DevPro.exe).
      DevPro.exe -operation TestFileRead -if SWD -speed 4000 -scriptfile C:\Temp\Template_FileOperations.JLinkScript

*/

/*********************************************************************
*
*       Local functions
*
**********************************************************************
*/

/*********************************************************************
*
*       strlen()
*
*  Function description
*    Get the string length.
*
*  Return value
*    Length of stirng
*/
int strlen(const char* s) {
  int i;

  i = 0;
  while (*s != 0) {
    i += 1;
    s += 1;
  }
  return i;
}

/*********************************************************************
*
*       Global functions
*
**********************************************************************
*/

/*********************************************************************
*
*       TestFileRead()
*
*  Function description
*    Test SYS_FILE_Read(), SYS_FILE_GetSize() functionality.
*
*  Return value
*    >= 0  OK
*     < 0  Error
*/
int TestFileRead(void) {
  SYS_FILE_HANDLE hFile;
  U32 FileSize;
  U32 MaxLen;
  int r;
  U8 acBuf[512];
  const char* sContent;

  hFile = SYS_FILE_Open("C:\\Work\\Data\\TestData.txt", SYS_FILE_FLAG_READ);
  if (hFile < 0) {
    JLINK_SYS_Report1("Invalid file to read from: ", hFile);
    return -1;
  }
  FileSize = SYS_FILE_GetSize(hFile);
  JLINK_SYS_Report1("File size (bytes): ", FileSize);
  MaxLen = sizeof(acBuf) - 1;
  r = SYS_FILE_Read(hFile, &acBuf[0], MaxLen);
  if (r < 0) {
    JLINK_SYS_Report1("Failed to readback: err = ", r);
  } else {
    acBuf[r] = 0;
    JLINK_SYS_Report1("Readback file: size = ", r);
    JLINK_SYS_Report(&acBuf[0]);
  }
  SYS_FILE_Close(hFile);
  return 0;
}

/*********************************************************************
*
*       TestFileReadModifyWrite()
*
*  Function description
*    Test SYS_FILE_Read(), SYS_FILE_SetPos() and SYS_FILE_Write() functionality.
*
*  Return value
*    >= 0  OK
*     < 0  Error
*/
int TestFileReadModifyWrite(void) {
  SYS_FILE_HANDLE hFile;
  U32 Len;
  int r;
  U8 acBuf[16];
  const char* sContent;

  hFile = SYS_FILE_Open("C:\\Work\\Data\\Test.txt", SYS_FILE_FLAG_WRITE | SYS_FILE_FLAG_READ | SYS_FILE_FLAG_CREATE | SYS_FILE_FLAG_TRUNC);
  if (hFile < 0) {
    JLINK_SYS_Report1("Invalid file to write to: ", hFile);
    return -1;
  }
  //
  // Write data
  //
  sContent = "Hello,  world!";
  Len = strlen(sContent);
  r = SYS_FILE_Write(hFile, sContent, Len);
  if (r != Len) {
    JLINK_SYS_Report1("Failed to write: err = ", r);
  }
  //
  // Readback data
  //
  r = SYS_FILE_SetPos(hFile, 0, SYS_FILE_ORIGIN_BEGIN);
  if (r < 0) {
    JLINK_SYS_Report1("Failed to set file position: err = ", r);
  }
  r = SYS_FILE_Read(hFile, &acBuf[0], Len);
  if (r != Len) {
    JLINK_SYS_Report1("Failed to readback: err = ", r);
  } else {
    acBuf[Len] = 0;
    JLINK_SYS_Report("Readback file content:");
    JLINK_SYS_Report(&acBuf[0]);
  }
  //
  // Move cursor from the end
  //
  r = SYS_FILE_SetPos(hFile, -7, SYS_FILE_ORIGIN_END);
  if (r < 0) {
    JLINK_SYS_Report1("Failed to set file position: err = ", r);
  }
  //
  // Replace data
  //
  sContent = "SEGGER.";
  Len = strlen(sContent);
  r = SYS_FILE_Write(hFile, sContent, Len);
  if (r != Len) {
    JLINK_SYS_Report1("Failed to write: err = ", r);
  }
  //
  // Readback modified data
  //
  r = SYS_FILE_SetPos(hFile, 0, 0);
  if (r < 0) {
    JLINK_SYS_Report1("Failed to set file position: err = ", r);
  }
  Len = 14;
  r = SYS_FILE_Read(hFile, &acBuf[0], Len);
  if (r != Len) {
    JLINK_SYS_Report1("Failed to readback: err = ", r);
  } else {
    acBuf[Len] = 0;
    JLINK_SYS_Report("Readback modified file:");
    JLINK_SYS_Report(&acBuf[0]);
  }
  SYS_FILE_Close(hFile);
  return 0;
}