Skip to content
Permalink
Browse files

Add -text-to-raw option for command line program format conversion.

  • Loading branch information...
RobFisher committed Mar 16, 2018
1 parent b212957 commit 6384f9da31282b8bc81eb00cc26bd20def4fb07f
Showing with 69 additions and 5 deletions.
  1. +4 −3 common/core_main.cc
  2. +1 −1 common/core_main.h
  3. +64 −1 gtk/shell_main.cc
@@ -3142,12 +3142,12 @@ static void paste_programs(const char *buf) {
}
}

void core_paste(const char *buf) {
void core_paste(const char *buf, bool programs_only) {
if (mode_interruptible != NULL)
stop_interruptible();
set_running(false);

if (flags.f.prgm_mode) {
if (flags.f.prgm_mode || programs_only) {
paste_programs(buf);
} else if (core_alpha_menu()) {
char hpbuf[48];
@@ -3434,7 +3434,8 @@ void core_paste(const char *buf) {
flags.f.message = 0;
flags.f.two_line_message = 0;
}
redisplay();
if (!programs_only)
redisplay();
}

void set_alpha_entry(bool state) {
@@ -278,7 +278,7 @@ char *core_copy();
* a plain string.
* Used by the shell to implement the Paste command.
*/
void core_paste(const char *s);
void core_paste(const char *buf, bool programs_only);

/* core_settings
*
@@ -180,6 +180,8 @@ static void txt_newliner();
static void gif_seeker(int4 pos);
static void gif_writer(const char *text, int length);

static long import_file_size();
static int convert_text_to_raw(char* input_name, char* output_name);

#ifdef BCD_MATH
#define TITLE "Free42 Decimal"
@@ -235,6 +237,9 @@ static int use_compactmenu = 0;
static bool decimal_point;

int main(int argc, char *argv[]) {
if (argc > 3 && strcmp(argv[1], "-text-to-raw") == 0)
exit(convert_text_to_raw(argv[2], argv[3]));

gtk_init(&argc, &argv);

// Capture state of decimal_point, which may have been changed by
@@ -1418,7 +1423,7 @@ static void copyCB() {

static void paste2(GtkClipboard *clip, const gchar *text, gpointer cd) {
if (text != NULL) {
core_paste(text);
core_paste(text, false);
redisplay();
// GTK will free the text once the callback returns.
}
@@ -2346,6 +2351,20 @@ int shell_read(char *buf, int4 buflen) {
return nread;
}

static long import_file_size() {
long size = -1;
long offset = ftell(import_file);
if(offset == -1) {
return -1;
}
if (fseek(import_file, 0, SEEK_END) != 0)
return -1;
size = ftell(import_file);
if(fseek(import_file, offset, SEEK_SET) != 0)
return -1;
return size;
}

void shell_get_time_date(uint4 *time, uint4 *date, int *weekday) {
struct timeval tv;
gettimeofday(&tv, NULL);
@@ -2358,3 +2377,47 @@ void shell_get_time_date(uint4 *time, uint4 *date, int *weekday) {
if (weekday != NULL)
*weekday = tms.tm_wday;
}

static int convert_text_to_raw(char* input_name, char* output_name) {
size_t size;
char* buffer;
import_file = fopen(input_name, "r");
if (import_file == NULL) {
int err = errno;
fprintf(stderr, "Error opening input file \"%s\": %s (%d)\n",
input_name, strerror(err), err);
return 1;
}
size = (size_t)import_file_size();
if (size <= 0) {
fprintf(stderr, "Error getting size of input file.\n");
return 1;
}
buffer = (char*)malloc(size);
if(buffer == NULL) {
fprintf(stderr, "Out of memory.\n");
fclose(import_file);
return 1;
}
if(shell_read(buffer, (int4)size) != (int)size) {
fprintf(stderr, "Error reading file.\n");
fclose(import_file);
return 1;
}
fclose(import_file);
init_shell_state(-1);
core_paste(buffer, true);
export_file = fopen(output_name, "w");
if (export_file == NULL) {
int err = errno;
fprintf(stderr, "Could not open \"%s\" for writing:\n%s (%d)",
output_name, strerror(err), err);
free(buffer);
return 1;
}
int program_index = 0;
core_export_programs(1, &program_index);
fclose(export_file);
free(buffer);
return 0;
}

0 comments on commit 6384f9d

Please sign in to comment.
You can’t perform that action at this time.