Project: folder line counter
The idea: like a small cloc: it walks a folder and everything under it, counts the files, lines and characters for each extension, and counts the binary and forbidden files.
The data
Section titled “The data”A «وثائق» (documents) folder with a subfolder, text files with different extensions, a file with no extension, and a binary file:
بلا امتداد
# دليل سطر أول سطر ثانٍ
Binary file (5 bytes): not shown.
اطبع("مرحبا")
اطبع("وداعا")
اطبع("!")
# ملاحظات فقرة
The program
Section titled “The program”# برنامج نظامي صغير: يمشي في شجرة مجلد ويعدّ الملفات والأسطر لكل امتداد (كـ cloc مصغّر)استورد نظاماستورد ملفاتاستورد مساراتاستورد رياضيات
هيكل احصاء: امتداد نص ملفات عدد64 اسطر عدد64 بايتات عدد64
هيكل حصيلة: مجلدات عدد64 ثنائية عدد64 ممنوعة عدد64
داله سجّل(جدول قائمة[احصاء], فهرس قاموس[نص, عدد64], امتداد نص, اسطر عدد64, بايتات عدد64) فراغ: اذا !يوجد(فهرس, امتداد): فهرس[امتداد] = طول(جدول) اضف(جدول, احصاء(قص(امتداد, 0), 0, 0, 0)) # نسخة: امتداد عرض على مسار سيُحرَّر م = فهرس[امتداد] ا = جدول[م] ا.ملفات += 1 ا.اسطر += اسطر ا.بايتات += بايتات جدول[م] = ا
داله امش(مسار نص, جدول قائمة[احصاء], فهرس قاموس[نص, عدد64], ح مؤشر[حصيلة], عمق عدد64) فراغ: اذا عمق > 40: ارجع # لا طريقة لكشف الروابط الرمزية: حد للعمق بدلها اسماء = ملفات.سرد_مجلد(مسار) والا خ: حالات خ: حاله خطأ.ليس_مجلدا: عد_ملف(مسار, جدول, فهرس, ح) حاله خطأ.ممنوع: محتوى(ح).ممنوعة += 1 حاله _: اطبع("تعذّر: ", مسار, " (", خ, ")") ارجع خاتمه حرر(اسماء) محتوى(ح).مجلدات += 1 لكل اسم في اسماء: اذا يبدأ_بـ(اسم, "."): تجاوز كامل = مسارات.ضم(مسار, اسم) امش(كامل, جدول, فهرس, ح, عمق + 1) حرر(كامل)
داله عد_ملف(مسار نص, جدول قائمة[احصاء], فهرس قاموس[نص, عدد64], ح مؤشر[حصيلة]) فراغ: محتوى_الملف = ملفات.اقرأ_ملف(مسار) والا خ: حالات خ: حاله خطأ.ليس_نصا: محتوى(ح).ثنائية += 1 حاله خطأ.ممنوع: محتوى(ح).ممنوعة += 1 حاله _: اطبع("تعذّر: ", مسار, " (", خ, ")") ارجع اسطر = 0 لكل ح_ في محتوى_الملف: اذا ح_ == '\n': اسطر += 1 بايتات = طول(محتوى_الملف) # بالأحرف لا بالبايتات (كما في التقرير؛ طول_بايت منذ 0.14.0) امتداد = مسارات.الامتداد(مسار) والا "" اذا طول(امتداد) == 0: امتداد = "(بلا)" سجّل(جدول, فهرس, امتداد, اسطر, بايتات) حرر(محتوى_الملف)
داله قبل(ا احصاء, ب احصاء) منطقي: ارجع ا.اسطر > ب.اسطر
وسائط = نظام.وسائط()البداية = "."اذا طول(وسائط) > 0: البداية = وسائط[0]جدول قائمة[احصاء] = []فهرس قاموس[نص, عدد64] = {}ح = جديد(حصيلة(0, 0, 0))خاتمه حرر(ح)امش(البداية, جدول, فهرس, ح, 0)رتب_ب(جدول, قبل)اطبع("الامتداد الملفات الأسطر الأحرف")مج_ملفات = 0مج_اسطر = 0لكل ا في جدول: اطبع(ا.امتداد, "\t", رياضيات.نسق_عدد(ا.ملفات, 8, خطاء), رياضيات.نسق_عدد(ا.اسطر, 11, خطاء), رياضيات.نسق_عدد(ا.بايتات, 12, خطاء)) مج_ملفات += ا.ملفات مج_اسطر += ا.اسطراطبع("المجموع: ", مج_ملفات, " ملفاً، ", مج_اسطر, " سطراً، ", محتوى(ح).مجلدات, " مجلداً؛ ثنائية: ", محتوى(ح).ثنائية, "، ممنوعة: ", محتوى(ح).ممنوعة)Also built with meem build when the site was built: a static file of 71.4 kB, run on its own, and its output matched the output above.
Step by step
Section titled “Step by step”- Walking:
امش(walk) reads the folder’s names withملفات.سرد_مجلد(list folder). If that returns the errorليس_مجلدا(not a folder), the path is a file, soعد_ملفcounts it. Andحالات خtellsممنوع(forbidden) apart from other errors. - The file:
ملفات.اقرأ_ملفrejects anything that is not UTF-8 with the errorليس_نصا(not text), so the file is counted as binary. Lines are counted by going through the file’s characters. - The extension:
مسارات.الامتداد(مسار) والا ""(extension; it returnsنص؟since 0.12.0), thenسجّل(record) stores it in the table as a copy withقص(امتداد, 0). - The totals: a
حصيلة(totals) struct is allocated withجديد, and changed throughمحتوى(ح)in every function.
What was hard
Section titled “What was hard”- The dangling view, the worst thing in the report:
مسارات.الامتدادreturns a view onto the path. The view was first stored in the struct, then the path was freed, and the extension was printed corrupted (ss.instead of.md) with exit code 0.--debug-memdid not catch it in 0.9.1; it actually fixed the output and so hid the error. In 0.10.0, the detector that is on by default in a direct run catches it as error E0430, with the lines where the string was created and freed; in a file built without the detector, the output stays undefined. The program here keeps a copy withقص, so it has no dangling view. - No way to find a file’s type (no
stat), so a folder is told apart from a file by the error fromسرد_مجلد. - No detection of symbolic links, so the depth is limited to forty.
طولcounts characters, not bytes: the column is «characters», not «bytes». There was no function for the byte count in 0.9.1; since 0.14.0طول_بايتgives it, and the program here is as it was written for the report.
What we learned
Section titled “What we learned”- Handling errors with
حالات خturns «folder or file, allowed or forbidden, text or binary» into clear branches. - Everything that
مسارات.الامتداد,المجلد,اسم_الملف,جزءandقص_الفراغاتreturn is a view: if it must live longer than its original string, copy it.