summaryrefslogtreecommitdiffstats
path: root/examples/rdfguess.c
blob: b636c0b8ff96bf62ebb31e8479742998d9289fac (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <raptor2.h>


/* rdfguess.c: guess parser name from filename and its content */

#define READ_BUFFER_SIZE 256

static int
rdfguess_guess_name(raptor_world* world, const char* filename)
{
  char *buffer[READ_BUFFER_SIZE];
  raptor_iostream* iostr = NULL;
  const char* name;
  const unsigned char* identifier;
  const char* label;
  size_t read_len;
  size_t count;
  
  if(!strcmp(filename, "-")) {
    iostr = raptor_new_iostream_from_file_handle(world, stdin);
    identifier = NULL;
    label = "<stdin>";
  } else {
    if(access(filename, R_OK)) {
      fprintf(stderr, "rdfguess: file %s not found\n", filename);
      return 1;
    }
    
    iostr = raptor_new_iostream_from_filename(world, filename);
    identifier = (const unsigned char *)filename;
    label = filename;
  }
  
  if(!iostr) {
    fprintf(stderr, "rdfguess: Could not create iostream for %s\n", label);
    goto tidy;
  }

  read_len = READ_BUFFER_SIZE;
  count = raptor_iostream_read_bytes(buffer, 1, read_len, iostr);
  if(count < 1) {
    fprintf(stderr, "rdfguess: Failed to read any data from %s\n",
            label);
    goto tidy;
  }
  
  name = raptor_world_guess_parser_name(world,
                                        /* uri*/ NULL,
                                        /* mime_type */ NULL,
                                        (const unsigned char*)buffer,
                                        read_len, 
                                        identifier);
                                        
  if(name)
    fprintf(stdout, "rdfguess: %s guessed to be %s\n", label, name);
  else
    fprintf(stdout, "rdfguess: failed to guess parser for %s\n", label);
  
  tidy:
  raptor_free_iostream(iostr);
    
  return 0;
}
      

int
main(int argc, char *argv[])
{
  raptor_world *world = NULL;
  const char *filename;
  int rc = 0;
  
  world = raptor_new_world();

  if(argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
    fprintf(stderr, "USAGE rdfguess [FILENAMES...]\n");
    rc = 1;
    goto tidy;
  }

  if(argc == 1) {
    rc = rdfguess_guess_name(world, "-");
  } else {
    int i;

    for(i = 1; (filename = (const char*)argv[i]); i++) {
      rc = rdfguess_guess_name(world, filename);
      if(rc)
        break;
    }
  }

  tidy:
  raptor_free_world(world);

  return rc;
}