Read external CSV file (Sample)

From SEGGER Wiki
Jump to: navigation, search
GUI_LANG_ReadCSVEx.c
GUI LANG ReadCSV.png
File(s) required
  • GUI_LANG_ReadCSVEx.c
  • SampleCSV.csv
Runs in simulation Yes
Runs on target Yes
Download GUI_LANG_ReadCSVEx.zip

This sample shows how to read different languages from a .csv file that is located in external memory. The sample supports both the usage of Windows file system and emFile.

Code

/*********************************************************************
*                    SEGGER Microcontroller GmbH                     *
*        Solutions for real time microcontroller applications        *
**********************************************************************
*                                                                    *
*        (c) 1996 - 2020  SEGGER Microcontroller GmbH                *
*                                                                    *
*        Internet: www.segger.com    Support:  support@segger.com    *
*                                                                    *
**********************************************************************

** emWin V6.10 - Graphical user interface for embedded applications **
emWin is protected by international copyright laws.   Knowledge of the
source code may not be used to write a similar product.  This file may
only  be used  in accordance  with  a license  and should  not be  re-
distributed in any way. We appreciate your understanding and fairness.
----------------------------------------------------------------------
File        : GUI_LANG_ReadCSVEx.c
Purpose     : Sample that demonstrates how to read different languages
              from a CSV file that is located somewhere externally.
              This sample can be used from the Windows simulation and
              from a target using emFile as file system.
              For reference on the CSV format, see the manual chapter
              "32.2.4 Rules for CSV files".
Requirements: WindowManager - ( )
              MemoryDevices - ( )
              AntiAliasing  - ( )
              VNC-Server    - ( )
              PNG-Library   - ( )
              TrueTypeFonts - ( )
---------------------------END-OF-HEADER------------------------------
*/

#include "DIALOG.h"
#ifdef WIN32
  #include <Windows.h>
#else
  #include "FS.h"
#endif
#include <stdio.h>

/*********************************************************************
*
*       Defines
*
**********************************************************************
*/
//
// Set file path to sample CSV in the same directory as this sample.
// The sample CSV file contains three languages: German, English and French.
//
#ifdef WIN32
  #define FILE_PATH "C:\\Work\\emWin\\Intern\\TutorialV6\\LangSupport\\SampleCSV.csv"
#else
  #define FILE_PATH "SampleCSV.csv"
#endif

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/
/*********************************************************************
*
*       _GetData
*/
static int _GetData(void * pVoid, const U8 ** ppData, unsigned NumBytes, U32 Off) {
#ifdef WIN32
  HANDLE  * phFile;
  DWORD     NumBytesRead;
#else
  FS_FILE * pFile;
  int       NumBytesRead;
#endif
  U8      * pData;

  pData  = (U8 *)*ppData;
#ifdef WIN32
  phFile = (HANDLE *)pVoid;
  //
  // Set file pointer to the required position
  //
  SetFilePointer(*phFile, Off, 0, FILE_BEGIN);
  //
  // Read data into buffer
  //
  ReadFile(*phFile, pData, NumBytes, &NumBytesRead, 0);
#else
  pFile = (FS_FILE *)pVoid;
  //
  // Set file pointer to the required position
  //
  FS_SetFilePos(pFile, Off, FS_FILE_BEGIN);
  //
  // Read data into buffer
  //
  NumBytesRead = FS_FRead(pData, 1, NumBytes, pFile);
  //
  // Set data pointer to the beginning of the buffer
  //
  //*ppData = pData;
#endif
  return NumBytesRead;
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
#ifdef WIN32
        HANDLE hFile;
#else
        FS_FILE * pFile;
        char      acVolumeName[10];
#endif
        int       NumLanguages;
        char      acBuffer[64];
        int       i, j;
  const char    * pText;

  //
  // Init emWin.
  //
  GUI_Init();
  //
  // Enable UTF-8 support.
  //
  GUI_UC_SetEncodeUTF8();
#ifdef WIN32
  //
  // Create file handle.
  //
  hFile = CreateFile(FILE_PATH, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  //
  // Read the CSV file and save the number of languages.
  //
  NumLanguages = GUI_LANG_LoadCSVEx(_GetData, &hFile);
#else
  //
  // Start emFile
  //
  FS_Init();
  //
  // Enable long file name support
  //
  FS_FAT_SupportLFN();
  //
  // Mount volume
  //
  FS_GetVolumeName(0, acVolumeName, sizeof(acVolumeName));
  if (FS_Mount(acVolumeName) > 0) {
    //
    // Open file
    //
    pFile = FS_FOpen(FILE_PATH, "rb");
  }
  //
  // Read the CSV file and save the number of languages.
  //
  NumLanguages = GUI_LANG_LoadCSVEx(_GetData, pFile);
#endif
  //
  // Print out some data.
  //
  sprintf(acBuffer, "Number of available languages: %i", NumLanguages);
  GUI_DispStringAt(acBuffer, 10, 10);
  //
  for(i = 0; i < NumLanguages; i++) {
    j = 0;
    //
    // Set index of language.
    //
    GUI_LANG_SetLang(i);
    //
    // Get pointer to text from current set language.
    //
    while(pText = GUI_LANG_GetText(j)) {
      //
      // Print the text.
      //
      GUI_DispStringAt(pText, 10 + (i * 60), 35 + (j * 12));
      j++;
    }
  }
  //
  // Free all allocated resources.
  //
  GUI_LANG_Clear();
#ifdef WIN32
  //
  // Close file
  //
  CloseHandle(hFile);
#else
  //
  // Close file
  //
  FS_FClose(pFile);
#endif

  while (1) {
    GUI_Delay(100);
  }
}

/*************************** End of file ****************************/