Leaked source code of windows server 2003
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.

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