Example #1
0
ScopeStmt* read_scopestmt () {
    AstTag tag = read_int();
    switch(tag) {
    case VAR_STMT: {
        char* name = read_string();
        Exp* exp = read_exp();
        return make_ScopeVar(name, exp);
    }
    case FN_STMT: {
        char* name = read_string();
        int nargs = read_int();
        char** args = read_strings(nargs);
        ScopeStmt* body = read_scopestmt();
        return make_ScopeFn(name, nargs, args, body);
    }
    case SEQ_STMT: {
        ScopeStmt* a = read_scopestmt();
        ScopeStmt* b = read_scopestmt();
        return make_ScopeSeq(a, b);
    }
    case EXP_STMT: {
        Exp* e = read_exp();
        return make_ScopeExp(e);
    }
    default: {
        printf("Scope statement with unrecognized tag: %d\n", tag);
        exit(-1);
    }
    }
    return 0;
}
Example #2
0
int					/* O - Exit code */
main(int  argc,				/* I - Number of command-line args */
     char *argv[])			/* I - Command-line arguments */
{
  FILE	*strings,			/* .strings file */
	*po;				/* .po file */
  char	iconv[1024],			/* iconv command */
	buffer[8192],			/* Line buffer */
	*id,				/* ID string */
	*str;				/* Translation string */
  int	count;				/* Number of messages converted */


  if (argc != 3)
  {
    puts("Usage: strings2po filename.strings filename.po");
    return (1);
  }

 /*
  * Cheat by using iconv to convert the .strings file from UTF-16 to UTF-8
  * which is what we need for the .po file (and it makes things a lot
  * simpler...)
  */

  snprintf(iconv, sizeof(iconv), "iconv -f utf-16 -t utf-8 '%s'", argv[1]);
  if ((strings = popen(iconv, "r")) == NULL)
  {
    perror(argv[1]);
    return (1);
  }

  if ((po = fopen(argv[2], "w")) == NULL)
  {
    perror(argv[2]);
    pclose(strings);
    return (1);
  }

  count = 0;

  while (read_strings(strings, buffer, sizeof(buffer), &id, &str))
  {
    count ++;
    write_po(po, "msgid", id);
    write_po(po, "msgstr", str);
  }

  pclose(strings);
  fclose(po);

  printf("%s: %d messages.\n", argv[2], count);

  return (0);
}
Example #3
0
File: Exam.cpp Project: CAHbl4/C
state input_str(void* data, void* params)
{
	cci.dwSize = 5;
	cci.bVisible = TRUE;
	SetConsoleCursorInfo(hConsole, &cci);
	system("cls");
	flush_stream(stdin);
	printf(Rus("Вводите строки.\n"));
	printf(Rus("Для прекращения ввода-<Enter> в начале строки.\n"));
	str = read_strings(stdin, str, &str_count);
	return REDRAW_ALL;
}
int main()
{
    bin_tree bin1;
    FILE *file_ptr;
    
    file_ptr = fopen("input.txt", "r");
    
    bin1 = read_strings(file_ptr);
    print_tree(bin1.root, bin1.max_depth);
    
    fclose(file_ptr);
    return 0;
    
}
Example #5
0
File: Exam.cpp Project: CAHbl4/C
state read_str(void*, void*)
{
	system("cls");
	FILE* file;
	if (file = fopen(STR_FILE_NAME, "r"))
	{
		str_count = 0;
		str = read_strings(file, str, &str_count);
		printf(Rus("Считано строк - %d\n"), str_count);
	}
	else
	{
		printf(Rus("Ошибка при открытии файла!\n"));
	}
	system("pause");
	return REDRAW_ALL;
}
Example #6
0
int read_file(char* s, int size_s)
{
    char *tmp_s;
    FILE* tmp;
    
    tmp_s = realloc(s, size_s+1);
    if (tmp_s == NULL) {
        free(s);
        return exit_force();
    }
    s = tmp_s;
    
    s[size_s] = '\0';
    tmp = fopen(s, "rb");
    if (tmp == NULL)
        return -3;
    read_strings(tmp);
    fclose(tmp);
    free(s);
    return 0;
}
Example #7
0
static int read_option(int fd, struct device_context *dev_ctx)
{
	struct boot_option *opt = malloc(sizeof(*opt));
	twin_pixmap_t *icon;
	int index = -1;

	if (!opt)
		return TWIN_FALSE;

	if (!read_strings(fd, (*opt)))
		return TWIN_FALSE;

	LOG("got option: '%s'\n", opt->name);
	icon = get_icon(opt->icon_file);

	if (icon)
		index = pboot_add_option(dev_ctx->device_idx, opt->name,
					 opt->description, icon, opt);

	return index != -1;
}
Example #8
0
SlotStmt* read_slot () {
    AstTag tag = read_int();
    switch(tag) {
    case VAR_STMT: {
        char* name = read_string();
        Exp* exp = read_exp();
        return make_SlotVar(name, exp);
    }
    case FN_STMT: {
        char* name = read_string();
        int nargs = read_int();
        char** args = read_strings(nargs);
        ScopeStmt* body = read_scopestmt();
        return make_SlotMethod(name, nargs, args, body);
    }
    default: {
        printf("Unrecognized slot with tag: %d\n", tag);
        exit(-1);
    }
    }
}
Example #9
0
int open_file(char* s, long int size_s)
{
    char *tmp_s;
    FILE* tmp;
    
    tmp_s = realloc(s, size_s+1);
    if (tmp_s == NULL) {
        free(s);
        return exit_force();
    }
    s = tmp_s;
    
    s[size_s] = '\0';
    tmp = fopen(s, "rb");
    if (tmp == NULL)
        return -3;
    memcpy(filename, s, sizeof(char)*(size_s+1));
    read_strings(tmp);
    fclose(tmp);
    free(s);
    return 0;
}
Example #10
0
static int read_device(int fd, struct device_context *dev_ctx)
{
	/* name, description, icon_file */
	struct device dev;
	twin_pixmap_t *icon;
	int index = -1;

	if (!read_strings(fd, dev))
		return TWIN_FALSE;

	LOG("got device: '%s'\n", dev.name);

	icon = get_icon(dev.icon_file);

	if (!icon)
		goto out;

	index = dev_ctx->device_idx = pboot_add_device(dev.id, dev.name, icon);

out:
	free_strings(dev);

	return index != -1;
}
Example #11
0
/**
 * Program entrypoint.
 */
int main(int argc, char **argv)
{
    int char_count;
    int string_count;
    int encoded_size;
    unsigned char charmap[256];
    int frequencies[256];
    huffman_node_t *leaf_nodes[256];
    huffman_node_t *code_nodes[256];
    huffman_node_t *root;
    int symbol_count;
    string_list_t *strings;
    FILE *input;
    FILE *table_output;
    FILE *data_output;
    int append_byte = -1;
    int ignore_case = 0;
    const char *input_filename = 0;
    const char *charmap_filename = 0;
    const char *table_output_filename = 0;
    const char *data_output_filename = 0;
    const char *table_label = "";
    const char *node_label_prefix = "";
    const char *string_table_label = "";
    const char *string_label_prefix = "";
    int generate_string_table = 0;
    int verbose = 0;

    /* Process arguments. */
    {
        char *p;
        while ((p = *(++argv))) {
            if (!strncmp("--", p, 2)) {
                const char *opt = &p[2];
                if (!strncmp("character-map=", opt, 14)) {
                    charmap_filename = &opt[14];
                } else if (!strncmp("table-output=", opt, 13)) {
                    table_output_filename = &opt[13];
                } else if (!strncmp("data-output=", opt, 12)) {
                    data_output_filename = &opt[12];
                } else if (!strncmp("table-label=", opt, 12)) {
                    table_label = &opt[12];
                } else if (!strncmp("node-label-prefix=", opt, 18)) {
                    node_label_prefix = &opt[18];
                } else if (!strncmp("string-label-prefix=", opt, 20)) {
                    string_label_prefix = &opt[20];
                    generate_string_table = 1;
                } else if (!strcmp("generate-string-table", opt)) {
                    generate_string_table = 1;
                } else if (!strncmp("string-table-label=", opt, 19)) {
                    string_table_label = &opt[19];
                } else if (!strncmp("append-byte=", opt, 12)) {
                    append_byte = strtol(&opt[12], 0, 0);
                    if ((append_byte < 0) || (append_byte >= 256)) {
                        fprintf(stderr, "huffpuff: --append-byte: value must be in range 0..255\n");
                        return(-1);
                    }
                } else if (!strcmp("ignore-case", opt)) {
                    ignore_case = 1;
                } else if (!strcmp("verbose", opt)) {
                    verbose = 1;
                } else if (!strcmp("help", opt)) {
                    help();
                } else if (!strcmp("usage", opt)) {
                    usage();
                } else if (!strcmp("version", opt)) {
                    version();
                } else {
                    fprintf(stderr, "huffpuff: unrecognized option `%s'\n"
			    "Try `huffpuff --help' or `huffpuff --usage' for more information.\n", p);
                    return(-1);
                }
            } else {
                input_filename = p;
            }
        }
    }

    /* Set default character mapping f(c)=c */
    {
        int i;
        for (i=0; i<256; i++)
            charmap[i] = (unsigned char)i;
    }

    if (charmap_filename) {
        if (verbose)
            fprintf(stdout, "reading character map\n");
        if (!charmap_parse(charmap_filename, charmap)) {
            fprintf(stderr, "error: failed to parse character map `%s'\n",
                    charmap_filename);
            return(-1);
        }
    }

    if (input_filename) {
        input = fopen(input_filename, "rt");
        if (!input) {
            fprintf(stderr, "error: failed to open `%s' for reading\n",
                    input_filename);
            return(-1);
        }
    } else {
        input = stdin;
    }

    /* Read strings to encode. */
    if (verbose)
        fprintf(stdout, "reading strings\n");
    strings = read_strings(input, ignore_case, frequencies, &char_count, &string_count);
    fclose(input);

    /* Create Huffman leaf nodes. */
    if (verbose)
        fprintf(stdout, "creating Huffman leaf nodes\n");
    symbol_count = 0;
    {
        int i;
        if (append_byte != -1)
            frequencies[append_byte] += string_count;
        for (i=0; i<256; i++) {
            if (frequencies[i] > 0) {
                huffman_node_t *node;
                node = huffman_create_node(
                    /*symbol=*/i, /*weight=*/frequencies[i],
                    /*left=*/NULL, /*right=*/NULL);
                leaf_nodes[symbol_count++] = node;
                code_nodes[i] = node;
            } else {
                code_nodes[i] = 0;
            }
        }
    }
    if (verbose)
        fprintf(stdout, "  number of symbols: %d\n", symbol_count);

    /* Build the Huffman tree. */
    if (verbose)
        fprintf(stdout, "Building the Huffman tree\n");
    root = huffman_build_tree(leaf_nodes, symbol_count);

    /* Huffman-encode strings. */
    if (verbose)
        fprintf(stdout, "encoding strings\n");
    encoded_size = encode_strings(strings, code_nodes, append_byte);

    /* Sanity check */
    if (verbose)
        fprintf(stdout, "verifying output integrity\n");
    if (!verify_data_integrity(strings, root)) {
        assert(0);
        /* Cleanup */
        huffman_delete_node(root);
        destroy_string_list(strings);
        return(-1);
    }

    /* Prepare output */
    if (!table_output_filename) {
        table_output_filename = "huffpuff.tab.asm";
    }
    table_output = fopen(table_output_filename, "wt");
    if (!table_output) {
        fprintf(stderr, "error: failed to open `%s' for writing\n",
                table_output_filename);
        /* Cleanup */
        huffman_delete_node(root);
        destroy_string_list(strings);
        return(-1);
    }

    if (!data_output_filename) {
        data_output_filename = "huffpuff.dat.asm";
    }
    data_output = fopen(data_output_filename, "wt");
    if (!data_output) {
        fprintf(stderr, "error: failed to open `%s' for writing\n",
                data_output_filename);
        /* Cleanup */
        huffman_delete_node(root);
        destroy_string_list(strings);
        return(-1);
    }
    fprintf(data_output, "; Huffman-encoded string data automatically generated by huffpuff.\n");

    /* Print the Huffman codes in code length order. */
    if (verbose)
        fprintf(stdout, "writing Huffman decoder table\n");
    fprintf(table_output, "; Huffman decoder table automatically generated by huffpuff.\n");
    if (table_label && strlen(table_label))
        fprintf(table_output, "%s:\n", table_label);
    write_huffman_codes(table_output, root, charmap, node_label_prefix);

    fclose(table_output);

    if (generate_string_table) {
        /* Print string pointer table */
        int i;
        string_list_t *lst;
        if (verbose)
            fprintf(stdout, "writing string pointer table\n");
        if (string_table_label && strlen(string_table_label))
            fprintf(data_output, "%s:\n", string_table_label);
        for (i = 0, lst = strings; lst != 0; lst = lst->next, ++i) {
            fprintf(data_output, ".dw %sString%d\n",
                    string_label_prefix, i);
        }
    }

    /* Write the Huffman-encoded strings. */
    if (verbose)
        fprintf(stdout, "writing encoded string data\n");
    write_huffman_strings(data_output, strings, string_label_prefix);

    fclose(data_output);

    if (verbose)
        fprintf(stdout, "compressed size: %d%%\n", (encoded_size*100) / char_count);

    /* Cleanup */
    huffman_delete_node(root);
    destroy_string_list(strings);

    return 0;
}