home *** CD-ROM | disk | FTP | other *** search
- #include <SetupA4.h>
- #include <StandardFile.h>
-
- #include <stdio.h>
- #include <string.h>
-
- #include "DialogUtilities.h"
- #include "ExternalInterface.h"
-
- enum
- {
- source_files,
- not_found,
-
- neither_tree,
- project_tree,
- system_tree,
-
- total_counts,
-
- // string list indexes
- prj_info,
- prj_files,
- prj_lines,
- file_summary,
- file_name,
- file_lines
- };
-
- static long count_lines(ExternalCallbackBlock *callbacks, const FSSpec *spec)
- {
- Boolean dispose;
- Handle text;
-
- long line_count = 0;
-
- text = callbacks->GetFileText(spec->vRefNum, spec->parID, *(Str255 *)spec->name, &dispose);
-
- if (text != nil)
- {
- char *p = *text;
- long len = GetHandleSize(text);
-
- while (--len)
- {
- if (*(p++) == '\r')
- line_count++;
- }
-
- if ((*text)[len - 1] != '\r')
- line_count++;
-
- if (dispose)
- DisposHandle(text);
- }
-
- return line_count;
- }
-
- #define OUTPUT(fmt, arg) \
- {\
- GetIndString(fmt_str, 128, fmt + 1);\
- PtoCstr(fmt_str);\
- sprintf(output_str, (char *)fmt_str, arg);\
- err = PtrAndHand(output_str, output_text, strlen(output_str));\
- }\
-
-
- pascal void main(ExternalCallbackBlock *callbacks, WindowPtr w)
- {
- StandardFileReply reply;
- ProjectEntry **entries;
- short type;
- short count;
-
- OSErr err = noErr;
- short progress_counter = 0;
-
- RememberA0();
- SetUpA4();
-
- StandardGetFile(nil, 3, *(SFTypeList *)"PROJQPRJMMPR", &reply);
- if (reply.sfGood && callbacks->GetProjectList(&reply.sfFile, &type, &count, &entries))
- {
- short counts[total_counts];
-
- short i;
- long total_line_count;
-
- Str255 fmt_str;
- char output_str[512];
-
-
- Handle output_text;
-
- for (i = 0; i < total_counts; i++)
- counts[i] = 0;
-
- total_line_count = 0;
-
- HLockHi((Handle)entries);
-
- callbacks->StartProgress("\pCounting Project…", count * 2, FALSE);
- progress_counter = 0;
-
- for (i = 0; i < count; i++)
- {
- ProjectEntry entry = (*entries)[i];
-
- if (entry.found)
- {
- counts[neither_tree + entry.tree]++;
-
- if (entry.type == 'TEXT')
- {
- counts[source_files]++;
-
- // use the "crtr" field to store the line count for this file.
- // It comes in handy later...
- total_line_count += ((*entries)[i].crtr = count_lines(callbacks, &entry.spec));
- }
- }
- else
- counts[not_found]++;
-
- callbacks->DoProgress(progress_counter++);
- }
-
- output_text = callbacks->Allocate(0, FALSE);
-
- PtoCstr(reply.sfFile.name);
- OUTPUT(prj_info, reply.sfFile.name);
-
- OUTPUT(prj_files, count);
- OUTPUT(prj_lines, total_line_count);
-
- for (i = 0; (i < total_counts) && (err == noErr); i++)
- {
- if (counts[i] != 0)
- OUTPUT(i, counts[i]);
- }
-
- OUTPUT(file_summary, "---");
-
- for (i = 0; i < count; i++)
- {
- ProjectEntry entry = (*entries)[i];
-
- if (entry.found && (entry.type == 'TEXT'))
- {
- PtoCstr(entry.spec.name);
-
- OUTPUT(file_name, entry.spec.name);
- if (err != noErr)
- break;
-
- OUTPUT(file_lines, entry.crtr);
- if (err != noErr)
- break;
- }
-
- callbacks->DoProgress(progress_counter++);
- }
-
- callbacks->DoneProgress();
-
- if (err == noErr)
- {
- w = callbacks->NewDocument();
- callbacks->SetWindowContents(w, output_text);
- }
- else
- {
- DisposHandle(output_text);
- callbacks->ReportOSError(err);
- }
-
- DisposHandle((Handle)entries);
- }
-
- RestoreA4();
- }
-