Changeset 3121 for trunk/tools/fastdep
- Timestamp:
- Mar 15, 2000, 6:02:29 PM (26 years ago)
- File:
-
- 1 edited
-
trunk/tools/fastdep/fastdep.c (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/fastdep/fastdep.c
r3120 r3121 1 /* $Id: fastdep.c,v 1. 5 2000-03-15 15:02:11bird Exp $1 /* $Id: fastdep.c,v 1.6 2000-03-15 17:02:29 bird Exp $ 2 2 * 3 3 * Fast dependents. (Fast = Quick and Dirty!) … … 105 105 106 106 107 /** 108 * Dependant Rule 109 */ 110 typedef struct _DepRule 111 { 112 char * pszRule; /* Pointer to rule name */ 113 int cDeps; /* Entries in the dependant array. */ 114 char ** papszDep; /* Pointer to an array of pointers to dependants. */ 115 struct _DepRule *pNext; /* Pointer to the next rule */ 116 } DEPRULE, *PDEPRULE; 117 118 107 119 /******************************************************************************* 108 120 * Internal Functions * … … 136 148 #endif 137 149 150 /* file helpers */ 151 static signed long fsize(FILE *phFile); 152 153 /* text helpers */ 154 static char *trim(char *psz); 155 static char *trimR(char *psz); 156 157 /* textbuffer */ 158 static void *textbufferCreate(const char *pszFilename); 159 static void textbufferDestroy(void *pvBuffer); 160 static char *textbufferNextLine(void *pvBuffer, char *psz); 161 162 /* depend workers */ 163 static BOOL depReadFile(const char *pszFilename); 164 static BOOL depWriteFile(const char *pszFilename); 165 static void *depAddRule(const char *pszRule); 166 static BOOL depAddDepend(void *pvRule, const char *pszDep); 167 168 static BOOL depCleanFile(const char *pszFilename); 169 170 138 171 /******************************************************************************* 139 172 * Global Variables * 140 173 *******************************************************************************/ 174 static PDEPRULE pdepList = NULL; 175 141 176 static const char pszDefaultDepFile[] = ".depend"; 142 177 static const char *apszExtC_CPP[] = {"c", "sqc", "cpp", "h", "hpp", NULL}; … … 144 179 static const char *apszExtRC[] = {"rc", "dlg", NULL}; 145 180 static const char *apszExtCOBOL[] = {"cbl", "cob", "sqb", NULL}; 146 147 181 static CONFIGENTRY aConfig[] = 148 182 { … … 1474 1508 #endif 1475 1509 1510 /** 1511 * Find the size of a file. 1512 * @returns Size of file. -1 on error. 1513 * @param phFile File handle. 1514 */ 1515 static signed long fsize(FILE *phFile) 1516 { 1517 int ipos; 1518 signed long cb; 1519 1520 if ((ipos = ftell(phFile)) < 0 1521 || 1522 fseek(phFile, 0, SEEK_END) != 0 1523 || 1524 (cb = ftell(phFile)) < 0 1525 || 1526 fseek(phFile, ipos, SEEK_SET) != 0 1527 ) 1528 cb = -1; 1529 return cb; 1530 } 1531 1532 1533 1534 /** 1535 * Trims a string, ie. removing spaces (and tabs) from both ends of the string. 1536 * @returns Pointer to first not space or tab char in the string. 1537 * @param psz Pointer to the string which is to be trimmed. 1538 * @status completely implmented. 1539 * @author knut st. osmundsen (knut.stange.osmundsen@pmsc.no) 1540 */ 1541 static char *trim(char *psz) 1542 { 1543 int i; 1544 if (psz == NULL) 1545 return NULL; 1546 while (*psz == ' ' || *psz == '\t') 1547 psz++; 1548 i = strlen(psz) - 1; 1549 while (i >= 0 && (psz[i] == ' ' || *psz == '\t')) 1550 i--; 1551 psz[i+1] = '\0'; 1552 return psz; 1553 } 1554 1555 1556 /** 1557 * Right trims a string, ie. removing spaces (and tabs) from the end of the stri 1558 * @returns Pointer to the string passed in. 1559 * @param psz Pointer to the string which is to be right trimmed. 1560 * @status completely implmented. 1561 * @author knut st. osmundsen (knut.stange.osmundsen@pmsc.no) 1562 */ 1563 static char *trimR(char *psz) 1564 { 1565 int i; 1566 if (psz == NULL) 1567 return NULL; 1568 i = strlen(psz) - 1; 1569 while (i >= 0 && (psz[i] == ' ' || *psz == '\t')) 1570 i--; 1571 psz[i+1] = '\0'; 1572 return psz; 1573 } 1574 1575 1576 /** 1577 * Creates a memory buffer for a text file. 1578 * @returns Pointer to file memoryblock. NULL on error. 1579 * @param pszFilename Pointer to filename string. 1580 */ 1581 static void *textbufferCreate(const char *pszFilename) 1582 { 1583 void *pvFile = NULL; 1584 FILE *phFile; 1585 1586 phFile = fopen(pszFilename, "r"); 1587 if (phFile != NULL) 1588 { 1589 signed long cbFile = fsize(phFile); 1590 if (cbFile != -1) 1591 { 1592 pvFile = malloc(cbFile + 1); 1593 if (pvFile != NULL) 1594 { 1595 memset(pvFile, 0, cbFile + 1); 1596 if (fread(pvFile, 1, cbFile, phFile) == 0) 1597 { /* failed! */ 1598 free(pvFile); 1599 pvFile = NULL; 1600 } 1601 } 1602 } 1603 fclose(phFile); 1604 } 1605 return pvFile; 1606 } 1607 1608 1609 /** 1610 * Destroys a text textbuffer. 1611 * @param pvBuffer Buffer handle. 1612 */ 1613 static void textbufferDestroy(void *pvBuffer) 1614 { 1615 free(pvBuffer); 1616 } 1617 1618 1619 /** 1620 * Gets the next line from an textbuffer. 1621 * @returns Pointer to the next line. 1622 * @param pvBuffer Buffer handle. 1623 * @param psz Pointer to current line. 1624 * NULL is passed in to get the first line. 1625 */ 1626 static char *textbufferNextLine(void *pvBuffer, register char *psz) 1627 { 1628 register char ch; 1629 1630 /* if first line psz is NULL. */ 1631 if (psz == NULL) 1632 return (char*)pvBuffer; 1633 1634 /* skip till end of file or end of line. */ 1635 ch = *psz; 1636 while (ch != '\0' && ch != '\n' && ch != '\r') 1637 ch = *++psz; 1638 1639 /* skip line end */ 1640 if (ch == '\n') 1641 psz++; 1642 if (*psz == '\r') 1643 psz++; 1644 1645 return psz; 1646 } 1647 1648 1649 /** 1650 * Appends a depend file to the internal file. 1651 */ 1652 static BOOL depReadFile(const char *pszFilename) 1653 { 1654 void *pvFile; 1655 char *pszNext; 1656 BOOL fMoreDeps = FALSE; 1657 void *pvRule = NULL; 1658 1659 /* read depend file */ 1660 pvFile = textbufferCreate(pszFilename); 1661 if (pvFile == NULL) 1662 return FALSE; 1663 1664 /* parse the original depend file */ 1665 pszNext = pvFile; 1666 while (*pszNext != '\0') 1667 { 1668 int i; 1669 int cch; 1670 char *psz; 1671 1672 /* get the next line. */ 1673 psz = pszNext; 1674 pszNext = textbufferNextLine(pvFile, pszNext); 1675 1676 /* 1677 * Process the current line: 1678 * Start off by terminating the line. 1679 * Trim the line, 1680 * Skip empty lines. 1681 * If not looking for more deps Then 1682 * Check if new rule starts here. 1683 * Endif 1684 * 1685 * If more deps to last rule Then 1686 * Get dependant name. 1687 * Endif 1688 */ 1689 i = -1; 1690 while (psz >= &pszNext[i] && pszNext[i] == '\n' || pszNext[i] == '\r') 1691 pszNext[i--] = '\0'; 1692 trimR(psz); 1693 cch = strlen(psz); 1694 if (cch == 0) 1695 continue; 1696 1697 /* new rule? */ 1698 if (!fMoreDeps && *psz != ' ' && *psz != '\t' && *psz != '\0') 1699 { 1700 while (psz[i] != '\0') 1701 { 1702 if (psz[i] == ':' 1703 && (psz[i+1] == ' ' 1704 || psz[i+1] == '\t' 1705 || psz[i+1] == '\0' 1706 || psz[i+1] == '\\' 1707 ) 1708 ) 1709 break; 1710 i++; 1711 } 1712 1713 if (psz[i] == ':') 1714 { /* new rule! */ 1715 psz[i] = '\0'; 1716 pvRule = depAddRule(trimR(psz)); 1717 psz += i + 1; 1718 fMoreDeps = TRUE; 1719 } 1720 } 1721 1722 /* more dependants */ 1723 if (fMoreDeps) 1724 { 1725 if (cch > 0 && psz[cch-1] == '\\') 1726 { 1727 fMoreDeps = TRUE; 1728 psz[cch-1] = '\0'; 1729 } 1730 else 1731 fMoreDeps = FALSE; 1732 psz = trim(psz); 1733 if (*psz != '\0') 1734 depAddDepend(pvRule, psz); 1735 } 1736 } /* while */ 1737 1738 1739 /* return succesfully */ 1740 textbufferDestroy(pvFile); 1741 return TRUE; 1742 } 1743 1744 /** 1745 * 1746 * @returns Success indicator. 1747 * @params pszFilename Pointer to name of the output file. 1748 */ 1749 static BOOL depWriteFile(const char *pszFilename) 1750 { 1751 FILE *phFile; 1752 phFile = fopen(pszFilename, "w"); 1753 if (phFile != NULL) 1754 { 1755 PDEPRULE pdep = pdepList; 1756 while (pdep != NULL) 1757 { 1758 1759 fprintf(phFile, "%s:", pdep->pszRule); 1760 if (pdep->papszDep != NULL) 1761 { 1762 char **ppsz = pdep->papszDep; 1763 while (*ppsz != NULL) 1764 { 1765 fprintf(phFile, " \\\n %s", *ppsz); 1766 1767 /* next dependant */ 1768 ppsz++; 1769 } 1770 } 1771 fputs("\n\n", phFile); 1772 1773 /* next rule */ 1774 pdep = pdep->pNext; 1775 } 1776 1777 fclose(phFile); 1778 return TRUE; 1779 } 1780 1781 return FALSE; 1782 } 1783 1784 /** 1785 * Adds a rule to the list of dependant rules. 1786 * @returns Rule handle. NULL if rule exists/error. 1787 * @param pszRule Pointer to rule text. Empty strings are banned! 1788 * 1789 */ 1790 static void *depAddRule(const char *pszRule) 1791 { 1792 PDEPRULE pdepPrev = NULL; 1793 PDEPRULE pdep = pdepList; 1794 PDEPRULE pNew; 1795 1796 /* find location */ 1797 while (pdep != NULL && 1798 stricmp(pdep->pszRule, pszRule) < 0 1799 ) 1800 { 1801 pdep = pdepPrev; 1802 pdep = pdep->pNext; 1803 } 1804 1805 /* check if matching rule name */ 1806 if (pdep != NULL && stricmp(pdep->pszRule, pszRule) == 0) 1807 return NULL; 1808 1809 /* allocate a new rule structure and fill in data */ 1810 pNew = malloc(sizeof(DEPRULE)); 1811 if (pNew == NULL) 1812 return NULL; 1813 pNew->pszRule = malloc(strlen(pszRule) + 1); 1814 if (pNew->pszRule == NULL) 1815 { 1816 free(pNew); 1817 return NULL; 1818 } 1819 strcpy(pNew->pszRule, pszRule); 1820 pNew->cDeps = 0; 1821 pNew->papszDep = NULL; 1822 1823 /* link in module (before pdep) */ 1824 pNew->pNext = pdep; 1825 if (pdepPrev == NULL) 1826 pdepList = pNew; 1827 else 1828 pdepPrev->pNext = pNew; 1829 1830 return pNew; 1831 } 1832 1833 1834 1835 /** 1836 * Adds a dependant to a rule. 1837 * @returns Successindicator. 1838 * @param pvRule Rule handle. 1839 * @param pszDep Pointer to dependant name 1840 */ 1841 static BOOL depAddDepend(void *pvRule, const char *pszDep) 1842 { 1843 PDEPRULE pdep = (PDEPRULE)pvRule; 1844 1845 /* allocate more array space */ 1846 if (pdep->cDeps == 0 || (pdep->cDeps + 2) % 32 == 0) 1847 { 1848 pdep->papszDep = realloc(pdep->papszDep, 32 * sizeof(char*)); 1849 if (pdep->papszDep == NULL) 1850 { 1851 pdep->cDeps = 0; 1852 return FALSE; 1853 } 1854 } 1855 1856 /* allocate string space and copy pszDep */ 1857 if ((pdep->papszDep[pdep->cDeps] = malloc(strlen(pszDep) + 1)) == NULL) 1858 return FALSE; 1859 strcpy(pdep->papszDep[pdep->cDeps], pszDep); 1860 1861 /* terminate array and increment dep count */ 1862 pdep->papszDep[++pdep->cDeps] = NULL; 1863 1864 /* successful! */ 1865 return TRUE; 1866 } 1867 1868 1869 1870 /** 1871 * Removes double dependencies. 1872 * @returns Success indicator. 1873 * @param pszFilename Depend filename. 1874 */ 1875 static BOOL depCleanFile(const char *pszFilename) 1876 { 1877 if (depReadFile(pszFilename)) 1878 return depWriteFile(pszFilename); 1879 return FALSE; 1880 } 1881 1882 1476 1883 /* 1477 * Testin purpose.1884 * Testing purpose. 1478 1885 */ 1479 1886 #include <os2.h>
Note:
See TracChangeset
for help on using the changeset viewer.
