Annotation of rpm2html/language.c, revision 1.4

1.1       veillard    1: /*
                      2:  * language.c: code for the Localization support.
                      3:  *
                      4:  * Copyright (c) 1997 Daniel Veillard <veillard@apocalypse.org>
                      5:  * See COPYING for the status of this software.
                      6:  *
1.4     ! veillard    7:  * $Id: language.c,v 1.3 1998/02/12 04:24:25 veillard Exp $
1.1       veillard    8:  */
                      9: 
                     10: #include <config.h>
                     11: #include <stdio.h>
                     12: #include <stdlib.h>
                     13: #include <string.h>
                     14: #ifdef HAVE_UNISTD_H
                     15: #include <unistd.h>
                     16: #endif
                     17: 
                     18: #include "rpm2html.h"
                     19: #include "rpmdata.h"
                     20: #include "html.h"
                     21: 
                     22: char *localizedStrings[] = {
                     23:     ".html",                   /* HTML files suffix */
                     24:     "Generated by",
                     25:     "index.html",
                     26:     "Groups.html",
                     27:     "ByDate.html",
                     28:     "ByName.html",
                     29:     "Vendors.html",
                     30:     "Distribs.html",
                     31:     "Welcome to the RPM repository on",
                     32:     "<p>\n\
                     33: <strong>rpm2html</strong> generate automatically Web pages describing a set of\n\
                     34: <a href=\"http://www.rpm.org/\">RPM</a> packages.</p>\n\
                     35: <p>\n\
                     36: The goal of rpm2html is also to identify the dependancies between various\n\
1.4     ! veillard   37: packages, and being able to find the packages providing the resources\n\
1.1       veillard   38: needed to install another package. Every package is analyzed to retrieve its\n\
1.4     ! veillard   39: dependancies and the resources it offers. These relationship are expressed\n\
1.1       veillard   40: using hyperlinks in the the generated pages. Finding the package providing\n\
1.4     ! veillard   41: the resource you need is just a matter of a few clicks!</p>\n\
1.1       veillard   42: <p>\n\
                     43: The ultimate commodity is ensured by indexing this set of pages, allowing to\n\
1.3       veillard   44: find instantaneously the packages providing some functionnalities (as\n\
1.1       veillard   45: long as the package maintainer has properly commented the RPM).</p>\n",
                     46:     "This archive hosts %d RPMs representing %d MBytes of data",
                     47:     "On this machine %d RPMs are installed representing %d MBytes of data",
                     48:     "The list of ",
                     49:     "RPM indexed by category",
                     50:     "RPM indexed by date of creation",
                     51:     "RPM indexed by name",
                     52:     "RPM indexed by maintainer",
                     53:     "RPM indexed by distribution",
                     54:     "RPM indexed by date of installation",
                     55:     "Repository for sources",
                     56:     "Local mirror",
                     57:     "Mirrors",
                     58:     "Generation took",
                     59:     "seconds",
                     60:     "Welcome to the RPM description of",
                     61:     "From",
                     62:     "Name",
                     63:     "Distribution",
                     64:     "Version",
                     65:     "Vendor",
                     66:     "Release",
                     67:     "Build date",
                     68:     "Install date",
                     69:     "Group",
                     70:     "Build host",
                     71:     "Size",
                     72:     "Source RPM",
                     73:     "Packager",
                     74:     "Url",
                     75:     "Summary",
                     76:     "Provides",
                     77:     "Requires",
                     78:     "Copyright",
                     79:     "Files",
                     80:     "No Filelist in the Package !",
                     81:     "No summary !",
1.4     ! veillard   82:     "RPM resource",
1.1       veillard   83:     "Provided by",
                     84:     "RPM sorted by Group",
                     85:     "RPM of Group",
                     86:     "RPM sorted by Distribution",
                     87:     "RPM of Distribution",
                     88:     "RPM sorted by Vendor",
                     89:     "RPM shipped by",
                     90:     "RPM sorted by creation date",
                     91:     "RPM sorted by installation date",
                     92:     "RPMs less than three days old",
                     93:     "RPMs less than one week old",
                     94:     "RPMs less than two weeks old",
                     95:     "RPMs less than one month old",
                     96:     "RPMs more than 1 months old",
                     97:     "RPMs installed less than three days ago",
                     98:     "RPMs installed less than one week ago",
                     99:     "RPMs installed less than two weeks ago",
                    100:     "RPMs installed less than one month ago",
                    101:     "RPMs installed more than 1 months ago",
                    102:     "RPM sorted by Name",
                    103:     "No description !",
                    104:     "Unknown",
                    105:     "None",
                    106:     "unknown/group",
1.2       veillard  107:     "unknown.host",
1.3       veillard  108:     "Index",
                    109:     "Packages beginning with letter"
1.1       veillard  110: };
                    111: 
                    112: #define NB_STRINGS (sizeof(localizedStrings)/sizeof(char *))
                    113: 
                    114: /****************************************************************
                    115:  *                                                             *
                    116:  *             The language file parser                        *
                    117:  *                                                             *
                    118:  ****************************************************************/
                    119: 
                    120: /*
                    121:  * A few macro needed to help building the parser
                    122:  */
                    123: 
                    124: #define IS_BLANK(ptr) \
                    125:      (((*(ptr)) == ' ') || ((*(ptr)) == '\b') || \
                    126:       ((*(ptr)) == '\n') || ((*(ptr)) == '\r'))
                    127: #define SKIP_BLANK(ptr) \
                    128:      { while (((*(ptr)) == ' ') || ((*(ptr)) == '\b') || \
                    129:               ((*(ptr)) == '\n') || ((*(ptr)) == '\r')) ptr++; }
                    130: #define GOTO_EQL(ptr) \
                    131:      { while (((*(ptr)) != '\0') && ((*(ptr)) != '=') && \
                    132:               ((*(ptr)) != '\n') && ((*(ptr)) != '\r')) ptr++; }
                    133: #define GOTO_EOL(ptr) \
                    134:      { while (((*(ptr)) != '\0') && \
                    135:               ((*(ptr)) != '\n') && ((*(ptr)) != '\r')) ptr++; }
                    136: 
                    137: 
                    138: /*
                    139:  * parse a language file
                    140:  */
                    141: int readLanguageFile(char *filename)
                    142: {
                    143:     FILE *input;
                    144:     char *str;
                    145:     char line[1000];
                    146:     char buffer[50000];
                    147:     int currentString;
                    148:     int len;
                    149: 
                    150:     input = fopen(filename, "r");
                    151:     if (input == NULL) {
                    152:        fprintf(stderr, "Cannot read language from %s :\n", filename);
                    153:        perror("fopen failed");
                    154:        return -1;
                    155:     }
                    156: 
                    157:     /*
                    158:      * all the localized strings are filled in one after the other.
                    159:      */
                    160:     buffer[0] = '\0';
                    161:     currentString = 0;
                    162: 
                    163:     while (1) {
                    164:        /*
                    165:         * read one line
                    166:         */
                    167:        if (fgets(&line[0], sizeof(line) - 1, input) == NULL)
                    168:            break;
                    169: 
                    170:        str = &line[0];
                    171:        line[sizeof(line) - 1] = '\0';
                    172:        len = strlen(line);
                    173:        if ((len > 0) && (line[len - 1] == '\n'))
                    174:            line[len - 1] = '\0';
                    175:        SKIP_BLANK(str)
                    176: 
                    177:        /*
                    178:         * Comment starts with a semicolumn.
                    179:         */
                    180:        if (*str == ';')
                    181:        continue;
                    182: 
                    183:        /*
                    184:         * an empty line is a field separator.
                    185:         */
                    186:        if (*str == '\0') {
                    187:            if (buffer[0] != '\0') {
                    188:                /*
                    189:                 * Check for localizedStrings overflow.
                    190:                 */
                    191:                if (currentString >= NB_STRINGS) {
                    192:                    fprintf(stderr,
                    193:              "File %s contains too many localized messages (%d expected)\n",
                    194:                      filename, NB_STRINGS);
                    195:                    break;
                    196:                }
                    197: 
                    198:                /*
                    199:                 * the last paragraph correspond to the new localized
                    200:                 * string. Replace the old one and reset the buffer.
                    201:                 */
                    202:                localizedStrings[currentString] = strdup(buffer);
                    203:                currentString++;
                    204:                buffer[0] = '\0';
                    205:            }
                    206:            continue;
                    207:        }
                    208: 
                    209:        /*
                    210:         * Aggregate the current line to the buffer.
                    211:         */
                    212:        if (buffer[0] == '\0')
                    213:            strcpy(buffer, line);
                    214:        else {
                    215:            /*
                    216:             * this is a multiline text field
                    217:             */
                    218:            strcat(buffer, "\n");
                    219:            strcat(buffer, line);
                    220:        }
                    221:     }
                    222: 
                    223:     fclose(input);
                    224:     return (0);
                    225: }
                    226: 
                    227: /*
                    228:  * dump the internal set of string to an external language file.
                    229:  */
                    230: int writeLanguageFile(char *filename)
                    231: {
                    232:     FILE *output;
                    233:     int currentString;
                    234: 
                    235:     output = fopen(filename, "w");
                    236:     if (output == NULL) {
                    237:        fprintf(stderr, "Cannot write language to %s :\n", filename);
                    238:        perror("fopen failed");
                    239:        return -1;
                    240:     }
                    241:     fprintf(output, ";\n; Automatically generated %s %s language file\n;\n",
                    242:             RPM2HTML_NAME, RPM2HTML_VER);
                    243:     for (currentString = 0; currentString < NB_STRINGS; currentString++) {
                    244:        fprintf(output, "%s\n\n", localizedStrings[currentString]);
                    245:     }
                    246:     fclose(output);
                    247:     return (0);
                    248: }
                    249: 

Webmaster