1# -*- coding: utf-8 -*-
2
3#-------------------------------------------------------------------------
4# drawElements Quality Program utilities
5# --------------------------------------
6#
7# Copyright 2015 The Android Open Source Project
8#
9# Licensed under the Apache License, Version 2.0 (the "License");
10# you may not use this file except in compliance with the License.
11# You may obtain a copy of the License at
12#
13#      http://www.apache.org/licenses/LICENSE-2.0
14#
15# Unless required by applicable law or agreed to in writing, software
16# distributed under the License is distributed on an "AS IS" BASIS,
17# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18# See the License for the specific language governing permissions and
19# limitations under the License.
20#
21#-------------------------------------------------------------------------
22
23import os
24import sys
25from itertools import chain
26
27INL_HEADER_TMPL = """\
28/* WARNING: This is auto-generated file. Do not modify, since changes will
29 * be lost! Modify the generating script instead.
30 *
31 * Generated from {registryName} revision {revision}.
32 */\
33"""
34
35def genInlHeader (registryName, revision):
36	return INL_HEADER_TMPL.format(
37		registryName	= registryName,
38		revision		= str(revision))
39
40def genInlHeaderForSource (registrySource):
41	return genInlHeaderForSource(registrySource.getFilename(), registrySource.getRevision())
42
43def nextMod (val, mod):
44	if val % mod == 0:
45		return val + mod
46	else:
47		return int(val/mod)*mod + mod
48
49def indentLines (lines):
50	tabSize = 4
51
52	# Split into columns
53	lineColumns = [line.split("\t") for line in lines if line is not None]
54	if len(lineColumns) == 0:
55		return
56
57	numColumns = max(len(line) for line in lineColumns)
58
59	# Figure out max length per column
60	columnLengths = [nextMod(max(len(line[ndx]) for line in lineColumns if len(line) > ndx), tabSize) for ndx in range(numColumns)]
61
62	for line in lineColumns:
63		indented = []
64		for columnNdx, col in enumerate(line[:-1]):
65			colLen	= len(col)
66			while colLen < columnLengths[columnNdx]:
67				col		+= "\t"
68				colLen	 = nextMod(colLen, tabSize)
69			indented.append(col)
70
71		# Append last col
72		indented.append(line[-1])
73		yield "".join(indented)
74
75def readFile (filename):
76	f = open(filename, 'rb')
77	data = f.read()
78	f.close()
79	return data
80
81def writeFileIfChanged (filename, data):
82	if not os.path.exists(filename) or readFile(filename) != data:
83		if (sys.version_info < (3, 0)):
84			f = open(filename, 'wt')
85		else:
86			f = open(filename, 'wt', newline='\n')
87		f.write(data)
88		f.close()
89
90def writeLines (filename, lines):
91	text = ""
92	for line in lines:
93		text += line
94		text += "\n"
95
96	writeFileIfChanged(filename, text)
97	print(filename)
98
99def writeInlFile (filename, header, source):
100	writeLines(filename, chain([header], source))
101
102def normalizeConstant (constant):
103	value = int(constant, base=0)
104	if value >= 1 << 63:
105		suffix = 'ull'
106	elif value >= 1 << 32:
107		suffix = 'll'
108	elif value >= 1 << 31:
109		suffix = 'u'
110	else:
111		suffix = ''
112	return constant + suffix
113
114def commandParams (command):
115	if len(command.params) > 0:
116		return ", ".join(param.declaration for param in command.params)
117	else:
118		return "void"
119
120def commandArgs (command):
121	return ", ".join(param.name for param in command.params)
122