Diff

Differences From Artifact [28f9b94a91]:

To Artifact [903b20a63c]:


1
2
3
4
5
6
7
8
9
10
11
..
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
..
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
...
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
161
162
163
164
165
166
167
168
169
...
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
...
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
...
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
...
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
...
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373





374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#! /usr/bin/env tclsh

if {[llength $argv] != 2} {
	puts stderr "Usage: kitdll <hashkey> <startdir>"

	exit 1
}

set hashkey [lindex $argv 0]
set startdir [lindex $argv 1]

................................................................................

	set ret "\"$ret\""

	return $ret
}

# This function must be kept in-sync with the generated C function below
proc kitdll_hash {path} {
	set h 0
	set g 0

	for {set idx 0} {$idx < [string length $path]} {incr idx} {
		binary scan [string index $path $idx] H* char
		set char "0x$char"

................................................................................
# Generate list of files to include in output
set files [recursive_glob $startdir]

# Insert dummy entry cooresponding to C dummy entry
set files [linsert $files 0 "__DUMMY__"]

# Produce C89 compatible header
set cpp_tag "KITDLL_[string toupper $hashkey]"
set code_tag "kitdll_[string tolower $hashkey]"
set hashkey [string tolower $hashkey]

puts "#ifndef $cpp_tag"
puts "#  define $cpp_tag 1"
puts {
#  ifdef HAVE_STDC
#    ifndef HAVE_UNISTD_H
................................................................................
#  ifdef HAVE_UNISTD_H
#    include <unistd.h>
#  endif
#  ifdef HAVE_STRING_H
#    include <string.h>
#  endif

#  ifndef LOADED_KITDLL_COMMON
#    define LOADED_KITDLL_COMMON 1

typedef enum {
	KITDLL_FILETYPE_FILE,
	KITDLL_FILETYPE_DIR
} kitdll_filetype_t;

struct kitdll_data {
	const char *             name;
	unsigned long            index;
	unsigned long            size;
	kitdll_filetype_t        type;
	const unsigned char *    data;
};

static unsigned long kitdll_hash(const unsigned char *path) {
	unsigned long i, h = 0, g = 0;

	for (i = 0; path[i]; i++) {
		h = (h << 4) + path[i];
		g = h & 0xf0000000;
		if (g) {
			h ^= (g >> 24);
................................................................................
		}
		h &= ((~g) & 0xffffffffLU);
	}
        
        return(h);
}

#  endif /* !LOADED_KITDLL_COMMON */}
puts ""

puts "static struct kitdll_data ${code_tag}_data\[\] = {"
puts "\t{"
puts "\t\t.name  = NULL,"
puts "\t\t.index = 0,"
puts "\t\t.type  = 0,"
puts "\t\t.size  = 0,"
puts "\t\t.data  = NULL,"
puts "\t},"
................................................................................
	set shortfile [shorten_file $startdir $file]

	unset -nocomplain finfo type
	file stat $file finfo

	switch -- $finfo(type) {
		"file" {
			set type "KITDLL_FILETYPE_FILE"
			set size $finfo(size)

			set fd [open $file]
			fconfigure $fd -translation binary
			set data [read $fd]
			close $fd

			set data "(unsigned char *) [stringify $data]"
		}
		"directory" {
			set type "KITDLL_FILETYPE_DIR"
			set data "NULL"
			set size 0
		}
	}

	puts "\t{"
	puts "\t\t.name  = \"$shortfile\","
................................................................................
	puts "\t\t.data  = $data,"
	puts "\t},"
}
puts "};"
puts ""

puts "static unsigned long ${code_tag}_lookup_index(const char *path) {"
puts "\tswitch (kitdll_hash((unsigned char *) path)) {"

for {set idx 1} {$idx < [llength $files]} {incr idx} {
	set file [lindex $files $idx]
	set shortfile [shorten_file $startdir $file]
	set hash [kitdll_hash $shortfile]

	lappend indexes_per_hash($hash) [list $shortfile $idx]
}

foreach {hash idx_list} [array get indexes_per_hash] {
	puts "\t\tcase $hash:"

................................................................................
}

puts "\t}"
puts "\treturn(0);"
puts "}"
puts ""

puts "static struct kitdll_data *${code_tag}_getData(const char *path, unsigned long index) {"
puts "\tif (path != NULL) {"
puts "\t\tindex = ${code_tag}_lookup_index(path);"
puts "\t}"
puts ""
puts "\tif (index == 0) {"
puts "\t\treturn(NULL);"
puts "\t}"
................................................................................
puts "\tunsigned long num_children = 0;"
puts ""
puts "\tindex = ${code_tag}_lookup_index(path);"
puts "\tif (index == 0) {"
puts "\t\treturn(0);"
puts "\t}"
puts ""
puts "\tif (${code_tag}_data\[index\].type != KITDLL_FILETYPE_DIR) {"
puts "\t\treturn(0);"
puts "\t}"
puts ""
puts "\tif (strcmp(path, ${code_tag}_data\[index\].name) != 0) {"
puts "\t\treturn(0);"
puts "\t}"
puts ""
................................................................................

puts "\t}"
puts ""
puts "\treturn(num_children);"
puts "}"
puts ""

puts "#  ifdef KITDLL_MAKE_LOADABLE"

set fd [open "vfs_kitdll_data.c"]
puts [read $fd]
close $fd


puts "static cmd_getData_t *getCmdData(const char *hashkey) {"
puts "\treturn(${code_tag}_getData);"
puts "}"
puts ""
puts "static cmd_getChildren_t *getCmdChildren(const char *hashkey) {"
puts "\treturn(${code_tag}_getChildren);"
puts "}"
puts ""

puts "int Vfs_kitdll_data_${hashkey}_Init(Tcl_Interp *interp) {"
puts "\tTcl_Command tclCreatComm_ret;"
puts "\tint tclPkgProv_ret;"
puts ""





puts "\ttclCreatComm_ret = Tcl_CreateObjCommand(interp, \"::vfs::kitdll::data::${hashkey}::getMetadata\", getMetadata, NULL, NULL);"
puts "\tif (!tclCreatComm_ret) {"
puts "\t\treturn(TCL_ERROR);"
puts "\t}"
puts ""
puts "\ttclCreatComm_ret = Tcl_CreateObjCommand(interp, \"::vfs::kitdll::data::${hashkey}::getData\", getData, NULL, NULL);"
puts "\tif (!tclCreatComm_ret) {"
puts "\t\treturn(TCL_ERROR);"
puts "\t}"
puts ""
puts "\ttclCreatComm_ret = Tcl_CreateObjCommand(interp, \"::vfs::kitdll::data::${hashkey}::getChildren\", getChildren, NULL, NULL);"
puts "\tif (!tclCreatComm_ret) {"
puts "\t\treturn(TCL_ERROR);"
puts "\t}"
puts ""
puts "\ttclPkgProv_ret = Tcl_PkgProvide(interp, \"vfs::kitdll::data::${hashkey}\", \"1.0\");"
puts ""
puts "\treturn(tclPkgProv_ret);"
puts "\t}"
puts "#  endif /* KITDLL_MAKE_LOADABLE */"

puts "#endif /* !$cpp_tag */"



|







 







|







 







|
|







 







|
|


|
|
|

|



|



|







 







|


|







 







|










|







 







|




|







 







|







 







|







 







|

|













|



>
>
>
>
>
|




|




<
<
<
<
<
|



|


1
2
3
4
5
6
7
8
9
10
11
..
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
..
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
...
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
161
162
163
164
165
166
167
168
169
...
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
...
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
...
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
...
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
...
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388





389
390
391
392
393
394
395
#! /usr/bin/env tclsh

if {[llength $argv] != 2} {
	puts stderr "Usage: dir2c.tcl <hashkey> <startdir>"

	exit 1
}

set hashkey [lindex $argv 0]
set startdir [lindex $argv 1]

................................................................................

	set ret "\"$ret\""

	return $ret
}

# This function must be kept in-sync with the generated C function below
proc cvfs_hash {path} {
	set h 0
	set g 0

	for {set idx 0} {$idx < [string length $path]} {incr idx} {
		binary scan [string index $path $idx] H* char
		set char "0x$char"

................................................................................
# Generate list of files to include in output
set files [recursive_glob $startdir]

# Insert dummy entry cooresponding to C dummy entry
set files [linsert $files 0 "__DUMMY__"]

# Produce C89 compatible header
set cpp_tag "CVFS_[string toupper $hashkey]"
set code_tag "cvfs_[string tolower $hashkey]"
set hashkey [string tolower $hashkey]

puts "#ifndef $cpp_tag"
puts "#  define $cpp_tag 1"
puts {
#  ifdef HAVE_STDC
#    ifndef HAVE_UNISTD_H
................................................................................
#  ifdef HAVE_UNISTD_H
#    include <unistd.h>
#  endif
#  ifdef HAVE_STRING_H
#    include <string.h>
#  endif

#  ifndef LOADED_CVFS_COMMON
#    define LOADED_CVFS_COMMON 1

typedef enum {
	CVFS_FILETYPE_FILE,
	CVFS_FILETYPE_DIR
} cvfs_filetype_t;

struct cvfs_data {
	const char *             name;
	unsigned long            index;
	unsigned long            size;
	cvfs_filetype_t          type;
	const unsigned char *    data;
};

static unsigned long cvfs_hash(const unsigned char *path) {
	unsigned long i, h = 0, g = 0;

	for (i = 0; path[i]; i++) {
		h = (h << 4) + path[i];
		g = h & 0xf0000000;
		if (g) {
			h ^= (g >> 24);
................................................................................
		}
		h &= ((~g) & 0xffffffffLU);
	}
        
        return(h);
}

#  endif /* !LOADED_CVFS_COMMON */}
puts ""

puts "static struct cvfs_data ${code_tag}_data\[\] = {"
puts "\t{"
puts "\t\t.name  = NULL,"
puts "\t\t.index = 0,"
puts "\t\t.type  = 0,"
puts "\t\t.size  = 0,"
puts "\t\t.data  = NULL,"
puts "\t},"
................................................................................
	set shortfile [shorten_file $startdir $file]

	unset -nocomplain finfo type
	file stat $file finfo

	switch -- $finfo(type) {
		"file" {
			set type "CVFS_FILETYPE_FILE"
			set size $finfo(size)

			set fd [open $file]
			fconfigure $fd -translation binary
			set data [read $fd]
			close $fd

			set data "(unsigned char *) [stringify $data]"
		}
		"directory" {
			set type "CVFS_FILETYPE_DIR"
			set data "NULL"
			set size 0
		}
	}

	puts "\t{"
	puts "\t\t.name  = \"$shortfile\","
................................................................................
	puts "\t\t.data  = $data,"
	puts "\t},"
}
puts "};"
puts ""

puts "static unsigned long ${code_tag}_lookup_index(const char *path) {"
puts "\tswitch (cvfs_hash((unsigned char *) path)) {"

for {set idx 1} {$idx < [llength $files]} {incr idx} {
	set file [lindex $files $idx]
	set shortfile [shorten_file $startdir $file]
	set hash [cvfs_hash $shortfile]

	lappend indexes_per_hash($hash) [list $shortfile $idx]
}

foreach {hash idx_list} [array get indexes_per_hash] {
	puts "\t\tcase $hash:"

................................................................................
}

puts "\t}"
puts "\treturn(0);"
puts "}"
puts ""

puts "static struct cvfs_data *${code_tag}_getData(const char *path, unsigned long index) {"
puts "\tif (path != NULL) {"
puts "\t\tindex = ${code_tag}_lookup_index(path);"
puts "\t}"
puts ""
puts "\tif (index == 0) {"
puts "\t\treturn(NULL);"
puts "\t}"
................................................................................
puts "\tunsigned long num_children = 0;"
puts ""
puts "\tindex = ${code_tag}_lookup_index(path);"
puts "\tif (index == 0) {"
puts "\t\treturn(0);"
puts "\t}"
puts ""
puts "\tif (${code_tag}_data\[index\].type != CVFS_FILETYPE_DIR) {"
puts "\t\treturn(0);"
puts "\t}"
puts ""
puts "\tif (strcmp(path, ${code_tag}_data\[index\].name) != 0) {"
puts "\t\treturn(0);"
puts "\t}"
puts ""
................................................................................

puts "\t}"
puts ""
puts "\treturn(num_children);"
puts "}"
puts ""

puts "#  ifdef CVFS_MAKE_LOADABLE"

set fd [open "cvfs_data.c"]
puts [read $fd]
close $fd


puts "static cmd_getData_t *getCmdData(const char *hashkey) {"
puts "\treturn(${code_tag}_getData);"
puts "}"
puts ""
puts "static cmd_getChildren_t *getCmdChildren(const char *hashkey) {"
puts "\treturn(${code_tag}_getChildren);"
puts "}"
puts ""

puts "int Cvfs_data_${hashkey}_Init(Tcl_Interp *interp) {"
puts "\tTcl_Command tclCreatComm_ret;"
puts "\tint tclPkgProv_ret;"
puts ""
puts "\ttclCreatComm_ret = Tcl_CreateObjCommand(interp, \"::vfs::cvfs::data::${hashkey}::getMetadata\", getMetadata, NULL, NULL);"
puts "\tif (!tclCreatComm_ret) {"
puts "\t\treturn(TCL_ERROR);"
puts "\t}"
puts ""
puts "\ttclCreatComm_ret = Tcl_CreateObjCommand(interp, \"::vfs::cvfs::data::${hashkey}::getData\", getData, NULL, NULL);"
puts "\tif (!tclCreatComm_ret) {"
puts "\t\treturn(TCL_ERROR);"
puts "\t}"
puts ""
puts "\ttclCreatComm_ret = Tcl_CreateObjCommand(interp, \"::vfs::cvfs::data::${hashkey}::getChildren\", getChildren, NULL, NULL);"
puts "\tif (!tclCreatComm_ret) {"
puts "\t\treturn(TCL_ERROR);"
puts "\t}"
puts ""





puts "\ttclPkgProv_ret = Tcl_PkgProvide(interp, \"vfs::cvfs::data::${hashkey}\", \"1.0\");"
puts ""
puts "\treturn(tclPkgProv_ret);"
puts "\t}"
puts "#  endif /* CVFS_MAKE_LOADABLE */"

puts "#endif /* !$cpp_tag */"