Back to index

lightning-sunbird  0.9+nobinonly
bin2rc.c
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 #include <stdio.h>
00038 #include <sys\stat.h>
00039 
00040 int main(int iArgc, char **ppArgv) {
00041     int iRetval = 1;
00042     
00043     /*  First argument, the filename to convert.
00044      *  Output to stdout, redirect to save.
00045      */
00046     char *pFileName = ppArgv[1];
00047     if(pFileName) {
00048         FILE *pFile = fopen(pFileName, "rb");
00049         if(pFile) {
00050             struct stat sInfo;
00051             
00052             /*  Stat the file for size.
00053              */
00054             if(!fstat(fileno(pFile), &sInfo)) {
00055                 int iChar;
00056                 int iX = 0;
00057                 int iFirsttime = 1;
00058                 
00059                 /*  Begin RCDATA
00060                  */
00061                 printf("BEGIN\n");
00062 
00063                 /*  First string identifies created via bin2rc.
00064                  *  Users of the RCDATA must check for this to
00065                  *      assume the format of the remainder of
00066                  *      the data.
00067                  */
00068                 printf("\t\"bin2rc generated resource\\0\",\t// bin2rc identity string\n");
00069 
00070                 /*  Next string is optional parameter on command
00071                  *      line.  If not present, an empty string.
00072                  *  Users of the RCDATA must understand this is
00073                  *      the optional string that can be used for
00074                  *      about any purpose they desire.
00075                  */
00076                 printf("\t\"%s\\0\",\t// optional command line string\n", ppArgv[2] ? ppArgv[2] : "");
00077                 
00078                 /*  Next string is the size of the original file.
00079                  *  Users of the RCDATA must understand that this
00080                  *      is the size of the file's actual contents.
00081                  */
00082                 printf("\t\"%ld\\0\"\t// data size header\n", sInfo.st_size);
00083                 
00084                 while(EOF != (iChar = fgetc(pFile))) {
00085                     /*  Comma?
00086                      */
00087                     if(0 == iFirsttime) {
00088                         iX += printf(",");
00089                     }
00090                     else {
00091                         iFirsttime = 0;
00092                     }
00093                     
00094                     /*  Newline?
00095                      */
00096                     if(iX >= 72) {
00097                         printf("\n");
00098                         iX = 0;
00099                     }
00100                     
00101                     /*  Tab?
00102                      */
00103                     if(0 == iX) {
00104                         printf("\t");
00105                         iX += 8;
00106                     }
00107                     
00108                     /*  Octal byte.
00109                      */
00110                     iX += printf("\"\\%.3o\"", iChar);
00111                     
00112                     
00113                 }
00114                 
00115                 /*  End RCDATA
00116                  */
00117                 if(0 != iX) {
00118                     printf("\n");
00119                 }
00120                 printf("END\n");
00121                 
00122                 /*  All is well.
00123                  */
00124                 iRetval = 0;
00125             }
00126             fclose(pFile);
00127             pFile = NULL;
00128         }
00129     }
00130     
00131     return(iRetval);
00132 }
00133