| fb-doc 0.2 documentation tool for FreeBasic

- Main Page
- Tutorial
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
fb-doc_options.bas
Go to the documentation of this file.
1 /'* \file fb-doc_options.bas
2 \brief The source code for the \ref Options class
3
4 This file contains the source code for the Options class. It's used
5 to scan the command line options and control the execution of fb-doc.
6
7 '/
8
9 #INCLUDE ONCE "fb-doc_options.bi"
10
11 /'* \brief Read options and parameters from the command line
12
13 The constructor scans all command line arguments and checks for
14 options and their parameters (, see \ref sectionoptions for
15 details). Options may be specified in short form (starting with a
16 single minus character) or in human readable long form (starting
17 with two minus characters). Some options max have an additinal
18 parameter.
19
20 Each command line argument that is neither an option nor a parameter
21 gets interpreted as a file name or pattern. fb-doc collects them in
22 a queue and operates on this queue afterwards. The queue may have
23 mixed entries (names and patterns). It's recommended to specify
24 queue entries in single or double quotes.
25
26 '/
27 CONSTRUCTOR Options()
28 Types = FB_STYLE
29 Function_ = @cppCreateFunction
30 Variable_ = @cppCreateTypNam
31
32 VAR i = 1
33 WHILE LEN(COMMAND(i)) ' evaluate options
34 SELECT CASE COMMAND(i)
35 CASE "--help", "-h" : RunMode = HELP_MESSAGE
36 CASE "--version", "-v" : RunMode = VERSION_MESSAGE
37 CASE "--asterix", "-a" : Asterix = GTK_DOC
38 CASE "--cstyle", "-c"
39 Types = C_STYLE
40 Function_ = @cCreateFunction
41 Variable_ = @cCreateTypNam
42 CASE "--emitter", "-e" : chooseEmitter(i)
43 CASE "--fileoutput", "-f"
44 RunMode = FILE_OUT
45 EmitIF = Emitters(C_SOURCE)
46 CASE "--geany", "-g"
47 RunMode = GEANYFILTER
48 EmitIF = Emitters(GTK_DOC_TEMPLATES)
49 chooseEmitter(i)
50 CASE "--list", "-l"
51 RunMode = SCAN_LIST
52 EmitIF = Emitters(DOXYGEN_CALLEES)
53 IF LCASE(COMMAND(i + 1)) = "ext" THEN AllCallees = -1 : i += 1
54 CASE "--path", "-p"
55 SELECT CASE AS CONST ASC(COMMAND(i + 1))
56 CASE ASC("-")
57 CASE ASC(""""), ASC("'") : i += 1
58 OutPath = MID(COMMAND(i), 2, LEN(COMMAND(i)) - 2)
59 CASE ELSE : i += 1 : OutPath = COMMAND(i)
60 END SELECT
61 CASE "--recursiv", "-r" : InRecursiv = 1
62 CASE "--tree", "-t" : InTree = 1 : AllCallees = -1
63 CASE ELSE
64 SELECT CASE AS CONST ASC(COMMAND(i))
65 CASE ASC("-") : ERROUT("Unknown parameter: " & COMMAND(i))
66 CASE ASC(""""), ASC("'")
67 InFile &= MID(COMMAND(i), 2, LEN(COMMAND(i)) - 2) & ";"
68 CASE ELSE : InFile &= COMMAND(i) & ";"
69 END SELECT
70 END SELECT : i += 1
71 WEND
72
73 IF 0 = LEN(InFile) THEN _
74 IF RunMode = DOXYFILTER THEN RunMode = HELP_MESSAGE _
75 ELSE InFile = "*.bas;*.bi;"
76
77 IF RunMode = FILE_OUT THEN _
78 IF 0 = LEN(OutPath) THEN _
79 OutPath = ".." & SLASH & "doc" & SLASH & "c_src" & SLASH
80
81 Pars = NEW Parser(EmitIF)
82 END CONSTRUCTOR
83
84
85 /'* \brief The destructor
86
87 Delete the memory used for an external emitter (if any) and for the
88 Parser.
89
90 '/
91 DESTRUCTOR Options()
92 IF DllEmitter THEN DYLIBFREE(OPT->DllEmitter)
93 DELETE Pars
94 END DESTRUCTOR
95
96
97 /'* \brief Check next parameter for emitter name
98 \param I the index of the current parameter
99
100 This function checks the following command line parameter (I + 1)
101 against the names of the internal emitters. The check is not
102 case-sensitve and is done for a fragment as well, so it's enough to
103 specify some of the start characters of the emitter name (ie \em dox
104 instead of \em DoxygenTemplates).
105
106 In case of no match it tries to load an external emitter.
107
108 In case of a match of an internal or external emitter the index gets
109 increased.
110
111 '/
112 SUB Options.chooseEmitter(BYREF I AS INTEGER)
113 VAR p = I + 1
114 IF 1 > LEN(COMMAND(p)) ORELSE LEFT(COMMAND(p), 1) = "-" THEN EXIT SUB
115 VAR t = UCASE(COMMAND(p)), l = LEN(t)
116 FOR e AS INTEGER = UBOUND(Emitters) TO 0 STEP -1
117 IF t <> UCASE(LEFT(Emitters(e)->Nam, l)) THEN CONTINUE FOR
118 EmitIF = Emitters(e) : I = p : EXIT SUB
119 NEXT
120
121 #IFDEF __FB_DOS__
122 ERROUT("No dll support on this platform!")
123 #ELSE
124 VAR f = COMMAND(p)
125 SELECT CASE AS CONST ASC(f)
126 CASE ASC(""""), ASC("'") : f = MID(f, 2, LEN(f) - 2)
127 END SELECT
128
129 DllEmitter = DYLIBLOAD(f) : IF 0 = DllEmitter THEN EXIT SUB
130 DIM ini AS FUNCTION CDECL() AS EmitterIF PTR
131 ini = DYLIBSYMBOL(DllEmitter, "EMITTERINIT") : IF 0 = ini THEN EXIT SUB
132 EmitIF = ini() : I = p
133 #ENDIF
134 END SUB
135
136
137 /'* \brief Create folder (if not exists)
138 \param P the folder name
139 \returns -1 on error, else 0
140
141 This SUB gets called when a recursiv scan walks through subfolders
142 in a source tree. It creates matching subfolders in the target
143 directory for the output files.
144
145 '/
146 FUNCTION Options.checkDir(BYREF P AS STRING) AS INTEGER
147 VAR a = 1, e = a, l = LEN(P)
148 #IFDEF __FB_UNIX__
149 IF LEFT(P, 1) = "/" THEN a = 2 : CHDIR("/")
150 #ELSE
151 IF MID(P, 2, 1) = ":" THEN a = 4 : CHDIR(LEFT(P, 3))
152 #ENDIF
153
154 DO
155 e = INSTR(a, P, SLASH) : IF e = 0 THEN e = l + 1
156 VAR n = MID(P, a, e - a)
157 IF CHDIR(n) THEN MKDIR(n) : IF CHDIR(n) THEN RETURN -1
158 a = e + 1
159 LOOP UNTIL a > l : RETURN 0
160 END FUNCTION
161
162
163 /'* \brief Operate on all files matching a pattern
164
165 In case of file input this SUB scans a directory for matching file
166 names (and its subfolders if option \em --recursiv is given). It
167 opens the output file (when option \em --fileoutput is given) and
168 calls the Parser to operate on the file.
169
170 '/
171 SUB Options.Pattern()
172 IF InRecursiv > 0 THEN
173 VAR n = DIR("*", fbDirectory), t = ""
174 WHILE LEN(n)
175 t &= n & ";"
176 n = DIR()
177 WEND
178
179 VAR a = 1, e = a, l = LEN(t)
180 WHILE a < l
181 e = INSTR(a, t, ";")
182 n = MID(t, a, e - a)
183 VAR r = LEN(OutPath)
184 CHDIR(n)
185 OutPath &= n & SLASH
186 Pattern()
187 OutPath = LEFT(OutPath, r)
188 CHDIR ("..")
189 a = e + 1
190 WEND
191 END IF
192
193 VAR c = 0, r = "", n = DIR(InPattern) : IF 0 = LEN(n) THEN EXIT SUB
194 IF RunMode = FILE_OUT THEN
195 VAR d = CURDIR()
196 VAR r = checkDir(OutPath)
197 CHDIR(d) : IF r THEN EXIT SUB
198 END IF
199 DO
200 SELECT CASE AS CONST RunMode
201 CASE FILE_OUT
202 MSG_LINE(n)
203 VAR f = OutPath & LEFT(n, INSTRREV(n, "."))
204 IF LCASE(RIGHT(n, 3)) = ".bi" THEN f &= "h" ELSE f &= "c"
205 Pars->Ocha = FREEFILE
206 IF OPEN(f FOR OUTPUT AS #Pars->Ocha) THEN
207 ERROUT("Couldn't write " & f)
208 ELSE
209 Pars->File_(n)
210 CLOSE #Pars->Ocha
211 ERROUT(" --> " & f)
212 END IF
213 CASE SCAN_LIST
214 MSG_LINE(n)
215 Pars->File_(n)
216 ERROUT(" --> scanned")
217 CASE ELSE
218 Pars->File_(n)
219 END SELECT
220 c += 1 : n = DIR()
221 LOOP UNTIL 1 > LEN(n)
222 END SUB