STRSTRING 22-SEP-1995 18:31:16 VAX C V3.2-044 Page 1 V1.0 16-MAR-1994 23:52:50 [GOPHER.G2.VMS2_13.OBJECT]STRSTRING.C;1 (1) 1 /******************************************************************** 2 * lindner 3 * 3.9 4 * 1994/03/17 05:52:49 5 * /home/arcwelder/GopherSrc/CVS/gopher+/object/STRstring.c,v 6 * $Status: $ 7 * 8 * Paul Lindner, University of Minnesota CIS. 9 * 10 * Copyright 1991, 1992 by the Regents of the University of Minnesota 11 * see the file "Copyright" in the distribution for conditions of use. 12 ********************************************************************* 13 * MODULE: STRstring.c 14 * Implement dynamic string library functions 15 ********************************************************************* 16 * Revision History: 17 * STRstring.c,v 18 * Revision 3.9 1994/03/17 05:52:49 lindner 19 * Fix for multiple STRinits 20 * 21 * Revision 3.8 1994/03/04 17:31:39 lindner 22 * Don't assume a char is one byte long 23 * 24 * Revision 3.7 1994/02/20 16:23:13 lindner 25 * Optimize STRinit so that memory isn't freed then reallocated 26 * 27 * Revision 3.6 1993/10/27 18:53:23 lindner 28 * don't forget \0 29 * 30 * Revision 3.5 1993/10/22 20:15:52 lindner 31 * Remove superfulous declaration of len (Fote) 32 * 33 * Revision 3.4 1993/10/19 20:46:00 lindner 34 * Better, tighter STRstring stuff (Fote) 35 * 36 * Revision 3.3 1993/08/16 19:35:09 lindner 37 * Return a correct value for STRcpy 38 * 39 * Revision 3.2 1993/03/24 17:07:52 lindner 40 * STRset with a NULL value will STRinit() the string 41 * 42 * Revision 3.1.1.1 1993/02/11 18:03:03 lindner 43 * Gopher+1.2beta release 44 * 45 * Revision 1.1 1992/12/10 23:27:52 lindner 46 * gopher 1.1 release 47 * 48 * 49 *********************************************************************/ 50 51 52 #include "STRstring.h" 131 #include "String.h" 1013 #include "Malloc.h" 1671 STRSTRING 22-SEP-1995 18:31:16 VAX C V3.2-044 Page 2 V1.0 16-MAR-1994 23:52:50 [GOPHER.G2.VMS2_13.OBJECT]STRSTRING.C;1 (1) 1672 /* 1673 * Make a new string, don't set anything for default yet. 1674 */ 1675 1676 String * 1677 STRnew() 1678 { 1679 1 String *temp; 1680 1 1681 1 temp = (String *) malloc(sizeof(String)); 1682 1 temp->data = NULL; 1683 1 temp->len = 0; 1684 1 1685 1 return(temp); 1686 1 } 1687 1688 /* 1689 * Destroy a string 1690 */ 1691 1692 void 1693 STRdestroy(st) 1694 String *st; 1695 { 1696 1 if (st != NULL) { 1697 2 if (st->data != NULL) 1698 2 free(st->data); 1699 2 free(st); 1700 2 } else 1701 1 perror("STRdestroy: non existant string!\n"); 1702 1 1703 1 } 1704 1705 1706 /* 1707 * Don't free the memory. Instead, negate the length value and keep the 1708 * memory for later use. More efficient that way. 1709 */ 1710 1711 void 1712 STRinit(st) 1713 String *st; 1714 { 1715 1 if (st != NULL) { 1716 2 if (st->len > 0) 1717 2 st->len = - st->len; 1718 2 /* if (st->data != NULL) { 1719 2 free(st->data); 1720 2 st->data = NULL; 1721 2 }*/ 1722 2 } else 1723 1 perror("STRinit, non existant string!"); 1724 1 } 1725 1726 /* 1727 * Set a string value 1728 */ STRSTRING 22-SEP-1995 18:31:16 VAX C V3.2-044 Page 3 V1.0 16-MAR-1994 23:52:50 [GOPHER.G2.VMS2_13.OBJECT]STRSTRING.C;1 (1) 1729 1730 void 1731 STRset(st, str) 1732 String *st; 1733 char *str; 1734 { 1735 1 register int len; 1736 1 1737 1 /* To set a null value, STRinit the item */ 1738 1 1739 1 if (str == NULL) { 1740 2 STRinit(st); 1741 2 return; 1742 2 } 1743 1 1744 1 /* Negative value for len means memory is initialized, reset to 1745 1 positive value so the rest of the code works well. 1746 1 */ 1747 1 1748 1 if (st->len < 0) 1749 1 st->len = -st->len; 1750 1 1751 1 if (*str == '\0') 1752 1 len = 1; 1753 1 else 1754 1 len = strlen(str)+1; /** Don't forget the '\0' **/ 1755 1 1756 1 /* Uninitialized data... */ 1757 1 1758 1 if (st->data == NULL) { 1759 2 st->data = (char *) malloc(sizeof(char*) * len); 1760 2 st->len = len; 1761 2 } 1762 1 1763 1 /** Something's already there... **/ 1764 1 1765 1 else if (STRsize(st) < len) { 1766 2 char *temp; 1767 2 1768 2 temp = (char *) realloc(st->data, sizeof(char) * len); 1769 2 /*** Should check for NULL ... ***/ 1770 2 if (temp == NULL) 1771 2 perror("realloc failed..."); 1772 2 1773 2 st->data = temp; 1774 2 st->len = len; 1775 2 } 1776 1 /* space is ok and st->len is set, so copy in the new string */ 1777 1 strcpy(st->data, str); 1778 1 } 1779 1780 /* 1781 * Add a string to the end of the string that's there already 1782 */ 1783 1784 String* 1785 STRcat(st, cp) STRSTRING 22-SEP-1995 18:31:16 VAX C V3.2-044 Page 4 V1.0 16-MAR-1994 23:52:50 [GOPHER.G2.VMS2_13.OBJECT]STRSTRING.C;1 (1) 1786 String *st; 1787 char *cp; 1788 { 1789 1 int len; 1790 1 char *temp; 1791 1 1792 1 if (cp == NULL) 1793 1 return(NULL); 1794 1 1795 1 if (STRlen(st) == 0) { 1796 2 STRset(st, cp); 1797 2 return(st); 1798 2 } 1799 1 1800 1 len = strlen(cp) + STRlen(st); 1801 1 1802 1 temp = (char *) malloc(sizeof(char) * len); 1803 1 strcpy(temp, STRget(st)); 1804 1 strcat(temp, cp); 1805 1 1806 1 STRset(st, temp); 1807 1 1808 1 free(temp); 1809 1 1810 1 return(st); 1811 1 } 1812 1813 1814 int 1815 STRcmp(st1, st2) 1816 String *st1; 1817 String *st2; 1818 { 1819 1 register char *cp1, *cp2; 1820 1 1821 1 cp1 = STRget(st1); 1822 1 cp2 = STRget(st2); 1823 1 1824 1 if (cp1 == NULL) 1825 1 return(- !0); 1826 1 else if (cp2 == NULL) 1827 1 return( !0); 1828 1 else 1829 1 return(strcmp(cp1, cp2)); 1830 1 } 1831 1832 String* 1833 STRcpy(s1, s2) 1834 String *s1; 1835 String *s2; 1836 { 1837 1 STRset(s1, STRget(s2)); 1838 1 return(s1); 1839 1 } STRSTRING 22-SEP-1995 18:31:16 VAX C V3.2-044 Page 5 V1.0 16-MAR-1994 23:52:50 [GOPHER.G2.VMS2_13.OBJECT]STRSTRING.C;1 (1) Command Line ------------ CC/INCL=([-],[-.OBJECT])/DEFINE=(MULTINET=1,DEBUGGING,__VMS)/DEBUG/NOOPT/OBJ=[.VAX.DBG]/LIS=[.VAX.LIS] STRSTRING.C .