|
|
1.1 root 1: /****************************************************************************
2: *
3: * File : profile.c
4: * Date Created : 10/14/94
5: * Description :
6: *
7: * Programmer(s) : Nick Skrepetos
8: * Last Modification : 12/10/94 - 11:56:16 PM
9: * Additional Notes :
10: *
11: *****************************************************************************
12: * Copyright (c) 1993-95, HMI, Inc. All Rights Reserved *
13: ****************************************************************************/
14:
15: #include <stdio.h>
16: #include <stdlib.h>
17: #include <dos.h>
18: #include <fcntl.h>
19: #include <string.h>
20: #include <io.h>
21: #include <ctype.h>
22: #include "sos.h"
23: #include "profile.h"
24: #include <memcheck.h>
25:
26: /****************************************************************************
27:
28: Possible Additions:
29:
30: hmiINIAddItemDecimal( ) adds new item w/decimal
31: hmiINIAddItemQuery( ) adds new item w/query Yes/No
32: hmiINIAddRawString( ) adds new raw string
33:
34: Error Checking:
35:
36: allow TABS to be skipped as "White Space" in addition to spaces so
37: that editors that use tabs will work also.
38:
39: allow String functions to support lists with comma (,) seperators
40: like the decimal functions do.
41:
42: allow location functions to be case sensitive or insensitive
43:
44: get rid of compiler dependent stuff like memcpy, stricmp
45:
46: ****************************************************************************/
47:
48: // local data
49: static PSTR szHexNumbers = "0123456789ABCDEF";
50: static WORD wMultiplier[] = { 1, 16, 256, 4096, 65536, 1048576, 16777216, 268435456 };
51:
52: // local function prototypes
53: WORD hmiINIHex2Decimal ( PSTR szHexValue );
54: WORD hmiINIGetHexIndex ( BYTE bValue );
55:
56:
57: /****************************************************************************
58: *
59: * Syntax
60: *
61: * BOOL cdecl hmiINIOpen( _INI_INSTANCE * sInstance, PSTR szName )
62: *
63: * Description
64: *
65: * opens and instance of a .ini file
66: *
67: * Parameters
68: *
69: * Type Description
70: * --------------------------
71: * sInstance pointer to an .ini instance
72: * szName pointer to the name of the .ini file
73: *
74: * Return
75: *
76: * _TRUE the file was opened correctly
77: * _FALSE there was a problem opening the file
78: *
79: ****************************************************************************/
80: BOOL cdecl hmiINIOpen( _INI_INSTANCE * sInstance, PSTR szName )
81: {
82: WORD hFile;
83:
84: // save the name of the .ini file
85: strcpy( sInstance->szName, szName );
86:
87: // open .ini file, return error if file is not
88: // found.
89: if ( ( hFile = open( szName, O_RDONLY | O_BINARY ) ) == -1 )
90: return( _FALSE );
91:
92: // determine size of file
93: sInstance->wSize = lseek( hFile, 0, SEEK_END );
94:
95: // set the new maximum size
96: sInstance->wMaxSize = sInstance->wSize + _INI_EXTRA_MEMORY;
97:
98: // seek back to start of file
99: lseek( hFile, 0, SEEK_SET );
100:
101: // allocate memory for the file
102: if ( ( sInstance->pData = ( PSTR )malloc( sInstance->wMaxSize ) ) == _NULL )
103: {
104: // close file
105: close( hFile );
106:
107: // return error, not enough memory
108: return( _FALSE );
109: }
110:
111: // read in file
112: if ( read( hFile, sInstance->pData, sInstance->wSize ) != sInstance->wSize )
113: {
114: // close file
115: close( hFile );
116:
117: // free memory
118: free( sInstance->pData );
119:
120: // return error, not file size incorrect
121: return( _FALSE );
122: }
123:
124: // close file
125: close( hFile );
126:
127: // init current position
128: sInstance->pCurrent = sInstance->pData;
129: sInstance->wCurrent = 0;
130:
131: // initalize current item pointer
132: sInstance->pItem = _NULL;
133: sInstance->pList = _NULL;
134: sInstance->pItemPtr = _NULL;
135: sInstance->pListPtr = _NULL;
136:
137: // reset the modified flag to indicate that the
138: // file is unmodified.
139: sInstance->wFlags &= ~_INI_MODIFIED;
140:
141: // return success
142: return( _TRUE );
143: }
144:
145: /****************************************************************************
146: *
147: * Syntax
148: *
149: * BOOL cdecl hmiINIClose( _INI_INSTANCE * sInstance )
150: *
151: * Description
152: *
153: * close and instance of a .ini file. note that if the file is modified
154: * it will be written back to the original file.
155: *
156: * Parameters
157: *
158: * Type Description
159: * --------------------------
160: * sInstance pointer to .ini instance
161: *
162: * Return
163: *
164: * _TRUE file was closed and/or written correctly
165: * _FALSE a problem was encountered when writing/closing the file
166: *
167: ****************************************************************************/
168: BOOL cdecl hmiINIClose( _INI_INSTANCE * sInstance )
169: {
170: WORD hFile;
171:
172: // determine if the .ini file has been modified
173: if ( sInstance->wFlags & _INI_MODIFIED )
174: {
175: // create and open file
176: if ( ( hFile = open( (const char * )sInstance->szName, O_CREAT | O_TRUNC | O_RDWR | O_BINARY, 0 ) ) == -1 )
177: {
178: // free memory
179: free( sInstance->pData );
180:
181: // error creating file
182: return( _FALSE );
183: }
184:
185: // write data back out
186: write( hFile, sInstance->pData, sInstance->wSize );
187:
188: // close file
189: close( hFile );
190: }
191:
192: // free memory
193: free( sInstance->pData );
194:
195: // return success
196: return( _TRUE );
197: }
198:
199:
200: /****************************************************************************
201: *
202: * Syntax
203: *
204: * BOOL cdecl hmiINILocateSection( _INI_INSTANCE * sInstance, PSTR szName )
205: *
206: * Description
207: *
208: * locates a section in a file. a section is determined by enclosing
209: * it in []. ie. [SECTION]
210: *
211: * Parameters
212: *
213: * Type Description
214: * --------------------------
215: * sInstance pointer to .ini instance
216: * szName pointer to section name
217: *
218: * Return
219: *
220: * _TRUE section located
221: * _FALSE section not located
222: *
223: ****************************************************************************/
224: BOOL cdecl hmiINILocateSection( _INI_INSTANCE * sInstance, PSTR szName )
225: {
226: PSTR pDataPtr;
227: PSTR pSectionPtr;
228: PSTR szSection;
229: WORD wIndex;
230: WORD wFoundFlag = _FALSE;
231:
232: // set data pointer to pointer to start of .ini in memory
233: pDataPtr = sInstance->pData;
234:
235: // initialize index
236: wIndex = 0;
237:
238: // search data until we have found a start section character
239: // and then attempt to match section string. continue to process
240: // entire data set until the end is reached or a match is
241: // found.
242: do
243: {
244: // check if character we are pointing to is a
245: // start section character
246: if ( *pDataPtr == _INI_SECTION_START )
247: {
248: // save pointer to start of section for use by the
249: // delete functions.
250: pSectionPtr = pDataPtr;
251:
252: // advance past the start section character
253: pDataPtr++;
254:
255: // set pointer to section name
256: szSection = szName;
257:
258: // search the string character by character to determine
259: // if we have a match.
260: while( *pDataPtr == *szSection && wIndex < sInstance->wSize )
261: {
262: // advance section pointer
263: szSection++;
264:
265: // advance data pointer
266: pDataPtr++;
267:
268: // advance data index
269: wIndex++;
270: }
271:
272: // determine if we are sitting on a end section
273: // character. if so then we have a complete match
274: // so set the found flag to true.
275: if ( *pDataPtr == _INI_SECTION_END && *szSection == _NULL )
276: {
277: // set found flag
278: wFoundFlag = _TRUE;
279:
280: // move to the next line
281: while( *pDataPtr != _INI_LF )
282: pDataPtr++;
283:
284: // advance past line feed
285: pDataPtr++;
286:
287: // set list pointer for raw name
288: sInstance->pListPtr = pDataPtr;
289:
290: // set current data pointer to new section
291: // location.
292: sInstance->pCurrent = pDataPtr;
293: sInstance->wCurrent = wIndex;
294:
295: // save pointer to start of current section for
296: // use by other functions.
297: sInstance->pSection = pSectionPtr;
298: }
299:
300: }
301:
302: // advance pointer
303: pDataPtr++;
304:
305: // advance index
306: wIndex++;
307: }
308: while( !wFoundFlag && wIndex < sInstance->wSize );
309:
310: // return the status of the found flag, this will indicate
311: // if the desired section was located.
312: return( ( BOOL )wFoundFlag );
313: }
314:
315:
316: /****************************************************************************
317: *
318: * Syntax
319: *
320: * BOOL cdecl hmiINILocateItem( _INI_INSTANCE * sInstance, PSTR szItem )
321: *
322: * Description
323: *
324: * locates an item under a section in an .ini file
325: *
326: * Parameters
327: *
328: * Type Description
329: * --------------------------
330: * sInstance pointer to an .ini instance
331: * szItem pointer to the name of the item
332: *
333: * Return
334: *
335: * _TRUE the item was located
336: * _FALSE the item was not located
337: *
338: ****************************************************************************/
339: BOOL cdecl hmiINILocateItem( _INI_INSTANCE * sInstance, PSTR szItem )
340: {
341: PSTR pDataPtr;
342: PSTR pItemPtr;
343: PSTR szSearch;
344: WORD wIndex;
345: WORD wFoundFlag = _FALSE;
346:
347: // initialize current location pointers
348: pDataPtr = sInstance->pCurrent;
349: wIndex = sInstance->wCurrent;
350:
351: // search each data item until match is found or the start
352: // of a new section is found
353: do
354: {
355: // set up search pointer
356: szSearch = szItem;
357:
358: // check if current character matches first
359: // character of search string
360: if ( *pDataPtr == *szSearch )
361: {
362: // set pointer to start of potential item. this pointer
363: // will be used later to mark the start of the item
364: // string.
365: pItemPtr = pDataPtr;
366:
367: // advance data pointer and search pointer
368: pDataPtr++;
369: szSearch++;
370:
371: // advance search index
372: wIndex++;
373:
374: // search the rest of the string, make sure we do not overrun
375: // the end of file and that the string still matches.
376: while( *pDataPtr == *szSearch && wIndex < sInstance->wSize )
377: {
378: // advance data pointer
379: pDataPtr++;
380:
381: // advance search pointer
382: szSearch++;
383:
384: // advance index
385: wIndex++;
386: }
387:
388: // check if we located the string
389: if ( *szSearch == _NULL )
390: {
391: // skip any white until we locate the '='
392: // sign.
393: while( *pDataPtr != _INI_EQUATE && *pDataPtr != _INI_EOL )
394: {
395: // advance data pointer
396: pDataPtr++;
397:
398: // advance index
399: wIndex++;
400: }
401:
402: // check if we found and equate '=' character, if not
403: // only set the start of line indicator so the string
404: // and decimal routines know there is no value.
405: if ( *pDataPtr == _INI_EQUATE )
406: {
407: // advance data pointer one past the equate
408: pDataPtr++;
409:
410: // advance index
411: wIndex++;
412:
413: // set the pointer to the new item
414: sInstance->pItem = pDataPtr;
415: }
416: else
417: sInstance->pItem = _NULL;
418:
419: // set the start of line item pointer for later use
420: sInstance->pItemPtr = pItemPtr;
421:
422: // reset list pointer to indicate that we
423: // do not have a list yet.
424: sInstance->pList = _NULL;
425:
426: // set the found flag
427: wFoundFlag = _TRUE;
428: }
429: }
430:
431: // advance to next place in data
432: pDataPtr++;
433:
434: // advance index
435: wIndex++;
436: }
437: while( !wFoundFlag && wIndex < sInstance->wSize && *pDataPtr != _INI_SECTION_START );
438:
439: // return found flag status
440: return( ( BOOL )wFoundFlag );
441: }
442:
443:
444: /****************************************************************************
445: *
446: * Syntax
447: *
448: * BOOL cdecl hmiINIGetDecimal( _INI_INSTANCE * sInstance, WORD * wValue )
449: *
450: * Description
451: *
452: * retrieves a decimal value from an item in a .ini file. note that if
453: * the value is in hex (0x....) it will be automatically converted to
454: * decimal.
455: *
456: * Parameters
457: *
458: * Type Description
459: * --------------------------
460: * sInstance pointer to an .ini instance
461: * wValue pointer to a word to store decimal value in
462: *
463: * Return
464: *
465: * _TRUE value was located and converted correctly
466: * _FALSE value was not valid, or item search was not performed
467: *
468: ****************************************************************************/
469: BOOL cdecl hmiINIGetDecimal( _INI_INSTANCE * sInstance, WORD * wValue )
470: {
471: PSTR pDataPtr;
472: WORD wDValue;
473: BYTE bBuffer[ 32 ];
474: WORD wIndex;
475:
476: // initialize pointer to data
477: if ( sInstance->pList )
478: pDataPtr = sInstance->pList;
479: else
480: pDataPtr = sInstance->pItem;
481:
482: // check if it is null
483: if ( pDataPtr == _NULL )
484: return( _FALSE );
485:
486: // skip all white space
487: while( *pDataPtr == _INI_SPACE )
488: pDataPtr++;
489:
490: // check if we are pointing to and EOL
491: if ( *pDataPtr == _INI_EOL )
492: return( _FALSE );
493:
494: // initialize buffer index
495: wIndex = 0;
496:
497: // fetch string for value
498: while( *pDataPtr != _INI_EOL && *pDataPtr != _INI_LIST_SEPERATOR
499: && *pDataPtr != _INI_SPACE )
500: {
501: // save character
502: bBuffer[ wIndex++ ] = *pDataPtr++;
503: }
504:
505: // set null at the end of buffer
506: bBuffer[ wIndex ] = '\0';
507:
508: // check if we have simply reached the end of the
509: // line with no number.
510: if ( wIndex == 0 )
511: return( _FALSE );
512:
513: // skip all white space
514: while( *pDataPtr == _INI_SPACE )
515: pDataPtr++;
516:
517: // check if we have a list of numbers
518: if ( *pDataPtr == _INI_LIST_SEPERATOR )
519: {
520: // set list pointer to one past the current
521: // seperator.
522: sInstance->pList = ++pDataPtr;
523: }
524: else
525: sInstance->pList = pDataPtr;
526:
527: // check if the buffer contains a hex value
528: if ( bBuffer[ 1 ] == _INI_HEX_INDICATOR )
529: {
530: // fetch hex value
531: wDValue = hmiINIHex2Decimal( &bBuffer[ 2 ] );
532: }
533: else
534: {
535: // fetch value
536: wDValue = (WORD)atoi( bBuffer );
537: }
538:
539: // set value
540: *wValue = wDValue;
541:
542: // return status
543: return( _TRUE );
544: }
545:
546:
547: /****************************************************************************
548: *
549: * Syntax
550: *
551: * BOOL cdecl hmiINIGetString( _INI_INSTANCE * sInstance, PSTR pString, WORD wMaxLength )
552: *
553: * Description
554: *
555: * fetches string from .ini file
556: *
557: * Parameters
558: *
559: * Type Description
560: * --------------------------
561: * sInstance pointer to .ini instance
562: * pString pointer to store string
563: * wMaxLength maximum length of string to fetch
564: *
565: * Return
566: *
567: * _TRUE string was returned correctly
568: * _FALSE item search was not performed before calling this function
569: *
570: ****************************************************************************/
571: BOOL cdecl hmiINIGetString( _INI_INSTANCE * sInstance, PSTR pString, WORD wMaxLength )
572: {
573: PSTR pDataPtr;
574: WORD wIndex;
575:
576: // initialize pointer to data
577: if ( sInstance->pList )
578: pDataPtr = sInstance->pList;
579: else
580: pDataPtr = sInstance->pItem;
581:
582: // check if it is null
583: if ( pDataPtr == _NULL )
584: return( _FALSE );
585:
586: // initialize index
587: wIndex = 0;
588:
589: // find start of string, first non-space character
590: while( *pDataPtr == _INI_SPACE )
591: pDataPtr++;
592:
593: // copy string into buffer
594: while( *pDataPtr != _INI_EOL && *pDataPtr != _INI_LIST_SEPERATOR && wIndex < wMaxLength - 1 )
595: pString[ wIndex++ ] = *pDataPtr++;
596:
597: // place a _NULL at the end of the string
598: pString[ wIndex ] = '\0';
599:
600: // if we have reached maximum buffer length, search until
601: // we get to the EOL or list seperator
602: if ( wIndex == wMaxLength - 1 )
603: {
604: // find end / list seperator
605: while( *pDataPtr != _INI_EOL && *pDataPtr != _INI_LIST_SEPERATOR )
606: pDataPtr++;
607: }
608:
609: // check if we have a list of items
610: if ( *pDataPtr == _INI_LIST_SEPERATOR )
611: {
612: // set list pointer to one past the current
613: // seperator.
614: sInstance->pList = ++pDataPtr;
615: }
616: else
617: sInstance->pList = pDataPtr;
618:
619: // return success
620: return( _TRUE );
621: }
622:
623: /****************************************************************************
624: *
625: * Syntax
626: *
627: * BOOL cdecl hmiINIGetRawString( _INI_INSTANCE * sInstance, PSTR pString, WORD wMaxLength )
628: *
629: * Description
630: *
631: * fetches string from .ini file
632: *
633: * Parameters
634: *
635: * Type Description
636: * --------------------------
637: * sInstance pointer to .ini instance
638: * pString pointer to store string
639: * wMaxLength maximum length of string to fetch
640: *
641: * Return
642: *
643: * _TRUE string was returned correctly
644: * _FALSE end of strings to fetch, or no section locate performed
645: *
646: ****************************************************************************/
647: BOOL cdecl hmiINIGetRawString( _INI_INSTANCE * sInstance, PSTR pString, WORD wMaxLength )
648: {
649: PSTR pDataPtr;
650: PSTR pEOFPtr;
651: WORD wIndex;
652:
653: // initialize data pointer
654: pDataPtr = sInstance->pListPtr;
655:
656: // check if it is null
657: if ( pDataPtr == _NULL || *pDataPtr == _INI_SECTION_START ||
658: *pDataPtr == _INI_EOL )
659: return( _FALSE );
660:
661: // determine EOF pointer
662: pEOFPtr = sInstance->pData + sInstance->wSize;
663:
664: // initialize index
665: wIndex = 0;
666:
667: // find start of string, first non-space character
668: while( *pDataPtr == _INI_SPACE )
669: pDataPtr++;
670:
671: // copy string into buffer
672: while( *pDataPtr != _INI_EOL && wIndex < wMaxLength - 1 )
673: pString[ wIndex++ ] = *pDataPtr++;
674:
675: // place a _NULL at the end of the string
676: pString[ wIndex ] = '\0';
677:
678: // skip past end of line
679: pDataPtr += 2;
680:
681: // make sure we are not at the end of the
682: // file.
683: if ( pDataPtr >= pEOFPtr )
684: sInstance->pListPtr = _NULL;
685: else
686: // save off current position
687: sInstance->pListPtr = pDataPtr;
688:
689: // return success
690: return( _TRUE );
691: }
692:
693: /****************************************************************************
694: *
695: * Syntax
696: *
697: * BOOL cdecl hmiINIWriteDecimal( _INI_INSTANCE * sInstance, WORD wValue )
698: *
699: * Description
700: *
701: * writes decimal value to .ini item
702: *
703: * Parameters
704: *
705: * Type Description
706: * --------------------------
707: * sInstance pointer to an .ini instance
708: * wValue value to write out
709: *
710: * Return
711: *
712: * _TRUE value written correctly
713: * _FALSE value not written correctly
714: *
715: ****************************************************************************/
716: BOOL cdecl hmiINIWriteDecimal( _INI_INSTANCE * sInstance, WORD wValue )
717: {
718: PSTR pDataPtr;
719: PSTR pValuePtr;
720: BYTE bBuffer[ 32 ];
721: WORD wIndex;
722: WORD wStringSize;
723: WORD wDecimalSize;
724: WORD wMoveSize;
725:
726: // check if item was previously located
727: if ( sInstance->pItem == _NULL )
728: return( _FALSE );
729:
730: // get pointer to item
731: pDataPtr = sInstance->pItem;
732:
733: // skip all white space
734: while( *pDataPtr == _INI_SPACE )
735: pDataPtr++;
736:
737: // save pointer to value location
738: pValuePtr = pDataPtr;
739:
740: // initialize string length
741: wStringSize = 0;
742:
743: // fetch string for value
744: while( *pDataPtr != _INI_EOL )
745: {
746: // save character and advance string size
747: bBuffer[ wStringSize++ ] = *pDataPtr++;
748: }
749:
750: // set null at the end of buffer
751: bBuffer[ wStringSize ] = '\0';
752:
753: // check if the buffer contains a hex value
754: if ( bBuffer[ 1 ] == _INI_HEX_INDICATOR )
755: {
756: // convert value to hex string
757: itoa( wValue, &bBuffer[ 2 ], 16 );
758: }
759: else
760: {
761: // convert value to decimal string
762: itoa( wValue, &bBuffer[ 0 ], 10 );
763: }
764:
765: // get length of converted string
766: wDecimalSize = strlen( bBuffer );
767:
768: // check if we need to shrink or expand the
769: // data size.
770: if ( wDecimalSize < wStringSize )
771: {
772: // calculate the move size
773: wMoveSize = ( ( sInstance->pData + sInstance->wSize ) - pValuePtr ) - ( wStringSize - wDecimalSize );
774:
775: // need to shrink the size of the .ini file
776: memmove( pValuePtr, pValuePtr + ( wStringSize - wDecimalSize ), wMoveSize );
777:
778: // adjust size
779: sInstance->wSize -= ( wStringSize - wDecimalSize );
780: }
781: else
782: // check if expand data
783: if ( wDecimalSize > wStringSize )
784: {
785: // make sure we have enough memory to expand
786: if ( sInstance->wSize + ( wDecimalSize - wStringSize ) > sInstance->wMaxSize )
787: return( _FALSE );
788:
789: // need to expand the size of the .ini file,
790: // calculate the move size
791: wMoveSize = ( ( sInstance->pData + sInstance->wSize ) - pValuePtr ) + ( wDecimalSize - wStringSize );
792:
793: // need to shrink the size of the .ini file
794: memmove( pValuePtr + ( wDecimalSize - wStringSize ), pValuePtr, wMoveSize );
795:
796: // adjust size
797: sInstance->wSize += ( wDecimalSize - wStringSize );
798: }
799:
800: // initialize index
801: wIndex = 0;
802:
803: // copy in new string
804: while( bBuffer[ wIndex ] )
805: *pValuePtr++ = bBuffer[ wIndex++ ];
806:
807: // set the modified flag
808: sInstance->wFlags |= _INI_MODIFIED;
809:
810: // return success
811: return( _TRUE );
812: }
813:
814: /****************************************************************************
815: *
816: * Syntax
817: *
818: * BOOL cdecl hmiINIWriteString( _INI_INSTANCE * sInstance, PSTR szString )
819: *
820: * Description
821: *
822: * writes out string to item
823: *
824: * Parameters
825: *
826: * Type Description
827: * --------------------------
828: * sInstance pointer to an .ini instance
829: * szString null terminated string
830: *
831: * Return
832: *
833: * _TRUE the string was written correctly
834: * _FALSE the string was not written correctly
835: *
836: ****************************************************************************/
837: BOOL cdecl hmiINIWriteString( _INI_INSTANCE * sInstance, PSTR szString )
838: {
839: PSTR pDataPtr;
840: PSTR pStringPtr;
841: WORD wMoveSize;
842: WORD wSourceSize;
843: WORD wDestSize;
844:
845: // initialize data pointer
846: pDataPtr = sInstance->pItem;
847:
848: // check if it is null
849: if ( pDataPtr == _NULL )
850: return( _FALSE );
851:
852: // initialize destination length
853: wDestSize = 0;
854:
855: // find start of string character, first non-space
856: // character.
857: while( *pDataPtr == _INI_SPACE )
858: pDataPtr++;
859:
860: // save place to store new string
861: pStringPtr = pDataPtr;
862:
863: // copy string into buffer
864: while( *pDataPtr++ != _INI_EOL )
865: wDestSize++;
866:
867: // get string length of new string
868: wSourceSize = strlen( szString );
869:
870: // check if we need to shrink or expand the
871: // data size.
872: if ( wSourceSize < wDestSize )
873: {
874: // calculate the move size
875: wMoveSize = ( ( sInstance->pData + sInstance->wSize ) - pStringPtr ) - ( wDestSize - wSourceSize );
876:
877: // need to shrink the size of the .ini file
878: memmove( pStringPtr, pStringPtr + ( wDestSize - wSourceSize ), wMoveSize );
879:
880: // adjust size
881: sInstance->wSize -= ( wDestSize - wSourceSize );
882: }
883: else
884: // check if expand data
885: if ( wSourceSize > wDestSize )
886: {
887: // make sure we have enough memory to expand
888: if ( sInstance->wSize + ( wSourceSize - wDestSize ) > sInstance->wMaxSize )
889: return( _FALSE );
890:
891: // need to expand the size of the .ini file,
892: // calculate the move size
893: wMoveSize = ( ( sInstance->pData + sInstance->wSize ) - pStringPtr ) + ( wSourceSize - wDestSize );
894:
895: // need to shrink the size of the .ini file
896: memmove( pStringPtr + ( wSourceSize - wDestSize ), pStringPtr, wMoveSize );
897:
898: // adjust size
899: sInstance->wSize += ( wSourceSize - wDestSize );
900: }
901:
902: // copy in new string
903: while( *szString )
904: *pStringPtr++ = *szString++;
905:
906: // set the modified flag
907: sInstance->wFlags |= _INI_MODIFIED;
908:
909: // return success
910: return( _TRUE );
911: }
912:
913: /****************************************************************************
914: *
915: * Syntax
916: *
917: * BOOL cdecl hmiINIGetQuery( _INI_INSTANCE * sInstance, PSTR szItem )
918: *
919: * Description
920: *
921: * get a Yes/No type answer from item
922: *
923: * Parameters
924: *
925: * Type Description
926: * --------------------------
927: * sInstance pointer to an .ini instance
928: * szItem pointer to item to locate in query
929: *
930: * Return
931: *
932: * _TRUE "Yes" was found
933: * _FALSE "No" or other was found
934: *
935: ****************************************************************************/
936: BOOL cdecl hmiINIGetQuery( _INI_INSTANCE * sInstance, PSTR szItem )
937: {
938: BYTE bBuffer[ 32 ];
939:
940: // locate item within section
941: if ( !hmiINILocateItem( sInstance, szItem ) )
942: return( _FALSE );
943:
944: // get string from .ini file
945: if ( !hmiINIGetString( sInstance, bBuffer, 32 ) )
946: return( _FALSE );
947:
948: // compare string to "Yes"
949: if ( strcmpi( bBuffer, "YES" ) == 0 )
950: return( _TRUE );
951: else
952: return( _FALSE );
953: }
954:
955: /****************************************************************************
956: *
957: * Syntax
958: *
959: * BOOL cdecl hmiINIGetItemDecimal( _INI_INSTANCE * sInstance, PSTR szItem,
960: * WORD * wValue )
961: *
962: * Description
963: *
964: * locates and item and returns the decimal value associated with it
965: *
966: * Parameters
967: *
968: * Type Description
969: * --------------------------
970: * sInstance pointer to .ini instance
971: * szItem pointer to item string
972: * wValue pointer to word value
973: *
974: * Return
975: *
976: * _TRUE value located and retrieved
977: * _FALSE error getting value
978: *
979: ****************************************************************************/
980: BOOL cdecl hmiINIGetItemDecimal( _INI_INSTANCE * sInstance, PSTR szItem,
981: WORD * wValue )
982: {
983: // attempt to locate item
984: if ( !hmiINILocateItem( sInstance, szItem ) )
985: return( _FALSE );
986:
987: // get decimal value
988: if ( !hmiINIGetDecimal( sInstance, wValue ) )
989: return( _FALSE );
990:
991: // return success
992: return( _TRUE );
993: }
994:
995: /****************************************************************************
996: *
997: * Syntax
998: *
999: * BOOL cdecl hmiINIGetItemString( _INI_INSTANCE * sInstance, PSTR szItem,
1000: * PSTR pString, WORD wMaxSize )
1001: *
1002: * Description
1003: *
1004: * locates item and retrieves string associated with it
1005: *
1006: * Parameters
1007: *
1008: * Type Description
1009: * --------------------------
1010: * sInstance pointer to an .ini instance
1011: * szItem pointer to item string
1012: * pString pointer to string data area
1013: * wMaxSize maximum size of the string to get
1014: *
1015: * Return
1016: *
1017: * _TRUE string retrieved
1018: * _FALSE error retrieving string
1019: *
1020: ****************************************************************************/
1021: BOOL cdecl hmiINIGetItemString( _INI_INSTANCE * sInstance, PSTR szItem,
1022: PSTR pString, WORD wMaxSize )
1023: {
1024: // attempt to locate the item string
1025: if ( !hmiINILocateItem( sInstance, szItem ) )
1026: return( _FALSE );
1027:
1028: // attempt to get string
1029: if ( !hmiINIGetString( sInstance, pString, wMaxSize ) )
1030: return( _FALSE );
1031:
1032: // return success
1033: return( _TRUE );
1034: }
1035:
1036: /****************************************************************************
1037: *
1038: * Syntax
1039: *
1040: * BOOL cdecl hmiINIWriteQuery( _INI_INSTANCE * sInstance, PSTR szItem, BOOL wState )
1041: *
1042: * Description
1043: *
1044: * write a Yes/No type answer to an item
1045: *
1046: * Parameters
1047: *
1048: * Type Description
1049: * --------------------------
1050: * sInstance pointer to an .ini instance
1051: * szItem pointer to item to alter
1052: * wState flag: _TRUE or _FALSE
1053: *
1054: * Return
1055: *
1056: * _TRUE query was modified
1057: * _FALSE query was not modified
1058: *
1059: ****************************************************************************/
1060: BOOL cdecl hmiINIWriteQuery( _INI_INSTANCE * sInstance, PSTR szItem, BOOL wState )
1061: {
1062: // locate item within section
1063: if ( !hmiINILocateItem( sInstance, szItem ) )
1064: return( _FALSE );
1065:
1066: // write string to .ini file
1067: if ( wState )
1068: {
1069: // write string
1070: if ( !hmiINIWriteString( sInstance, "Yes" ) )
1071: return( _FALSE );
1072: }
1073: else
1074: {
1075: // write string
1076: if ( !hmiINIWriteString( sInstance, "No" ) )
1077: return( _FALSE );
1078: }
1079:
1080: // return success
1081: return( _TRUE );
1082: }
1083:
1084: /****************************************************************************
1085: *
1086: * Syntax
1087: *
1088: * BOOL cdecl hmiINIDeleteItem( _INI_INSTANCE * sInstance, PSTR szItem )
1089: *
1090: * Description
1091: *
1092: * removes and item from within a section
1093: *
1094: * Parameters
1095: *
1096: * Type Description
1097: * --------------------------
1098: * sInstance pointer to and .ini instance
1099: * szItem pointer to item string to remove
1100: *
1101: * Return
1102: *
1103: * _TRUE item was found and removed
1104: * _FALSE item was not located
1105: *
1106: ****************************************************************************/
1107: BOOL cdecl hmiINIDeleteItem( _INI_INSTANCE * sInstance, PSTR szItem )
1108: {
1109: PSTR pDataPtr;
1110: WORD wStrSize = 0;
1111:
1112: // locate item within section
1113: if ( !hmiINILocateItem( sInstance, szItem ) )
1114: return( _FALSE );
1115:
1116: // get pointer to data
1117: pDataPtr = sInstance->pItemPtr;
1118:
1119: // now find the length of the string to delete
1120: while( *(pDataPtr + wStrSize ) != _INI_LF )
1121: wStrSize++;
1122:
1123: // add one to size to include line feed
1124: wStrSize++;
1125:
1126: // perform memory move to delete string
1127: memmove( pDataPtr, pDataPtr + wStrSize, ( sInstance->pData + sInstance->wSize ) - ( pDataPtr + wStrSize ) );
1128:
1129: // adjust size of .ini file
1130: sInstance->wSize -= wStrSize;
1131:
1132: // set the modified flag
1133: sInstance->wFlags |= _INI_MODIFIED;
1134:
1135: // return success
1136: return( _TRUE );
1137: }
1138:
1139: /****************************************************************************
1140: *
1141: * Syntax
1142: *
1143: * BOOL cdecl hmiINIDeleteSection( _INI_INSTANCE * sInstance, PSTR szSection )
1144: *
1145: * Description
1146: *
1147: * removes and item from within a section
1148: *
1149: * Parameters
1150: *
1151: * Type Description
1152: * --------------------------
1153: * sInstance pointer to and .ini instance
1154: * szSection pointer to section to remove
1155: *
1156: * Return
1157: *
1158: * _TRUE section was found and removed
1159: * _FALSE section was not located
1160: *
1161: ****************************************************************************/
1162: BOOL cdecl hmiINIDeleteSection( _INI_INSTANCE * sInstance, PSTR szSection )
1163: {
1164: PSTR pDataPtr;
1165: PSTR pEOFPtr;
1166: WORD wSize;
1167:
1168: // locate item within section
1169: if ( !hmiINILocateSection( sInstance, szSection ) )
1170: return( _FALSE );
1171:
1172: // get pointer to data
1173: pDataPtr = sInstance->pSection;
1174:
1175: // set end of file pointer
1176: pEOFPtr = sInstance->pData + sInstance->wSize;
1177:
1178: // advance szie one past section start to find the start of either the
1179: // next section or the end of file.
1180: wSize = 1;
1181:
1182: // now find the length of the string to delete
1183: while( *(pDataPtr + wSize ) != _INI_SECTION_START && ( pDataPtr + wSize ) < pEOFPtr )
1184: wSize++;
1185:
1186: // perform memory move to delete string
1187: memmove( pDataPtr, pDataPtr + wSize, pEOFPtr - ( pDataPtr + wSize ) );
1188:
1189: // adjust size of .ini file
1190: sInstance->wSize -= wSize;
1191:
1192: // set the modified flag
1193: sInstance->wFlags |= _INI_MODIFIED;
1194:
1195: // return success
1196: return( _TRUE );
1197: }
1198:
1199: /****************************************************************************
1200: *
1201: * Syntax
1202: *
1203: * BOOL cdecl hmiINIAddSection( _INI_INSTANCE * sInstance, PSTR szSection )
1204: *
1205: * Description
1206: *
1207: * adds new section [...] to end of current file
1208: *
1209: * Parameters
1210: *
1211: * Type Description
1212: * --------------------------
1213: * sInstance pointer to an .ini instance
1214: * szSection pointer to name of section to add
1215: *
1216: * Return
1217: *
1218: * _TRUE section added to end of file
1219: * _FALSE not enough memory to add new section or section exists
1220: *
1221: ****************************************************************************/
1222: BOOL cdecl hmiINIAddSection( _INI_INSTANCE * sInstance, PSTR szSection )
1223: {
1224: PSTR pDataPtr;
1225: WORD wSize;
1226:
1227: // check if section exists
1228: if ( hmiINILocateSection( sInstance, szSection ) )
1229: return( _FALSE );
1230:
1231: // get pointer to end of file
1232: pDataPtr = sInstance->pData + sInstance->wSize;
1233:
1234: // get size of new section name to add, plus
1235: // 8 to account for the CR/LF, start, end, CR/LF
1236: wSize = strlen( szSection ) + 6;
1237:
1238: // check if there is room to add section
1239: if ( sInstance->wSize + wSize > sInstance->wMaxSize )
1240: return( _FALSE );
1241:
1242: // add CR/LF @ the end of the file
1243: *pDataPtr++ = _INI_CR;
1244: *pDataPtr++ = _INI_LF;
1245:
1246: // set current section pointer to here
1247: sInstance->pSection = pDataPtr;
1248:
1249: // copy in string start
1250: *pDataPtr++ = _INI_SECTION_START;
1251:
1252: // copy in string
1253: while( *szSection )
1254: *pDataPtr++ = *szSection++;
1255:
1256: // copy in string end
1257: *pDataPtr++ = _INI_SECTION_END;
1258:
1259: // add CR/LF @ the end of the file
1260: *pDataPtr++ = _INI_CR;
1261: *pDataPtr++ = _INI_LF;
1262:
1263: // set section list pointer to here
1264: sInstance->pListPtr = pDataPtr;
1265:
1266: // set current pointer to here
1267: sInstance->pCurrent = pDataPtr;
1268:
1269: // adjust size
1270: sInstance->wSize += wSize;
1271:
1272: // set the modified flag
1273: sInstance->wFlags |= _INI_MODIFIED;
1274:
1275: // return success
1276: return( _TRUE );
1277:
1278: }
1279:
1280: /****************************************************************************
1281: *
1282: * Syntax
1283: *
1284: * BOOL cdecl hmiINIAddItemString( _INI_INSTANCE * sInstance, PSTR szItem,
1285: * PSTR szString, WORD wJustify )
1286: *
1287: * Description
1288: *
1289: * adds new item field w/string
1290: *
1291: * Parameters
1292: *
1293: * Type Description
1294: * --------------------------
1295: * sInstance pointer to an .ini instance
1296: * szItem pointer to item string
1297: * szString pointer to string
1298: * wJustify width of field
1299: *
1300: * Return
1301: *
1302: * _TRUE item and string added
1303: * _FALSE item not added, not enough memory, etc..
1304: *
1305: ****************************************************************************/
1306: BOOL cdecl hmiINIAddItemString( _INI_INSTANCE * sInstance, PSTR szItem, PSTR szString, WORD wJustify )
1307: {
1308: PSTR pDataPtr;
1309: WORD wSize;
1310:
1311: // check if item exists
1312: if ( hmiINILocateItem( sInstance, szItem ) )
1313: {
1314: // alter string
1315: hmiINIWriteString( sInstance, szString );
1316:
1317: // return success
1318: return( _TRUE );
1319: }
1320:
1321: // get data pointer
1322: pDataPtr = sInstance->pCurrent;
1323:
1324: // adjust size to account for item, =, SPACE, STRING, CR/LF
1325: wSize = wJustify + 4 + strlen( szString );
1326:
1327: // check if there is room to add item/string
1328: if ( sInstance->wSize + wSize > sInstance->wMaxSize )
1329: return( _FALSE );
1330:
1331: // make room in memory
1332: memmove( pDataPtr + wSize, pDataPtr, ( sInstance->pData + sInstance->wSize ) - pDataPtr );
1333:
1334: // copy in new item string
1335: while( *szItem )
1336: {
1337: // copy in character into .ini
1338: *pDataPtr++ = *szItem++;
1339:
1340: // decrement justify characters
1341: wJustify--;
1342: }
1343:
1344: // fill in the rest of the white space to
1345: // justify item
1346: while( wJustify-- )
1347: *pDataPtr++ = _INI_SPACE;
1348:
1349: // fill in the = sign and an extra space
1350: *pDataPtr++ = _INI_EQUATE;
1351: *pDataPtr++ = _INI_SPACE;
1352:
1353: // copy in the string
1354: while( *szString )
1355: *pDataPtr++ = *szString++;
1356:
1357: // add in CR/LF
1358: *pDataPtr++ = _INI_CR;
1359: *pDataPtr++ = _INI_LF;
1360:
1361: // adjust size
1362: sInstance->wSize += wSize;
1363:
1364: // set the modified flag
1365: sInstance->wFlags |= _INI_MODIFIED;
1366:
1367: // return success
1368: return( _TRUE );
1369:
1370: }
1371:
1372: /****************************************************************************
1373: *
1374: * Syntax
1375: *
1376: * BOOL cdecl hmiINIAddItemDecimal( _INI_INSTANCE * sInstance, PSTR szItem,
1377: * WORD wValue, WORD wJustify, WORD wRadix )
1378: *
1379: * Description
1380: *
1381: * adds new item field w/decimal
1382: *
1383: * Parameters
1384: *
1385: * Type Description
1386: * --------------------------
1387: * sInstance pointer to an .ini instance
1388: * szItem pointer to item string
1389: * wValue value to write out
1390: * wJustify width of field
1391: * wRadix radix (base) of field
1392: *
1393: * Return
1394: *
1395: * _TRUE item and string added
1396: * _FALSE item not added, not enough memory, etc..
1397: *
1398: ****************************************************************************/
1399: BOOL cdecl hmiINIAddItemDecimal( _INI_INSTANCE * sInstance,
1400: PSTR szItem,
1401: WORD wValue,
1402: WORD wJustify,
1403: WORD wRadix )
1404: {
1405: PSTR pDataPtr;
1406: WORD wSize;
1407: WORD wIndex;
1408: BYTE szBuffer[ 32 ];
1409:
1410: // check if item exists
1411: if ( hmiINILocateItem( sInstance, szItem ) )
1412: {
1413: // alter string
1414: hmiINIWriteDecimal( sInstance, wValue );
1415:
1416: // return success
1417: return( _TRUE );
1418: }
1419:
1420: // convert decimal to string
1421: if ( wRadix == 16 )
1422: {
1423: // add hex '0x' preceeding value
1424: szBuffer[ 0 ] = '0';
1425: szBuffer[ 1 ] = 'x';
1426:
1427: // convert to hex
1428: itoa( wValue, (PSTR)&szBuffer[ 2 ], 16 );
1429: }
1430: else
1431: itoa( wValue, (PSTR)&szBuffer[ 0 ], 10 );
1432:
1433: // get data pointer
1434: pDataPtr = sInstance->pCurrent;
1435:
1436: // adjust size to account for item, =, SPACE, STRING, CR/LF
1437: wSize = wJustify + 4 + strlen( szBuffer );
1438:
1439: // check if there is room to add item/string
1440: if ( sInstance->wSize + wSize > sInstance->wMaxSize )
1441: return( _FALSE );
1442:
1443: // make room in memory
1444: memmove( pDataPtr + wSize, pDataPtr, ( sInstance->pData + sInstance->wSize ) - pDataPtr );
1445:
1446: // copy in new item string
1447: while( *szItem )
1448: {
1449: // copy in character into .ini
1450: *pDataPtr++ = *szItem++;
1451:
1452: // decrement justify characters
1453: wJustify--;
1454: }
1455:
1456: // fill in the rest of the white space to
1457: // justify item
1458: while( wJustify-- )
1459: *pDataPtr++ = _INI_SPACE;
1460:
1461: // fill in the = sign and an extra space
1462: *pDataPtr++ = _INI_EQUATE;
1463: *pDataPtr++ = _INI_SPACE;
1464:
1465: // reset index
1466: wIndex = 0;
1467:
1468: // copy in the string
1469: while( szBuffer[ wIndex ] )
1470: *pDataPtr++ = szBuffer[ wIndex++ ];
1471:
1472: // add in CR/LF
1473: *pDataPtr++ = _INI_CR;
1474: *pDataPtr++ = _INI_LF;
1475:
1476: // adjust size
1477: sInstance->wSize += wSize;
1478:
1479: // set the modified flag
1480: sInstance->wFlags |= _INI_MODIFIED;
1481:
1482: // return success
1483: return( _TRUE );
1484:
1485: }
1486:
1487: /****************************************************************************
1488: *
1489: * Syntax
1490: *
1491: * WORD hmiINIHex2Decimal( PSTR szHexValue )
1492: *
1493: * Description
1494: *
1495: * Converts passed number to hex
1496: *
1497: * Parameters
1498: *
1499: * Type Description
1500: * --------------------------
1501: * szHexValue pointer to the string containing hex value
1502: *
1503: * Return
1504: *
1505: * decimal value of hex number
1506: *
1507: ****************************************************************************/
1508: WORD hmiINIHex2Decimal( PSTR szHexValue )
1509: {
1510: WORD wDecimal = 0;
1511: WORD wPlaces = strlen( szHexValue );
1512: WORD wMultIndex;
1513: WORD wIndex = 0;
1514:
1515: // count down
1516: do
1517: {
1518: // accumulate value
1519: wDecimal += wMultiplier[ wPlaces - 1 ] * hmiINIGetHexIndex( (BYTE)szHexValue[ wIndex++ ] );
1520:
1521: // decrement places
1522: wPlaces--;
1523: }
1524: while( wPlaces > 0 );
1525:
1526: // return decimal
1527: return( wDecimal );
1528: }
1529:
1530: /****************************************************************************
1531: *
1532: * Syntax
1533: *
1534: * WORD hmiINIGetHexIndex( BYTE bValue )
1535: *
1536: * Description
1537: *
1538: * Get the index of a hex character
1539: *
1540: * Parameters
1541: *
1542: * Type Description
1543: * --------------------------
1544: * bValue value to located
1545: *
1546: * Return
1547: *
1548: * index into table of hex value
1549: *
1550: ****************************************************************************/
1551: WORD hmiINIGetHexIndex( BYTE bValue )
1552: {
1553: WORD wIndex;
1554:
1555: // search
1556: for ( wIndex = 0; wIndex < 16; wIndex++ )
1557: if ( szHexNumbers[ wIndex ] == toupper( bValue ) )
1558: return( wIndex );
1559:
1560: // error return
1561: return( -1 );
1562: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.