hinote.c

Go to the documentation of this file.
00001 /*
00002  * $Id: hinote.c,v 1.22 2006-10-12 14:21:22 desrod Exp $
00003  *
00004  * hinote.c:  Translate Hi-Note data formats
00005  *
00006  * Copyright 1997 Bill Goodman
00007  *
00008  * This library is free software; you can redistribute it and/or modify it
00009  * under the terms of the GNU Library General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or (at
00011  * your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library
00016  * General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Library General Public License
00019  * along with this library; if not, write to the Free Software Foundation,
00020  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00021  */
00022 
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 
00027 #include "pi-hinote.h"
00028 
00029 /***********************************************************************
00030  *
00031  * Function:    free_HiNoteNote
00032  *
00033  * Summary:     frees HiNoteNote_t members
00034  *
00035  * Parameters:  HiNoteNote_t*
00036  *
00037  * Returns:     void
00038  *
00039  ***********************************************************************/
00040 void
00041 free_HiNoteNote(HiNoteNote_t *hinote)
00042 {
00043         if (hinote->text != NULL) {
00044                 free(hinote->text);
00045                 hinote->text = NULL;
00046         }
00047 }
00048 
00049 /***********************************************************************
00050  *
00051  * Function:    unpack_HiNoteNote
00052  *
00053  * Summary:     Unpack a HiNote record
00054  *
00055  * Parameters:  HiNoteNote_t*, char* to buffer, buffer length
00056  *
00057  * Returns:     effective buffer length
00058  *
00059  ***********************************************************************/
00060 int
00061 unpack_HiNoteNote(HiNoteNote_t *hinote, unsigned char *buffer, int len)
00062 {
00063         if (len < 3)
00064                 return 0;
00065 
00066         hinote->flags   = buffer[0];
00067         hinote->level   = buffer[1];
00068         hinote->text    = strdup((char *) &buffer[2]);
00069 
00070         return strlen((char *) &buffer[2]) + 3;
00071 }
00072 
00073 /***********************************************************************
00074  *
00075  * Function:    pack_HiNoteNote
00076  *
00077  * Summary:     Pack a HiNote record
00078  *
00079  * Parameters:  HiNoteNote_t*
00080  *
00081  * Returns:     Nothing
00082  *
00083  ***********************************************************************/
00084 int
00085 pack_HiNoteNote(HiNoteNote_t *hinote, unsigned char *buffer, int len)
00086 {
00087         int     destlen;
00088 
00089         destlen = 3;
00090         if (hinote->text)
00091                 destlen += strlen(hinote->text);
00092 
00093         if (!buffer)
00094                 return destlen;
00095         if (len < destlen)
00096                 return 0;
00097 
00098         buffer[0] = hinote->flags;
00099         buffer[1] = hinote->level;
00100 
00101         if (hinote->text)
00102                 strcpy((char *) &buffer[2], hinote->text);
00103         else {
00104                 buffer[2] = 0;
00105         }
00106         return destlen;
00107 }
00108 
00109 
00110 /***********************************************************************
00111  *
00112  * Function:    unpack_HiNoteAppInfo
00113  *
00114  * Summary:     Unpack the HiNote AppInfo block
00115  *
00116  * Parameters:  HiNoteAppInfo_t*, char* to record, record length
00117  *
00118  * Returns:     effective buffer length
00119  *
00120  ***********************************************************************/
00121 int
00122 unpack_HiNoteAppInfo(HiNoteAppInfo_t *appinfo, unsigned char *record, size_t len)
00123 {
00124         int     i,
00125                 idx;
00126         unsigned char *start;
00127 
00128         start = record;
00129         i = unpack_CategoryAppInfo(&appinfo->category, record, len);
00130         if (!i)
00131                 return i;
00132         record += i;
00133         len -= i;
00134         if (len < 48)
00135                 return 0;
00136         for (idx = 0; i < 48; i++)
00137                 appinfo->reserved[i] = *record++;
00138         return (record - start);
00139 }
00140 
00141 
00142 /***********************************************************************
00143  *
00144  * Function:    pack_HiNoteAppInfo
00145  *
00146  * Summary:     Pack the HiNote AppInfo block
00147  *
00148  * Parameters:  HiNoteAppInfo_t*, char* record, length of record
00149  *
00150  * Returns:     effective record length
00151  *
00152  ***********************************************************************/
00153 int
00154 pack_HiNoteAppInfo(HiNoteAppInfo_t *appinfo, unsigned char *record, size_t len)
00155 {
00156         int     i,
00157                 idx;
00158         unsigned char *start = record;
00159 
00160         i = pack_CategoryAppInfo(&appinfo->category, record, len);
00161         if (i == 0)             /* category pack failed */
00162                 return 0;
00163         if (!record)
00164                 return i + 48;
00165         record += i;
00166         len -= i;
00167         if (len < 48)
00168                 return (record - start);
00169         for (idx = 0; i < 48; i++)
00170                 *record++ = appinfo->reserved[i];
00171 
00172         return (record - start);
00173 }
00174 
00175 /* vi: set ts=8 sw=4 sts=4 noexpandtab: cin */
00176 /* ex: set tabstop=4 expandtab: */
00177 /* Local Variables: */
00178 /* indent-tabs-mode: t */
00179 /* c-basic-offset: 8 */
00180 /* End: */

© 1996-2007 by pilot-link.org. All rights reserved.