@@ -506,6 +506,73 @@ jerry_port_print_char (char c) /**< the character to print */
506506 printf ("%c" , c );
507507} /* jerry_port_print_char */
508508
509+ /**
510+ * Determines the size of the given file.
511+ * @return size of the file
512+ */
513+ static size_t
514+ jerry_port_get_file_size (FILE * file_p ) /**< opened file */
515+ {
516+ fseek (file_p , 0 , SEEK_END );
517+ long size = ftell (file_p );
518+ fseek (file_p , 0 , SEEK_SET );
519+
520+ return (size_t ) size ;
521+ } /* jerry_port_get_file_size */
522+
523+ /**
524+ * Opens file with the given path and reads its source.
525+ * @return the source of the file
526+ */
527+ uint8_t *
528+ jerry_port_read_source (const char * file_name_p , /**< file name */
529+ size_t * out_size_p ) /**< [out] read bytes */
530+ {
531+ FILE * file_p = fopen (file_name_p , "rb" );
532+
533+ if (file_p == NULL )
534+ {
535+ jerry_port_log (JERRY_LOG_LEVEL_ERROR , "Error: failed to open file: %s\n" , file_name_p );
536+ return NULL ;
537+ }
538+
539+ size_t file_size = jerry_port_get_file_size (file_p );
540+ uint8_t * buffer_p = (uint8_t * ) malloc (file_size );
541+
542+ if (buffer_p == NULL )
543+ {
544+ fclose (file_p );
545+
546+ jerry_port_log (JERRY_LOG_LEVEL_ERROR , "Error: failed to allocate memory for module" );
547+ return NULL ;
548+ }
549+
550+ size_t bytes_read = fread (buffer_p , 1u , file_size , file_p );
551+
552+ if (!bytes_read )
553+ {
554+ fclose (file_p );
555+ free (buffer_p );
556+
557+ jerry_port_log (JERRY_LOG_LEVEL_ERROR , "Error: failed to read file: %s\n" , file_name_p );
558+ return NULL ;
559+ }
560+
561+ fclose (file_p );
562+ * out_size_p = bytes_read ;
563+
564+ return buffer_p ;
565+ } /* jerry_port_read_source */
566+
567+ /**
568+ * Release the previously opened file's content.
569+ */
570+ void
571+ jerry_port_release_source (uint8_t * buffer_p ) /**< buffer to free */
572+ {
573+ free (buffer_p );
574+ } /* jerry_port_release_source */
575+
509576/**
510577* Main program.
511578*
0 commit comments