Files
jerryscript/jerry-port/common/jerry-port-io.c
T
Yonggang Luo dfa9afbf6e Introduce JERRY_ZSTR_ARG macro to avoid jerryx_print_string, jerryx_print_byte, jerry_port_print_byte, strlen (#4982)
Replace usage of jerryx_print_byte, jerryx_print_string with jerryx_print_buffer.

As we now have JERRY_ZSTR_ARG, so we can take advantage of it

With this, the jerry_port_print_byte port api won't need any more
this reduced the port api surface

JerryScript-DCO-1.0-Signed-off-by: Yonggang Luo luoyonggang@gmail.com
2024-11-25 17:28:55 +01:00

93 lines
2.2 KiB
C

/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jerryscript-port.h"
/**
* Default implementation of jerry_port_log. Prints log messages to stderr.
*/
void JERRY_ATTR_WEAK
jerry_port_log (const char *message_p) /**< message */
{
fputs (message_p, stderr);
} /* jerry_port_log */
void JERRY_ATTR_WEAK
jerry_port_print_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size)
{
fwrite (buffer_p, 1, buffer_size, stdout);
} /* jerry_port_print_buffer */
/**
* Read a line from standard input as a zero-terminated string.
*
* @param out_size_p: length of the string
*
* @return pointer to the buffer storing the string,
* or NULL if end of input
*/
jerry_char_t *JERRY_ATTR_WEAK
jerry_port_line_read (jerry_size_t *out_size_p)
{
char *line_p = NULL;
size_t allocated = 0;
size_t bytes = 0;
while (true)
{
allocated += 64;
line_p = realloc (line_p, allocated);
if (line_p == NULL)
{
jerry_port_fatal (JERRY_FATAL_OUT_OF_MEMORY);
}
while (bytes < allocated - 1)
{
char ch = (char) fgetc (stdin);
if (feof (stdin))
{
free (line_p);
return NULL;
}
line_p[bytes++] = ch;
if (ch == '\n')
{
*out_size_p = (jerry_size_t) bytes;
line_p[bytes++] = '\0';
return (jerry_char_t *) line_p;
}
}
}
} /* jerry_port_line_read */
/**
* Free a line buffer allocated by jerry_port_line_read
*
* @param buffer_p: buffer that has been allocated by jerry_port_line_read
*/
void JERRY_ATTR_WEAK
jerry_port_line_free (jerry_char_t *buffer_p)
{
free (buffer_p);
} /* jerry_port_line_free */