program convbanners; uses Math; const AutobannerModuleName = '_autobanners_m.pas'; BannerStartS = '== BANNER START =='; BannersN = 10; MenuFile = 'menu.txt'; LevelFile = 'level.txt'; PauseFile = 'paused.txt'; GameOverFile = 'gameover.txt'; GameCompleteFile = 'completed.txt'; KeyInfoFile = 'keys.txt'; ExitFile = 'exit.txt'; LifeupFile = 'lifeup.txt'; SpeedupFile = 'speedup.txt'; SpeeddownFile = 'speeddown.txt'; BannerFiles: array[1..BannersN] of string = ( MenuFile, LevelFile, PauseFile, GameOverFile, GameCompleteFile, KeyInfoFile, ExitFile, LifeupFile, SpeedupFile, SpeeddownFile ); MenuVarsPrefix = 'MenuBanner'; LevelVarsPrefix = 'LevelAnnounceBanner'; PauseVarsPrefix = 'PauseBanner'; GameOverVarsPrefix = 'GameOverBanner'; GameCompleteVarsPrefix = 'GameCompleteBanner'; KeysVarsPrefix = 'KeysInfoBanner'; ExitVarsPrefix = 'ExitBanner'; SpeedupVarsPrefix = 'SpeedupBanner'; SpeeddownVarsPrefix = 'SpeeddownBanner'; LifeupVarsPrefix = 'LifeupBanner'; VarsPrefixes: array[1..BannersN] of string = ( MenuVarsPrefix, LevelVarsPrefix, PauseVarsPrefix, GameOverVarsPrefix, GameCompleteVarsPrefix, KeysVarsPrefix, ExitVarsPrefix, LifeupVarsPrefix, SpeedupVarsPrefix, SpeeddownVarsPrefix ); DecimalBase = 10; AfterImageLinesN = 2; ModuleBeginH = 13; ModuleBegin: array[1..ModuleBeginH] of string = ( '{ ************************************************** }', '{ ************************************************** }', '{ *** *** }', '{ *** *** }', '{ *** AUTOMATICALLY GENERATED FILE. DO NOT EDIT. *** }', '{ *** *** }', '{ *** *** }', '{ ************************************************** }', '{ ************************************************** }', 'unit _autobanners_m;', '', 'interface', '' ); BannerImageTypeStr = 'BannerImage = array[1..MaxBannerHeight] of string[MaxBannerWidth];'; ModuleEndH = 2; ModuleEnd: array[1..ModuleEndH] of string = ( 'implementation', 'end.' ); procedure AppendText(var f: text; var t: array of string; h: integer); var i: integer; begin for i := 1 to h do writeln(f, t[i - 1]) end; function ParsedStrLength(var s: string): integer; var res: integer = 0; backtickCnt: integer = 0; i, sLen: integer; begin sLen := Length(s); res := sLen; for i := 1 to Length(s) do if s[i] = '''' then backtickCnt := backtickCnt + 1; if s[sLen] = ',' then res := res - 1; res := res - 2; {Subtract first and last '} backtickCnt := backtickCnt - 2; {Subtract first and last '} ParsedStrLength := res - (backtickCnt div 2) end; procedure GetMaxBannersDimensions(var h, w: integer); var i, len: integer; curH: integer = 0; t: text; ln: string; isBanner: boolean = false; begin h := 0; w := 0; for i := 1 to BannersN do begin curH := 0; isBanner := false; assign(t, BannerFiles[i]); reset(t); while not eof(t) do begin readln(t, ln); if not isBanner then begin isBanner := (ln = BannerStartS); continue end; curH := curH + 1; len := ParsedStrLength(ln); w := Max(w, len); if len > 0 then h := max(h, curH) end end end; procedure AppendMaxBannersDimensions(var t: text; maxH, maxW: integer); begin writeln(t, 'MaxBannerHeight = ', maxH, ';'); writeln(t, 'MaxBannerWidth = ', maxW, ';'); writeln(t, '') end; procedure AppendBannersType(var t: text); begin writeln(t, 'type'); writeln(t, BannerImageTypeStr); writeln(t, '') end; procedure GetBannerDimensions(var fileName: string; var h, w: integer); var sLen: integer; hNow: integer = 0; fileFrom: text; ln: string; isBanner: boolean = false; begin w := 0; h := 0; assign(fileFrom, fileName); reset(fileFrom); while not eof(fileFrom) do begin readln(fileFrom, ln); if not isBanner then begin isBanner := (ln = BannerStartS); continue end; sLen := ParsedStrLength(ln); w := Max(w, sLen); hNow := hNow + 1; if sLen > 0 then h := hNow end end; procedure ParseNum(var ln, res: string; idx: integer); var i, n: integer; begin n := Length(ln); for i := idx to n do begin if (ln[i] <= '0') or (ln[i] >= '9') then begin res := copy(ln, idx, i - 1); break end; if i = n then res := copy(ln, idx, i) end end; procedure AppendConst(var fileTo: text; var ln: string); var idx: integer = 1; lenS: integer; num: string; constName: string = ''; begin lenS := Length(ln); while (idx <= lenS) do begin if ln[idx] = ' ' then break; idx := idx + 1 end; if (idx >= lenS) or (idx = 1) then exit; constName := copy(ln, 1, idx - 1); ParseNum(ln, num, idx + 1); writeln(fileTo, constName, ' = ', num, ';') end; procedure AppendConsts(var fileFrom, fileTo: text); var isBanner: boolean = false; ln: string; begin while not isBanner do begin readln(fileFrom, ln); isBanner := (ln = BannerStartS); if not isBanner and (Length(ln) > 0) then AppendConst(fileTo, ln) end; end; procedure AppendBanner(var fileTo: text; bannerImageH: integer; var fileName, varPrefix: string); var w, h, i: integer; fileFrom: text; ln: string; begin GetBannerDimensions(fileName, h, w); writeln(fileTo, varPrefix, 'Height = ', h, ';'); writeln(fileTo, varPrefix, 'Width = ', w, ';'); assign(fileFrom, fileName); reset(fileFrom); AppendConsts(fileFrom, fileTo); writeln(fileTo, varPrefix, ': BannerImage = ('); while not eof(fileFrom) do begin readln(fileFrom, ln); if (ln[Length(ln)] = ',') or (h = bannerImageH) then writeln(fileTo, ln) else writeln(fileTo, ln, ',') end; for i := 1 to (bannerImageH - h - 1) do writeln(fileTo, ''''','); if bannerImageH <> h then writeln(fileTo, ''''''); writeln(fileTo, ');'); writeln(fileTo, '') end; procedure CreateAutobannerModule; var i, maxH, maxW: integer; newModule: text; begin assign(newModule, AutobannerModuleName); rewrite(newModule); AppendText(newModule, ModuleBegin, ModuleBeginH); writeln(newModule, 'const'); GetMaxBannersDimensions(maxH, maxW); AppendMaxBannersDimensions(newModule, maxH, maxW); AppendBannersType(newModule); writeln(newModule, 'const'); for i := 1 to BannersN do AppendBanner(newModule, maxH, BannerFiles[i], VarsPrefixes[i]); AppendText(newModule, ModuleEnd, ModuleEndH); close(newModule) end; begin CreateAutobannerModule end.