update for HEAD-2003050101
[reactos.git] / lib / freetype / src / tools / docmaker / utils.py
1 import string, sys, os
2
3 # current output directory
4 #
5 output_dir = None
6
7
8 # This function is used to sort the index.  It is a simple lexicographical
9 # sort, except that it places capital letters before lowercase ones.
10 #
11 def index_sort( s1, s2 ):
12     if not s1:
13         return -1
14
15     if not s2:
16         return 1
17
18     l1 = len( s1 )
19     l2 = len( s2 )
20     m1 = string.lower( s1 )
21     m2 = string.lower( s2 )
22
23     for i in range( l1 ):
24         if i >= l2 or m1[i] > m2[i]:
25             return 1
26
27         if m1[i] < m2[i]:
28             return -1
29
30         if s1[i] < s2[i]:
31             return -1
32
33         if s1[i] > s2[i]:
34             return 1
35
36     if l2 > l1:
37         return -1
38
39     return 0
40
41 # Sort input_list, placing the elements of order_list in front.
42 #
43 def sort_order_list( input_list, order_list ):
44     new_list = order_list[:]
45     for id in input_list:
46         if not id in order_list:
47             new_list.append( id )
48     return new_list
49
50
51
52 # Open the standard output to a given project documentation file.  Use
53 # "output_dir" to determine the filename location if necessary and save the
54 # old stdout in a tuple that is returned by this function.
55 #
56 def open_output( filename ):
57     global output_dir
58
59     if output_dir and output_dir != "":
60         filename = output_dir + os.sep + filename
61
62     old_stdout = sys.stdout
63     new_file   = open( filename, "w" )
64     sys.stdout = new_file
65
66     return ( new_file, old_stdout )
67
68
69 # Close the output that was returned by "close_output".
70 #
71 def close_output( output ):
72     output[0].close()
73     sys.stdout = output[1]
74
75
76 # Check output directory.
77 #
78 def check_output( ):
79     global output_dir
80     if output_dir:
81         if output_dir != "":
82             if not os.path.isdir( output_dir ):
83                 sys.stderr.write( "argument" + " '" + output_dir + "' " +
84                                   "is not a valid directory" )
85                 sys.exit( 2 )
86         else:
87             output_dir = None