Back to index

lightning-sunbird  0.9+nobinonly
SimpleTypeLib.c
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 
00038 /* Test the structure creation and serialization APIs from xpt_struct.c */
00039 #include <stdio.h>
00040 
00041 #include "xpt_xdr.h"
00042 #include "xpt_struct.h"
00043 
00044 #define PASS(msg)                                                    \
00045   fprintf(stderr, "PASSED : %s\n", msg);
00046 
00047 #define FAIL(msg)                                                    \
00048   fprintf(stderr, "FAILURE: %s\n", msg);
00049 
00050 #define TRY_(msg, cond, silent)                                             \
00051   if ((cond) && !silent) {                                           \
00052     PASS(msg);                                                              \
00053   } else {                                                           \
00054     FAIL(msg);                                                              \
00055     return 1;                                                        \
00056   }
00057 
00058 #define TRY(msg, cond)             TRY_(msg, cond, 0)
00059 #define TRY_Q(msg, cond)    TRY_(msg, cond, 1);
00060 
00061 struct nsID iid = {
00062     0x00112233,
00063     0x4455,
00064     0x6677,
00065     {0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}
00066 };
00067 
00068 XPTTypeDescriptor td_void;
00069 
00070 int
00071 main(int argc, char **argv)
00072 {
00073     XPTArena *arena;
00074     XPTHeader *header;
00075     XPTAnnotation *ann;
00076     XPTInterfaceDescriptor *id;
00077     XPTMethodDescriptor *meth;
00078 
00079     XPTState *state;
00080     XPTCursor curs, *cursor = &curs;
00081     char *data, *head;
00082     FILE *out;
00083     uint32 len, header_sz;
00084 
00085     PRBool ok;
00086 
00087     td_void.prefix.flags = TD_VOID;
00088 
00089 #ifdef XP_MAC
00090        if (argc == 0) {
00091               static char* args[] = { "SimpleTypeLib", "simple.xpt", NULL };
00092               argc = 2;
00093               argv = args;
00094        }
00095 #endif
00096 
00097     if (argc != 2) {
00098        fprintf(stderr, "Usage: %s <filename.xpt>\n"
00099               "       Creates a simple typelib file.\n", argv[0]);
00100        
00101        return 1;
00102     }
00103 
00104     arena = XPT_NewArena(1024, sizeof(double), "main");
00105     TRY("XPT_NewArena", arena);
00106 
00107     /* construct a header */
00108     header = XPT_NewHeader(arena, 1, XPT_MAJOR_VERSION, XPT_MINOR_VERSION);
00109     TRY("NewHeader", header);
00110 
00111     
00112     ann = XPT_NewAnnotation(arena, XPT_ANN_LAST | XPT_ANN_PRIVATE,
00113                             XPT_NewStringZ(arena, "SimpleTypeLib 1.0"),
00114                             XPT_NewStringZ(arena, "See You In Rome"));
00115     TRY("NewAnnotation", ann);
00116     header->annotations = ann;
00117 
00118     header_sz = XPT_SizeOfHeaderBlock(header);
00119 
00120     id = XPT_NewInterfaceDescriptor(arena, 0, 2, 2, 0);
00121     TRY("NewInterfaceDescriptor", id);
00122     
00123     ok = XPT_FillInterfaceDirectoryEntry(arena, header->interface_directory, &iid,
00124                                     "Interface", "NS", id);
00125     TRY("FillInterfaceDirectoryEntry", ok);
00126 
00127     /* void method1(void) */
00128     meth = &id->method_descriptors[0];
00129     ok = XPT_FillMethodDescriptor(arena, meth, 0, "method1", 0);
00130     TRY("FillMethodDescriptor", ok);
00131     meth->result->flags = 0;
00132     meth->result->type.prefix.flags = TD_VOID;
00133 
00134     /* wstring method2(in uint32, in bool) */
00135     meth = &id->method_descriptors[1];
00136     ok = XPT_FillMethodDescriptor(arena, meth, 0, "method2", 2);
00137     TRY("FillMethodDescriptor", ok);
00138 
00139     meth->result->flags = 0;
00140     meth->result->type.prefix.flags = TD_PSTRING | XPT_TDP_POINTER;
00141     meth->params[0].type.prefix.flags = TD_UINT32;
00142     meth->params[0].flags = XPT_PD_IN;
00143     meth->params[1].type.prefix.flags = TD_BOOL;
00144     meth->params[1].flags = XPT_PD_IN;
00145 
00146 #if 0
00147     /* const one = 1; */
00148     id->const_descriptors[0].name = "one";
00149     id->const_descriptors[0].type.prefix.flags = TD_UINT16;
00150     id->const_descriptors[0].value.ui16 = 1;
00151     
00152     /* const squeamish = "ossifrage"; */
00153     id->const_descriptors[1].name = "squeamish";
00154     id->const_descriptors[1].type.prefix.flags = TD_PBSTR | XPT_TDP_POINTER;
00155     id->const_descriptors[1].value.string = XPT_NewStringZ(arena, "ossifrage");
00156 #endif
00157 
00158     /* serialize it */
00159     state = XPT_NewXDRState(XPT_ENCODE, NULL, 0);
00160     TRY("NewState (ENCODE)", state);
00161     
00162     ok = XPT_MakeCursor(state, XPT_HEADER, header_sz, cursor);
00163     TRY("MakeCursor", ok);
00164 
00165     ok = XPT_DoHeader(arena, cursor, &header);
00166     TRY("DoHeader", ok);
00167 
00168     out = fopen(argv[1], "wb");
00169     if (!out) {
00170         perror("FAILED: fopen");
00171         return 1;
00172     }
00173     
00174     XPT_GetXDRData(state, XPT_HEADER, &head, &len);
00175     fwrite(head, len, 1, out);
00176 
00177     XPT_GetXDRData(state, XPT_DATA, &data, &len);
00178     fwrite(data, len, 1, out);
00179 
00180     if (ferror(out) != 0 || fclose(out) != 0) {
00181         fprintf(stderr, "\nError writing file: %s\n\n", argv[1]);
00182     } else {
00183         fprintf(stderr, "\nFile written: %s\n\n", argv[1]);
00184     }
00185     XPT_DestroyXDRState(state);
00186     
00187     XPT_FreeHeader(arena, header);
00188     XPT_DestroyArena(arena);
00189 
00190     return 0;
00191 }
00192