home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / config / bin2rc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.9 KB  |  115 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. #include <stdio.h>
  19. #include <sys\stat.h>
  20.  
  21. int main(int iArgc, char **ppArgv) {
  22.     int iRetval = 1;
  23.     
  24.     /*  First argument, the filename to convert.
  25.      *  Output to stdout, redirect to save.
  26.      */
  27.     char *pFileName = ppArgv[1];
  28.     if(pFileName) {
  29.         FILE *pFile = fopen(pFileName, "rb");
  30.         if(pFile) {
  31.             struct stat sInfo;
  32.             
  33.             /*  Stat the file for size.
  34.              */
  35.             if(!fstat(fileno(pFile), &sInfo)) {
  36.                 int iChar;
  37.                 int iX = 0;
  38.                 int iFirsttime = 1;
  39.                 
  40.                 /*  Begin RCDATA
  41.                  */
  42.                 printf("BEGIN\n");
  43.  
  44.                 /*  First string identifies created via bin2rc.
  45.                  *  Users of the RCDATA must check for this to
  46.                  *      assume the format of the remainder of
  47.                  *      the data.
  48.                  */
  49.                 printf("\t\"bin2rc generated resource\\0\",\t// bin2rc identity string\n");
  50.  
  51.                 /*  Next string is optional parameter on command
  52.                  *      line.  If not present, an empty string.
  53.                  *  Users of the RCDATA must understand this is
  54.                  *      the optional string that can be used for
  55.                  *      about any purpose they desire.
  56.                  */
  57.                 printf("\t\"%s\\0\",\t// optional command line string\n", ppArgv[2] ? ppArgv[2] : "");
  58.                 
  59.                 /*  Next string is the size of the original file.
  60.                  *  Users of the RCDATA must understand that this
  61.                  *      is the size of the file's actual contents.
  62.                  */
  63.                 printf("\t\"%ld\\0\"\t// data size header\n", sInfo.st_size);
  64.                 
  65.                 while(EOF != (iChar = fgetc(pFile))) {
  66.                     /*  Comma?
  67.                      */
  68.                     if(0 == iFirsttime) {
  69.                         iX += printf(",");
  70.                     }
  71.                     else {
  72.                         iFirsttime = 0;
  73.                     }
  74.                     
  75.                     /*  Newline?
  76.                      */
  77.                     if(iX >= 72) {
  78.                         printf("\n");
  79.                         iX = 0;
  80.                     }
  81.                     
  82.                     /*  Tab?
  83.                      */
  84.                     if(0 == iX) {
  85.                         printf("\t");
  86.                         iX += 8;
  87.                     }
  88.                     
  89.                     /*  Octal byte.
  90.                      */
  91.                     iX += printf("\"\\%.3o\"", iChar);
  92.                     
  93.                     
  94.                 }
  95.                 
  96.                 /*  End RCDATA
  97.                  */
  98.                 if(0 != iX) {
  99.                     printf("\n");
  100.                 }
  101.                 printf("END\n");
  102.                 
  103.                 /*  All is well.
  104.                  */
  105.                 iRetval = 0;
  106.             }
  107.             fclose(pFile);
  108.             pFile = NULL;
  109.         }
  110.     }
  111.     
  112.     return(iRetval);
  113. }
  114.  
  115.