Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

327 lines
8.7 KiB

  1. #include "loc.h"
  2. /* Statistic variables */
  3. unsigned int lines; /* lines of text */
  4. unsigned int locs; /* lines of code */
  5. unsigned int semis; /* semicolons */
  6. unsigned int clocs; /* commented lines of code */
  7. unsigned int cls; /* comment lines */
  8. unsigned int totfiles = 0; /* #files */
  9. unsigned long totlines = 0L; /* total lines of text */
  10. unsigned long totlocs = 0L; /* total lines of code */
  11. unsigned long totsemis = 0L; /* total semicolons */
  12. unsigned long totclocs = 0L; /* total commented lines of code */
  13. unsigned long totcls = 0L; /* total comment lines */
  14. char *pTotStr;
  15. char linebuf[ 256 ];
  16. int fBanner, fErr, fDetail, fRbrace;
  17. FILE *locout;
  18. FILE *fh;
  19. char buf[2];
  20. char ReadChar( void ) {
  21. if (buf[0] != (char)EOF) {
  22. buf[0] = buf[1];
  23. buf[1] = (char)fgetc( fh );
  24. }
  25. return( buf[0] );
  26. }
  27. char PeekChar( void ) {
  28. if (buf[0] != (char)EOF) {
  29. return( buf[ 1 ] );
  30. }
  31. else {
  32. return( (char)EOF );
  33. }
  34. }
  35. void
  36. ProcessFile(
  37. char *pName,
  38. struct findType *pbuf,
  39. void *Args
  40. )
  41. {
  42. unsigned int CommentFound, CommentLevel, CountChars;
  43. register char *s;
  44. char c, QuoteChar;
  45. if (!(fh = fopen( pName, "rt" ))) {
  46. fprintf( stderr, "*** Unable to read file - %s\n", pName );
  47. fErr = TRUE;
  48. return;
  49. }
  50. buf[0] = 0;
  51. buf[1] = 0;
  52. ReadChar();
  53. if (!fBanner) {
  54. fprintf( locout, "%-14s %7s %7s %9s %7s %7s %7s %8s\n",
  55. "",
  56. "", "", "Commented",
  57. "Comment", "Comment", "", "LOC/semi" );
  58. fprintf( locout, "%-14s %7s %7s %9s %7s %7s %7s %8s\n",
  59. fDetail ? "File Name" : "Component Name",
  60. "Lines", "LOCS", "LOCS",
  61. "Lines", "Ratio", "Semis", "Ratio" );
  62. fBanner = TRUE;
  63. }
  64. lines = 0;
  65. locs = 0;
  66. semis = 0;
  67. clocs = 0;
  68. cls = 0;
  69. CommentLevel = 0;
  70. QuoteChar = 0;
  71. CountChars = 0;
  72. CommentFound = FALSE;
  73. while ((c = ReadChar()) != (char)EOF) {
  74. if (c == '\n') {
  75. lines++;
  76. if (CountChars) {
  77. if (CommentFound) {
  78. clocs++;
  79. }
  80. locs++;
  81. CountChars = 0;
  82. }
  83. else
  84. if (CommentFound) {
  85. cls++;
  86. }
  87. CommentFound = FALSE;
  88. }
  89. else
  90. if (c == '/') {
  91. if (PeekChar() == '*') {
  92. CommentLevel++;
  93. ReadChar();
  94. while ((c = ReadChar()) != (char)EOF) {
  95. if (c == '/') {
  96. if (PeekChar() == '*') {
  97. ReadChar();
  98. CommentLevel++;
  99. }
  100. else {
  101. CommentFound = TRUE;
  102. }
  103. }
  104. else
  105. if (c == '*') {
  106. if (PeekChar() == '/') {
  107. ReadChar();
  108. if (!--CommentLevel) {
  109. break;
  110. }
  111. }
  112. else {
  113. CommentFound = TRUE;
  114. }
  115. }
  116. else
  117. if (c == '\n') {
  118. lines++;
  119. if (CommentFound) {
  120. cls++;
  121. CommentFound = 0;
  122. }
  123. }
  124. else
  125. if (c > ' ') {
  126. CommentFound = TRUE;
  127. }
  128. }
  129. }
  130. else
  131. if (PeekChar() == '/') {
  132. ReadChar();
  133. while ((c = PeekChar()) != '\n') {
  134. if (c == (char)EOF) {
  135. break;
  136. }
  137. else
  138. if (ReadChar() > ' ') {
  139. CommentFound = TRUE;
  140. }
  141. }
  142. }
  143. else {
  144. CountChars++;
  145. }
  146. }
  147. else
  148. if (c == '\'' || c == '\"') {
  149. QuoteChar = c;
  150. CountChars++;
  151. while ((c = ReadChar()) != (char)EOF) {
  152. CountChars++;
  153. if (c == '\\') {
  154. ReadChar();
  155. }
  156. else
  157. if (c == QuoteChar) {
  158. if (PeekChar() == QuoteChar) {
  159. CountChars++;
  160. ReadChar();
  161. }
  162. else {
  163. break;
  164. }
  165. }
  166. else
  167. if (c == '\n') {
  168. lines++;
  169. locs++;
  170. }
  171. }
  172. }
  173. else
  174. if (fRbrace && (c == '}')) {
  175. if (CountChars) {
  176. CountChars++;
  177. }
  178. }
  179. else {
  180. if (c == ';') {
  181. semis++;
  182. }
  183. if (CountChars || c > ' ') {
  184. CountChars++;
  185. }
  186. }
  187. }
  188. if (fDetail)
  189. fprintf( locout, "%7u %7u %9u %7u %7.2f %7u %8.2f %s\n",
  190. lines, locs, clocs, cls,
  191. ((float) cls + (float) clocs / 2) / (float)( locs ? locs : 1),
  192. semis,
  193. (float) locs / (float)(semis ? semis : 1) , pName);
  194. totfiles++;
  195. totlines += lines;
  196. totlocs += locs;
  197. totsemis += semis;
  198. totclocs += clocs;
  199. totcls += cls;
  200. fclose( fh );
  201. }
  202. void
  203. DoTotals() {
  204. if (totfiles)
  205. fprintf( locout, "%-14s %7lu %7lu %9lu %7lu %7.2f %7lu %8.2f\n",
  206. pTotStr ? pTotStr : "Totals",
  207. totlines, totlocs, totclocs, totcls,
  208. ((float) totcls + (float) totclocs / 2) /
  209. (float)(totlocs ? totlocs : 1),
  210. totsemis,
  211. (float) totlocs / (float)(totsemis ? totsemis : 1) );
  212. if (pTotStr) {
  213. free( pTotStr );
  214. pTotStr = NULL;
  215. }
  216. totfiles = 0;
  217. totlocs = 0L;
  218. totsemis = 0L;
  219. totclocs = 0L;
  220. totcls = 0L;
  221. }
  222. int
  223. __cdecl main( argc, argv )
  224. int argc;
  225. char *argv[];
  226. {
  227. FILE *fh;
  228. char *p, buf[ 128 ], *p1;
  229. int i;
  230. ConvertAppToOem( argc, argv );
  231. fErr = FALSE;
  232. fDetail = TRUE;
  233. fRbrace = TRUE;
  234. pTotStr = NULL;
  235. locout = stdout;
  236. for (i=1; i<argc; i++) {
  237. p = argv[ i ];
  238. if (*p == '/' || *p == '-') {
  239. while (*++p)
  240. switch (toupper( *p )) {
  241. case 'S': {
  242. fDetail = FALSE;
  243. break;
  244. }
  245. case 'B': {
  246. fRbrace = FALSE;
  247. break;
  248. }
  249. case 'F': {
  250. strcpy( buf, argv[ ++i ] );
  251. p1 = strbscan( buf, "." );
  252. if (!*p1)
  253. strcpy( p1, ".loc" );
  254. if (fh = fopen( buf, "r" )) {
  255. while (fgetl( buf, 128, fh )) {
  256. if (buf[ 0 ] == '*') {
  257. DoTotals();
  258. pTotStr = MakeStr( strbskip( buf, "*" ) );
  259. }
  260. else
  261. if (buf[ 0 ])
  262. forfile( buf, -1, ProcessFile, NULL );
  263. }
  264. DoTotals();
  265. fclose( fh );
  266. }
  267. else {
  268. fprintf( stderr, "Unable to open response file - %s\n",
  269. buf );
  270. fErr = TRUE;
  271. }
  272. break;
  273. }
  274. default: {
  275. fprintf( stderr, "*** Invalid switch - /%c\n", *p );
  276. fErr = TRUE;
  277. }
  278. }
  279. }
  280. else
  281. forfile( p, -1, ProcessFile, NULL );
  282. }
  283. if (fErr) {
  284. fprintf( stderr, "Usage: LOC [/S] [/F responseFile] fileTemplates ...\n" );
  285. fprintf( stderr, " /S - summary only (no individual file counts)\n" );
  286. fprintf( stderr, " /B - old mode, counting lone } lines as LOCs\n" );
  287. fprintf( stderr, " /F - read file templates from responseFile\n" );
  288. }
  289. else
  290. DoTotals();
  291. return( 0 );
  292. }
  293.