File: ujguess.c

package info (click to toggle)
libunicode-japanese-perl 0.50-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,376 kB
  • sloc: ansic: 30,821; perl: 5,635; erlang: 224; makefile: 191
file content (160 lines) | stat: -rw-r--r-- 3,744 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* ----------------------------------------------------------------------------
 * ujguess.c
 * ----------------------------------------------------------------------------
 * Mastering programmed by YAMASHINA Hio
 *
 * Copyright 2008 YAMASHINA Hio
 * ----------------------------------------------------------------------------
 * $Id$
 * ------------------------------------------------------------------------- */

#include "unijp.h"

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define UJGUESS_VERSION "0.01"

int main(int argc, const char* argv[])
{
  const char* files[10];
  int end_of_opts;
  int nr_files;
  int no_filename;
  int show_filename;
  int i;

  nr_files      = 0;
  end_of_opts   = 0;
  no_filename   = 0;
  show_filename = 0;
  for( i=1; i<argc; ++i )
  {
    if( argv[i][0]=='-' && argv[i][1]!='\0' && !end_of_opts )
    {
      if( strcmp(argv[i], "--no-filename")==0 )
      {
        no_filename   = 1;
        show_filename = 0;
      }else if( strcmp(argv[i], "--show-filename")==0 )
      {
        no_filename   = 0;
        show_filename = 1;
      }else if( strcmp(argv[i], "-h")==0 || strcmp(argv[i], "--help")==0 )
      {
        printf("usage: ujguess [options..] [files..]\n");
        printf("options:\n");
        printf("-h, --help     show this usage\n");
        printf("-V, --version  show version information\n");
        return 0;
      }else if( strcmp(argv[i], "-V")==0 || strcmp(argv[i], "--version")==0 )
      {
        printf("version %s\n", UJGUESS_VERSION);
        printf("libunijp version %s\n", UNIJP_VERSION_STRING);
        return 0;
      }else if( strcmp(argv[i], "--")==0 )
      {
        end_of_opts = 1;
      }else
      {
        fprintf(stderr, "invalid option: %s\n", argv[i]);
        return 1;
      }
    }else
    {
      if( nr_files==10 )
      {
        fprintf(stderr, "too many files\n");
        return 1;
      }
      files[nr_files++] = argv[i];
    }
  }
  if( !no_filename && !show_filename )
  {
    if( nr_files >= 2 )
    {
      show_filename = 1;
    }else
    {
      no_filename   = 1;
    }
  }
  if( nr_files==0 )
  {
    files[nr_files++] = "-";
  }


  {
    char* buf;
    size_t buf_size;
    size_t buf_len;

    buf_len = 0;
    buf_size = 1024;
    buf = malloc(buf_size);
    if( buf==NULL )
    {
      fprintf(stderr, "malloc: %s\n", strerror(errno));
      return 1;
    }

    for( i=0; i<nr_files; ++i )
    {
      FILE* fp;
      uj_charcode_t code;
      int use_stdin = strcmp(files[i], "-")==0;
      if( use_stdin )
      {
        fp = stdin;
      }else
      {
        fp = fopen(files[i], "r");
        if( fp==NULL )
        {
          fprintf(stderr, "fopen: %s: %s\n", files[i], strerror(errno));
          return 1;
        }
      }

      buf_len = 0;
      while( fgets(buf+buf_len, 1023, fp)!=NULL )
      {
        char* new_buf;
        size_t rlen;
        rlen = strlen(buf+buf_len);
        buf_len += rlen;
        buf_size += 1024;
        new_buf = realloc(buf, buf_size);
        if( new_buf==0 )
        {
          fprintf(stderr, "realloc: %s: %s\n", files[i], strerror(errno));
          return 1;
        }
        buf = new_buf;
      }
      if( ferror(fp) )
      {
        fprintf(stderr, "fread: %s: %s\n", files[i], strerror(errno));
        return 1;
      }
      fclose(fp);

      code = uj_getcode((uj_uint8*)buf, buf_len);
      if( !no_filename )
      {
        printf("%s:", files[i]);
      }
      printf("%s\n", uj_charcode_str(code));
    }
    free(buf);
  }
  return 0;
}

/* ----------------------------------------------------------------------------
 * End of File.
 * ------------------------------------------------------------------------- */