654 } else { |
676 } else { |
655 url = cx_strcat(2, base, path); |
677 url = cx_strcat(2, base, path); |
656 } |
678 } |
657 |
679 |
658 return url; |
680 return url; |
|
681 } |
|
682 |
|
683 cxmutstr util_concat_path_ext(cxstring base, cxstring path, char separator) { |
|
684 if(!path.ptr) { |
|
685 path = CX_STR(""); |
|
686 } |
|
687 |
|
688 int add_separator = 0; |
|
689 if(base.length != 0 && base.ptr[base.length-1] == separator) { |
|
690 if(path.ptr[0] == separator) { |
|
691 base.length--; |
|
692 } |
|
693 } else { |
|
694 if(path.length == 0 || path.ptr[0] != separator) { |
|
695 add_separator = 1; |
|
696 } |
|
697 } |
|
698 |
|
699 cxmutstr url; |
|
700 if(add_separator) { |
|
701 url = cx_strcat(3, base, cx_strn(&separator, 1), path); |
|
702 } else { |
|
703 url = cx_strcat(2, base, path); |
|
704 } |
|
705 |
|
706 return url; |
|
707 } |
|
708 |
|
709 cxmutstr util_concat_sys_path(cxstring base, cxstring path) { |
|
710 #ifdef _WIN32 |
|
711 return util_concat_path_ext(base, path, '\\'); |
|
712 #else |
|
713 return util_concat_path_ext(base, path, '/'); |
|
714 #endif |
659 } |
715 } |
660 |
716 |
661 char* util_get_url(DavSession *sn, const char *href) { |
717 char* util_get_url(DavSession *sn, const char *href) { |
662 cxstring base = cx_str(sn->base_url); |
718 cxstring base = cx_str(sn->base_url); |
663 cxstring href_str = cx_str(href); |
719 cxstring href_str = cx_str(href); |
714 memcpy(parent, path, parentlen); |
770 memcpy(parent, path, parentlen); |
715 parent[parentlen] = '\0'; |
771 parent[parentlen] = '\0'; |
716 return parent; |
772 return parent; |
717 } |
773 } |
718 |
774 |
|
775 char* util_sys_parent_path(const char *path) { |
|
776 const char *name = util_path_file_name(path); |
|
777 size_t namelen = strlen(name); |
|
778 size_t pathlen = strlen(path); |
|
779 size_t parentlen = pathlen - namelen; |
|
780 char *parent = malloc(parentlen + 1); |
|
781 memcpy(parent, path, parentlen); |
|
782 parent[parentlen] = '\0'; |
|
783 return parent; |
|
784 } |
|
785 |
719 char* util_size_str(DavBool iscollection, uint64_t contentlength) { |
786 char* util_size_str(DavBool iscollection, uint64_t contentlength) { |
|
787 return util_size_str2(iscollection, contentlength, contentlength, 1); |
|
788 } |
|
789 |
|
790 char* util_size_str2(DavBool iscollection, uint64_t contentlength, uint64_t dimension, int precision) { |
720 char *str = malloc(16); |
791 char *str = malloc(16); |
721 uint64_t size = contentlength; |
792 uint64_t size = contentlength; |
722 |
793 |
723 if(iscollection) { |
794 if(iscollection) { |
724 str[0] = '\0'; // currently no information for collections |
795 str[0] = '\0'; // currently no information for collections |
725 } else if(size < 0x400) { |
796 } else if(dimension < 0x400) { |
726 snprintf(str, 16, "%" PRIu64 " bytes", size); |
797 snprintf(str, 16, "%" PRIu64 " bytes", size); |
727 } else if(size < 0x100000) { |
798 } else if(dimension < 0x100000) { |
728 float s = (float)size/0x400; |
799 float s = (float)size/0x400; |
729 int diff = (s*100 - (int)s*100); |
800 int diff = (s*100 - (int)s*100); |
730 if(diff > 90) { |
801 if(diff > 90) { |
731 diff = 0; |
802 diff = 0; |
732 s += 0.10f; |
803 s += 0.10f; |
733 } |
804 } |
734 if(size < 0x2800 && diff != 0) { |
805 if(dimension < 0x2800 && diff != 0) { |
735 // size < 10 KiB |
806 // size < 10 KiB |
736 snprintf(str, 16, "%.1f KiB", s); |
807 snprintf(str, 16, "%.*f KiB", precision, s); |
737 } else { |
808 } else { |
738 snprintf(str, 16, "%.0f KiB", s); |
809 snprintf(str, 16, "%.0f KiB", s); |
739 } |
810 } |
740 } else if(size < 0x40000000) { |
811 } else if(dimension < 0x40000000) { |
741 float s = (float)size/0x100000; |
812 float s = (float)size/0x100000; |
742 int diff = (s*100 - (int)s*100); |
813 int diff = (s*100 - (int)s*100); |
743 if(diff > 90) { |
814 if(diff > 90) { |
744 diff = 0; |
815 diff = 0; |
745 s += 0.10f; |
816 s += 0.10f; |
746 } |
817 } |
747 if(size < 0xa00000 && diff != 0) { |
818 if(dimension < 0xa00000 && diff != 0) { |
748 // size < 10 MiB |
819 // size < 10 MiB |
749 snprintf(str, 16, "%.1f MiB", s); |
820 snprintf(str, 16, "%.*f MiB", precision, s); |
750 } else { |
821 } else { |
751 size /= 0x100000; |
822 size /= 0x100000; |
752 snprintf(str, 16, "%.0f MiB", s); |
823 snprintf(str, 16, "%.0f MiB", s); |
753 } |
824 } |
754 } else if(size < 0x1000000000ULL) { |
825 } else if(dimension < 0x1000000000ULL) { |
755 float s = (float)size/0x40000000; |
826 float s = (float)size/0x40000000; |
756 int diff = (s*100 - (int)s*100); |
827 int diff = (s*100 - (int)s*100); |
757 if(diff > 90) { |
828 if(diff > 90) { |
758 diff = 0; |
829 diff = 0; |
759 s += 0.10f; |
830 s += 0.10f; |
760 } |
831 } |
761 if(size < 0x280000000 && diff != 0) { |
832 if(dimension < 0x280000000 && diff != 0) { |
762 // size < 10 GiB |
833 // size < 10 GiB |
763 snprintf(str, 16, "%.1f GiB", s); |
834 snprintf(str, 16, "%.*f GiB", precision, s); |
764 } else { |
835 } else { |
765 size /= 0x40000000; |
836 size /= 0x40000000; |
766 snprintf(str, 16, "%.0f GiB", s); |
837 snprintf(str, 16, "%.0f GiB", s); |
767 } |
838 } |
768 } else { |
839 } else { |